Skip to content
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

How to get result from external API to response for this #258

Closed
thinhtranquoc opened this issue Apr 8, 2021 · 3 comments
Closed

How to get result from external API to response for this #258

thinhtranquoc opened this issue Apr 8, 2021 · 3 comments
Labels
question Further information is requested

Comments

@thinhtranquoc
Copy link

I see an example for httpcall
https://github.com/envoyproxy/envoy-wasm/blob/dfb104874b3babc7e676aaa0199f2926410c0671/test/extensions/filters/http/wasm/test_data/async_call_cpp.cc#L15

I have a question. How to not using async in this case? Because I want to return the result of the external API.

auto callback = [context_id](uint32_t headerCount, size_t body_size, uint32_t trailerCount) {
WasmDataPtr wdpGhmv = getHeaderMapValue(WasmHeaderMapType::HttpCallResponseHeaders, ":status");
uint32_t status = std::stoi(wdpGhmv->toString());
WasmDataPtr wdpBody = getBufferBytes(WasmBufferType::HttpCallResponseBody, 0, body_size);
std::string reponse = wdpBody->toString();
getContext(context_id)->setEffectiveContext();
if (status == 200){
sendLocalResponse(428, "Precondition Required", reponse, {});
return FilterHeadersStatus::StopIteration;
}
};

@thinhtranquoc thinhtranquoc added the question Further information is requested label Apr 8, 2021
@Sodman
Copy link
Member

Sodman commented Apr 8, 2021

Hi @thinhtranquoc. While the calls will always technically be async (as we don't want to block Envoy, we're on the critical request path!), there is a way to do what I believe you're probably trying to do, which is essentially:

  • Receive a request in Envoy
  • Send a request to some external service
  • Wait for the response
  • If the response fits some criteria, allow continue, otherwise reject request.

You can do this by making an async request, pausing via a StopIteration, and then in the callback if everything looks good you can resume iteration with a call to proxy_continue_stream.

Here is an example of this exact flow working using our AssemblyScript proxy runtime - https://github.com/solo-io/proxy-runtime/blob/master/examples/auth/assembly/index.ts#L37-L60

@thinhtranquoc
Copy link
Author

Thanks, @Sodman, I will try it.

@Sodman
Copy link
Member

Sodman commented Jun 7, 2021

Closing this issue due to inactivity, let me know if there's still any issues and we can re-open!

@Sodman Sodman closed this as completed Jun 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants