Class OneDriveConnection
java.lang.Object
com.amilesend.onedrive.connection.OneDriveConnection
Wraps a
OkHttpClient that manages authentication refresh and parsing responses to corresponding POJO types.
Construct a new instance via the OneDriveConnectionBuilder that simplifies configuration of the
AuthManager and automatically sets a pre-configured Gson instance for proper request/response
serialization.- See Also:
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionOneDriveConnection(@NonNull okhttp3.OkHttpClient httpClient, @NonNull AuthManager authManager, @NonNull GsonFactory gsonFactory, String baseUrl) Creates a newOneDriveConnectionobject. -
Method Summary
Modifier and TypeMethodDescriptionlongdownload(@NonNull okhttp3.Request request, @NonNull Path folderPath, String name, long sizeBytes, @NonNull TransferProgressCallback callback) Downloads the contents for the givenrequestto the specifiedfolderPathandname.downloadAsync(@NonNull okhttp3.Request request, @NonNull Path folderPath, String name, long sizeBytes, @NonNull TransferProgressCallback callback) Downloads the contents for the givenrequestasynchronously to the specifiedfolderPathandname.intexecute(@NonNull okhttp3.Request request) Executes the givenRequestand returns the associated HTTP response code.<T> Texecute(@NonNull okhttp3.Request request, @NonNull GsonParser<T> parser) Executes the givenRequestand parses the JSON-formatted response with givenGsonParser.<T> CompletableFuture<T> executeAsync(@NonNull okhttp3.Request request, @NonNull GsonParser<T> parser) Executes the givenRequestand parses the JSON-formatted response with givenGsonParser.executeRemoteAsync(@NonNull okhttp3.Request request) Executes the givenRequestfor a remote asynchronous operation and returns the monitoring URL.The authorization manager used to manage auth tokens.The base URL for the Graph API.com.google.gson.GsongetGson()The configured GSON instance used for marshalling request and responses to/from JSON.okhttp3.Request.BuilderCreates a newRequest.Builderwith pre-configured headers for request that expect a JSON-formatted response body.okhttp3.Request.BuilderCreates a newRequest.Builderwith pre-configured headers for a request that contains both a JSON-formatted request and response body.okhttp3.Request.BuilderCreates a newRequest.Builderwith pre-configured headers for requests that expect no responses in the body (e.g., post or upload/download operations).
-
Field Details
-
JSON_CONTENT_TYPE
-
JSON_MEDIA_TYPE
public static final okhttp3.MediaType JSON_MEDIA_TYPE
-
-
Constructor Details
-
OneDriveConnection
public OneDriveConnection(@NonNull okhttp3.OkHttpClient httpClient, @NonNull AuthManager authManager, @NonNull GsonFactory gsonFactory, String baseUrl) Creates a newOneDriveConnectionobject. It is recommended to use theOneDriveConnectionBuilderinstead.- Parameters:
httpClient- the configured HTTP clientauthManager- the authorization manager used to manage auth and refresh tokensgsonFactory- the factory used to vend configured GSON instances for request/reply serializationbaseUrl- the base URL to use for Graph API invocations- See Also:
-
-
Method Details
-
newSignedForRequestBuilder
public okhttp3.Request.Builder newSignedForRequestBuilder()Creates a newRequest.Builderwith pre-configured headers for requests that expect no responses in the body (e.g., post or upload/download operations).- Returns:
- the request builder.
-
newSignedForApiRequestBuilder
public okhttp3.Request.Builder newSignedForApiRequestBuilder()Creates a newRequest.Builderwith pre-configured headers for request that expect a JSON-formatted response body.- Returns:
- the request builder
-
newSignedForApiWithBodyRequestBuilder
public okhttp3.Request.Builder newSignedForApiWithBodyRequestBuilder()Creates a newRequest.Builderwith pre-configured headers for a request that contains both a JSON-formatted request and response body.- Returns:
- the request builder
-
execute
public <T> T execute(@NonNull okhttp3.Request request, @NonNull GsonParser<T> parser) throws OneDriveConnectionException Executes the givenRequestand parses the JSON-formatted response with givenGsonParser.- Type Parameters:
T- the POJO resource type- Parameters:
request- the requestparser- the parser to decode the response body- Returns:
- the response as a POJO resource type
- Throws:
OneDriveConnectionException- if an error occurred during the transaction
-
execute
Executes the givenRequestand returns the associated HTTP response code. This is used for transactions that do not expect a response in the body.- Parameters:
request- the request- Returns:
- the HTTP response code
- Throws:
OneDriveConnectionException- if an error occurred during the transaction
-
executeRemoteAsync
public String executeRemoteAsync(@NonNull okhttp3.Request request) throws OneDriveConnectionException Executes the givenRequestfor a remote asynchronous operation and returns the monitoring URL.- Parameters:
request- the request- Returns:
- the monitoring URL to track the remote asynchronous operation
- Throws:
OneDriveConnectionException- if an error occurred during the transaction
-
executeAsync
public <T> CompletableFuture<T> executeAsync(@NonNull okhttp3.Request request, @NonNull GsonParser<T> parser) Executes the givenRequestand parses the JSON-formatted response with givenGsonParser.- Type Parameters:
T- the POJO resource type- Parameters:
request- the requestparser- the parser to decode the response body- Returns:
- the CompletableFuture used to fetch the parsed response or failure exception reason
-
download
public long download(@NonNull okhttp3.Request request, @NonNull Path folderPath, String name, long sizeBytes, @NonNull TransferProgressCallback callback) throws OneDriveConnectionException Downloads the contents for the givenrequestto the specifiedfolderPathandname.- Parameters:
request- the requestfolderPath- the path of the folder to download the contents toname- the name of the file to download the contents tosizeBytes- the total size of the expected file in bytescallback- theTransferProgressCallbackcall to invoke to report download transfer progress- Returns:
- the size of the downloaded file in bytes
- Throws:
OneDriveConnectionException- if an error occurred while downloading the content for the request
-
downloadAsync
public CompletableFuture<Long> downloadAsync(@NonNull okhttp3.Request request, @NonNull Path folderPath, String name, long sizeBytes, @NonNull TransferProgressCallback callback) Downloads the contents for the givenrequestasynchronously to the specifiedfolderPathandname.- Parameters:
request- the requestfolderPath- the path of the folder to download the contents toname- the name of the file to download the contents tosizeBytes- the total size of the expected file in bytescallback- theTransferProgressCallbackcall to invoke to report download transfer progress- Returns:
- the CompletableFuture used to fetch the number of bytes downloaded
-
getAuthManager
The authorization manager used to manage auth tokens. -
getGson
public com.google.gson.Gson getGson()The configured GSON instance used for marshalling request and responses to/from JSON. -
getBaseUrl
The base URL for the Graph API.
-