-
Notifications
You must be signed in to change notification settings - Fork 58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pass REST params and content to extensions #163
Changes from 7 commits
70a8c32
16854bd
61efa08
f29c893
89eee75
ffa6b38
a9033cb
ca71302
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,15 +7,14 @@ | |
*/ | ||
package org.opensearch.sdk.sample.helloworld.rest; | ||
|
||
import org.opensearch.extensions.rest.ExtensionRestRequest; | ||
import org.opensearch.rest.RestHandler.Route; | ||
import org.opensearch.rest.RestRequest.Method; | ||
import org.opensearch.sdk.ExtensionRestHandler; | ||
import org.opensearch.sdk.ExtensionRestRequest; | ||
import org.opensearch.sdk.ExtensionRestResponse; | ||
|
||
import java.net.URLDecoder; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.opensearch.rest.RestRequest.Method.GET; | ||
|
@@ -39,31 +38,32 @@ public List<Route> routes() { | |
|
||
@Override | ||
public ExtensionRestResponse handleRequest(ExtensionRestRequest request) { | ||
// We need to track which parameters are consumed to pass back to OpenSearch | ||
List<String> consumedParams = new ArrayList<>(); | ||
Method method = request.method(); | ||
String uri = request.uri(); | ||
|
||
if (Method.GET.equals(method) && "/hello".equals(uri)) { | ||
return new ExtensionRestResponse(OK, String.format(GREETING, worldName), consumedParams); | ||
} else if (Method.PUT.equals(method) && uri.startsWith("/hello/")) { | ||
// Placeholder code here for parameters in named wildcard paths | ||
// Full implementation based on params() will be implemented as part of | ||
// https://github.com/opensearch-project/opensearch-sdk-java/issues/111 | ||
String name = uri.substring("/hello/".length()); | ||
consumedParams.add("name"); | ||
try { | ||
worldName = URLDecoder.decode(name, StandardCharsets.UTF_8); | ||
} catch (IllegalArgumentException e) { | ||
return new ExtensionRestResponse(BAD_REQUEST, e.getMessage(), consumedParams); | ||
} | ||
return new ExtensionRestResponse(OK, "Updated the world's name to " + worldName, consumedParams); | ||
if (Method.GET.equals(method)) { | ||
return handleGetRequest(request); | ||
} else if (Method.PUT.equals(method)) { | ||
return handlePutRequest(request); | ||
} | ||
return new ExtensionRestResponse( | ||
NOT_FOUND, | ||
"Extension REST action improperly configured to handle " + method.name() + " " + uri, | ||
consumedParams | ||
); | ||
return handleBadRequest(request); | ||
} | ||
|
||
private ExtensionRestResponse handleGetRequest(ExtensionRestRequest request) { | ||
return new ExtensionRestResponse(request, OK, String.format(GREETING, worldName)); | ||
} | ||
|
||
private ExtensionRestResponse handlePutRequest(ExtensionRestRequest request) { | ||
String name = request.param("name"); | ||
try { | ||
worldName = URLDecoder.decode(name, StandardCharsets.UTF_8); | ||
} catch (IllegalArgumentException e) { | ||
return new ExtensionRestResponse(request, BAD_REQUEST, e.getMessage()); | ||
} | ||
return new ExtensionRestResponse(request, OK, "Updated the world's name to " + worldName); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we have a sample POST request too here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can I do that as part of #165? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure @dbwiddis! Not blocking this PR :) |
||
private ExtensionRestResponse handleBadRequest(ExtensionRestRequest request) { | ||
return new ExtensionRestResponse(request, NOT_FOUND, "Extension REST action improperly configured to handle " + request.toString()); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thinking out loud. Why are we keeping
ExtensionRestRequest
in OpenSearch and not in SDK?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because the previous
RestSendToExtensionRequest
entirely duplicated it. Literally all the lines were the same except for a few naming differences.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also it makes sense (see #165) to move the ExtensionRestResponse there as well since it's really the same thing as the BytesRestResponse with some tweaks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This answers my questions. Just one thing to add can we keep the name as
RestSendToExtensionRequest
instead ofExtensionRestRequest
for better understanding. Can be address in the next PRThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let’s discuss on #165, because both classes are part of the API we expect extension authors to use. The ExtensionRestRequest is the argument type for the handleRequest method and it closely parallels the same method on plugins and I’d really like similar names.