|
35 | 35 | import com.facebook.react.bridge.ReactContext;
|
36 | 36 |
|
37 | 37 | import com.github.scribejava.core.builder.api.BaseApi;
|
| 38 | +import com.github.scribejava.core.model.Verb; |
38 | 39 |
|
39 | 40 | // import com.wuman.android.auth.AuthorizationDialogController;
|
40 | 41 | // import com.wuman.android.auth.AuthorizationFlow;
|
@@ -191,31 +192,81 @@ public void makeRequest(
|
191 | 192 | final Callback onComplete) {
|
192 | 193 |
|
193 | 194 | Log.i(TAG, "makeRequest called for " + providerName + " to " + urlString);
|
194 |
| - // try { |
195 |
| - // Credential creds = this.loadCredentialForProvider(providerName, params); |
196 |
| - // HashMap<String,String> cfg = this.getConfiguration(providerName); |
197 |
| - // final String authVersion = (String) cfg.get("auth_version"); |
198 |
| - |
199 |
| - // URL url; |
200 |
| - // try { |
201 |
| - // if (urlString.contains("http")) { |
202 |
| - // url = new URL(urlString); |
203 |
| - // } else { |
204 |
| - // String apiHost = (String) cfg.get("api_url"); |
205 |
| - // url = new URL(apiHost + urlString); |
206 |
| - // } |
207 |
| - // } catch (MalformedURLException ex) { |
208 |
| - // Log.e(TAG, "Bad url. Check request and try again: " + ex.getMessage()); |
209 |
| - // exceptionCallback(ex, onComplete); |
210 |
| - // return; |
211 |
| - // } |
212 |
| - |
| 195 | + try { |
| 196 | + HashMap<String,String> cfg = this.getConfiguration(providerName); |
| 197 | + final String authVersion = (String) cfg.get("auth_version"); |
| 198 | + |
| 199 | + URL url; |
| 200 | + try { |
| 201 | + if (urlString.contains("http")) { |
| 202 | + url = new URL(urlString); |
| 203 | + } else { |
| 204 | + String apiHost = (String) cfg.get("api_url"); |
| 205 | + url = new URL(apiHost + urlString); |
| 206 | + } |
| 207 | + } catch (MalformedURLException ex) { |
| 208 | + Log.e(TAG, "Bad url. Check request and try again: " + ex.getMessage()); |
| 209 | + exceptionCallback(ex, onComplete); |
| 210 | + return; |
| 211 | + } |
| 212 | + |
| 213 | + String httpMethod; |
| 214 | + if (params.hasKey("method")) { |
| 215 | + httpMethod = params.getString("method"); |
| 216 | + } else { |
| 217 | + httpMethod = "GET"; |
| 218 | + } |
| 219 | + |
| 220 | + Verb httpVerb; |
| 221 | + if (httpMethod.equalsIgnoreCase("GET")) { |
| 222 | + httpVerb = Verb.GET; |
| 223 | + } else if (httpMethod.equalsIgnoreCase("POST")) { |
| 224 | + httpVerb = Verb.POST; |
| 225 | + } else if (httpMethod.equalsIgnoreCase("PUT")) { |
| 226 | + httpVerb = Verb.PUT; |
| 227 | + } else if (httpMethod.equalsIgnoreCase("DELETE")) { |
| 228 | + httpVerb = Verb.DELETE; |
| 229 | + } else if (httpMethod.equalsIgnoreCase("OPTIONS")) { |
| 230 | + httpVerb = Verb.OPTIONS; |
| 231 | + } else if (httpMethod.equalsIgnoreCase("HEAD")) { |
| 232 | + httpVerb = Verb.HEAD; |
| 233 | + } else if (httpMethod.equalsIgnoreCase("PATCH")) { |
| 234 | + httpVerb = Verb.PATCH; |
| 235 | + } else if (httpMethod.equalsIgnoreCase("TRACE")) { |
| 236 | + httpVerb = Verb.TRACE; |
| 237 | + } else { |
| 238 | + httpVerb = Verb.GET; |
| 239 | + } |
213 | 240 |
|
| 241 | + if (authVersion.equals("1.0")) { |
| 242 | + final OAuth10aService service = |
| 243 | + OAuthManagerProviders.getApiFor10aProvider(providerName, cfg, null); |
| 244 | + OAuth1AccessToken token = _credentialsStore.get(providerName, OAuth1AccessToken.class); |
| 245 | + final OAuthRequest request = new OAuthRequest(Verb.GET, url.toString(), service); |
| 246 | + |
| 247 | + service.signRequest(token, request); |
| 248 | + final Response response = request.send(); |
| 249 | + final String rawBody = response.getBody(); |
| 250 | + |
| 251 | + Log.d(TAG, "rawBody: " + rawBody); |
| 252 | + // final Object response = new Gson().fromJson(rawBody, Object.class); |
| 253 | + |
| 254 | + WritableMap resp = Arguments.createMap(); |
| 255 | + resp.putInt("status", response.getCode()); |
| 256 | + resp.putString("data", rawBody); |
| 257 | + onComplete.invoke(null, resp); |
| 258 | + } else { |
| 259 | + // TODO: |
| 260 | + } |
214 | 261 |
|
215 |
| - // } catch (Exception ex) { |
216 |
| - // Log.e(TAG, "Exception when making request: " + ex.getMessage()); |
217 |
| - // exceptionCallback(ex, onComplete); |
218 |
| - // } |
| 262 | + } catch (IOException ex) { |
| 263 | + Log.e(TAG, "IOException when making request: " + ex.getMessage()); |
| 264 | + ex.printStackTrace(); |
| 265 | + exceptionCallback(ex, onComplete); |
| 266 | + } catch (Exception ex) { |
| 267 | + Log.e(TAG, "Exception when making request: " + ex.getMessage()); |
| 268 | + exceptionCallback(ex, onComplete); |
| 269 | + } |
219 | 270 | }
|
220 | 271 |
|
221 | 272 | @ReactMethod
|
|
0 commit comments