Class OneDriveConnection

java.lang.Object
com.amilesend.onedrive.connection.OneDriveConnection

public class OneDriveConnection extends Object
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
    Modifier and Type
    Field
    Description
    static final String
     
    static final okhttp3.MediaType
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    OneDriveConnection(@NonNull okhttp3.OkHttpClient httpClient, @NonNull AuthManager authManager, @NonNull GsonFactory gsonFactory, String baseUrl)
    Creates a new OneDriveConnection object.
  • Method Summary

    Modifier and Type
    Method
    Description
    long
    download(@NonNull okhttp3.Request request, @NonNull Path folderPath, String name, long sizeBytes, @NonNull TransferProgressCallback callback)
    Downloads the contents for the given request to the specified folderPath and name.
    downloadAsync(@NonNull okhttp3.Request request, @NonNull Path folderPath, String name, long sizeBytes, @NonNull TransferProgressCallback callback)
    Downloads the contents for the given request asynchronously to the specified folderPath and name.
    int
    execute(@NonNull okhttp3.Request request)
    Executes the given Request and returns the associated HTTP response code.
    <T> T
    execute(@NonNull okhttp3.Request request, @NonNull GsonParser<T> parser)
    Executes the given Request and parses the JSON-formatted response with given GsonParser.
    executeAsync(@NonNull okhttp3.Request request, @NonNull GsonParser<T> parser)
    Executes the given Request and parses the JSON-formatted response with given GsonParser.
    executeRemoteAsync(@NonNull okhttp3.Request request)
    Executes the given Request for 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.Gson
    The configured GSON instance used for marshalling request and responses to/from JSON.
    okhttp3.Request.Builder
    Creates a new Request.Builder with pre-configured headers for request that expect a JSON-formatted response body.
    okhttp3.Request.Builder
    Creates a new Request.Builder with pre-configured headers for a request that contains both a JSON-formatted request and response body.
    okhttp3.Request.Builder
    Creates a new Request.Builder with pre-configured headers for requests that expect no responses in the body (e.g., post or upload/download operations).

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • JSON_CONTENT_TYPE

      public static final String 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 new OneDriveConnection object. It is recommended to use the OneDriveConnectionBuilder instead.
      Parameters:
      httpClient - the configured HTTP client
      authManager - the authorization manager used to manage auth and refresh tokens
      gsonFactory - the factory used to vend configured GSON instances for request/reply serialization
      baseUrl - the base URL to use for Graph API invocations
      See Also:
  • Method Details

    • newSignedForRequestBuilder

      public okhttp3.Request.Builder newSignedForRequestBuilder()
      Creates a new Request.Builder with 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 new Request.Builder with pre-configured headers for request that expect a JSON-formatted response body.
      Returns:
      the request builder
    • newSignedForApiWithBodyRequestBuilder

      public okhttp3.Request.Builder newSignedForApiWithBodyRequestBuilder()
      Creates a new Request.Builder with 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 given Request and parses the JSON-formatted response with given GsonParser.
      Type Parameters:
      T - the POJO resource type
      Parameters:
      request - the request
      parser - 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

      public int execute(@NonNull okhttp3.Request request) throws OneDriveConnectionException
      Executes the given Request and 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 given Request for 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 given Request and parses the JSON-formatted response with given GsonParser.
      Type Parameters:
      T - the POJO resource type
      Parameters:
      request - the request
      parser - 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 given request to the specified folderPath and name.
      Parameters:
      request - the request
      folderPath - the path of the folder to download the contents to
      name - the name of the file to download the contents to
      sizeBytes - the total size of the expected file in bytes
      callback - the TransferProgressCallback call 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 given request asynchronously to the specified folderPath and name.
      Parameters:
      request - the request
      folderPath - the path of the folder to download the contents to
      name - the name of the file to download the contents to
      sizeBytes - the total size of the expected file in bytes
      callback - the TransferProgressCallback call to invoke to report download transfer progress
      Returns:
      the CompletableFuture used to fetch the number of bytes downloaded
    • getAuthManager

      public AuthManager 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

      public String getBaseUrl()
      The base URL for the Graph API.