forked from auth0/auth0-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseRequest.java
More file actions
38 lines (30 loc) · 1.04 KB
/
BaseRequest.java
File metadata and controls
38 lines (30 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.auth0.net;
import com.auth0.exception.Auth0Exception;
import okhttp3.OkHttpClient;
import okhttp3.Response;
import java.io.IOException;
public abstract class BaseRequest<T> implements Request<T> {
private final OkHttpClient client;
BaseRequest(OkHttpClient client) {
this.client = client;
}
protected abstract okhttp3.Request createRequest() throws Auth0Exception;
protected abstract T parseResponse(Response response) throws Auth0Exception;
/**
* Executes this request.
*
* @return the response body JSON decoded as T
* @throws Auth0Exception if the request execution fails.
*/
@Override
public T execute() throws Auth0Exception {
okhttp3.Request request = createRequest();
try (Response response = client.newCall(request).execute()) {
return parseResponse(response);
} catch (Auth0Exception e) {
throw e;
} catch (IOException e) {
throw new Auth0Exception("Failed to execute request", e);
}
}
}