|
18 | 18 |
|
19 | 19 | package io.ballerina.stdlib.oauth2; |
20 | 20 |
|
| 21 | +import io.ballerina.runtime.api.Environment; |
21 | 22 | import io.ballerina.runtime.api.creators.ErrorCreator; |
22 | 23 | import io.ballerina.runtime.api.utils.StringUtils; |
23 | 24 | import io.ballerina.runtime.api.values.BError; |
@@ -56,8 +57,8 @@ public class OAuth2Client { |
56 | 57 |
|
57 | 58 | private OAuth2Client() {} |
58 | 59 |
|
59 | | - public static Object doHttpRequest(BString url, BMap<BString, Object> clientConfig, BMap<BString, BString> headers, |
60 | | - BString payload) { |
| 60 | + public static Object doHttpRequest(Environment env, BString url, BMap<BString, Object> clientConfig, |
| 61 | + BMap<BString, BString> headers, BString payload) { |
61 | 62 | BString customPayload = getBStringValueIfPresent(clientConfig, OAuth2Constants.CUSTOM_PAYLOAD); |
62 | 63 | String textPayload = payload.getValue(); |
63 | 64 | if (customPayload != null) { |
@@ -100,13 +101,13 @@ public static Object doHttpRequest(BString url, BMap<BString, Object> clientConf |
100 | 101 | try { |
101 | 102 | SSLContext sslContext = getSslContext(secureSocket); |
102 | 103 | HttpClient client = buildHttpClient(httpVersion, sslContext); |
103 | | - return callEndpoint(client, request); |
| 104 | + return callEndpoint(env, client, request); |
104 | 105 | } catch (Exception e) { |
105 | 106 | return createError("Failed to init SSL context. " + e.getMessage()); |
106 | 107 | } |
107 | 108 | } |
108 | 109 | HttpClient client = buildHttpClient(httpVersion); |
109 | | - return callEndpoint(client, request); |
| 110 | + return callEndpoint(env, client, request); |
110 | 111 | } |
111 | 112 |
|
112 | 113 | private static URI buildUri(String url, BMap<BString, ?> secureSocket) throws IllegalArgumentException { |
@@ -300,17 +301,19 @@ private static HttpRequest buildHttpRequest(URI uri, String[] headers, String pa |
300 | 301 | .build(); |
301 | 302 | } |
302 | 303 |
|
303 | | - private static Object callEndpoint(HttpClient client, HttpRequest request) { |
304 | | - try { |
305 | | - HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); |
306 | | - if (response.statusCode() >= 200 && response.statusCode() < 300) { |
307 | | - return StringUtils.fromString(response.body()); |
| 304 | + private static Object callEndpoint(Environment env, HttpClient client, HttpRequest request) { |
| 305 | + return env.yieldAndRun(() -> { |
| 306 | + try { |
| 307 | + HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); |
| 308 | + if (response.statusCode() >= 200 && response.statusCode() < 300) { |
| 309 | + return StringUtils.fromString(response.body()); |
| 310 | + } |
| 311 | + return createError("Failed to get a success response from the endpoint. Response code: '" + |
| 312 | + response.statusCode() + "'. Response body: '" + response.body() + "'"); |
| 313 | + } catch (IOException | InterruptedException e) { |
| 314 | + return createError("Failed to send the request to the endpoint. " + e.getMessage()); |
308 | 315 | } |
309 | | - return createError("Failed to get a success response from the endpoint. Response code: '" + |
310 | | - response.statusCode() + "'. Response body: '" + response.body() + "'"); |
311 | | - } catch (IOException | InterruptedException e) { |
312 | | - return createError("Failed to send the request to the endpoint. " + e.getMessage()); |
313 | | - } |
| 316 | + }); |
314 | 317 | } |
315 | 318 |
|
316 | 319 | private static BMap<BString, ?> getBMapValueIfPresent(BMap<BString, ?> config, BString key) { |
|
0 commit comments