Skip to content
This repository was archived by the owner on Dec 6, 2024. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 37 additions & 35 deletions src/main/java/synapticloop/b2/request/BaseB2Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

/*
* Copyright (c) 2016 Synapticloop.
*
*
* All rights reserved.
*
* This code may contain contributions from other parties which, where
* applicable, will be listed in the default build file for the project
*
* This code may contain contributions from other parties which, where
* applicable, will be listed in the default build file for the project
* ~and/or~ in a file named CONTRIBUTORS.txt in the root of the project.
*
* This source code and any derived binaries are covered by the terms and
* conditions of the Licence agreement ("the Licence"). You may not use this
* source code or any derived binaries except in compliance with the Licence.
* A copy of the Licence is available in the file named LICENSE.txt shipped with
*
* This source code and any derived binaries are covered by the terms and
* conditions of the Licence agreement ("the Licence"). You may not use this
* source code or any derived binaries except in compliance with the Licence.
* A copy of the Licence is available in the file named LICENSE.txt shipped with
* this source code or binaries.
*/

Expand Down Expand Up @@ -44,6 +44,8 @@
import synapticloop.b2.exception.B2ApiException;
import synapticloop.b2.response.B2AuthorizeAccountResponse;

import static org.apache.http.entity.ContentType.APPLICATION_JSON;

public abstract class BaseB2Request {
private static final Logger LOGGER = LoggerFactory.getLogger(BaseB2Request.class);

Expand Down Expand Up @@ -85,7 +87,7 @@ protected BaseB2Request(CloseableHttpClient client, final String url) {
/**
* Instantiate the base B2 request which adds headers with the authorization
* token.
*
*
* @param client Shared HTTP client
* @param b2AuthorizeAccountResponse the authorize account response
* @param url Fully qualified request URI
Expand All @@ -97,7 +99,7 @@ protected BaseB2Request(CloseableHttpClient client, B2AuthorizeAccountResponse b
/**
* Instantiate the base B2 request which adds headers with the authorization
* token.
*
*
* @param client Shared HTTP client
* @param b2AuthorizeAccountResponse the authorize account response
* @param url Fully qualified request URI
Expand All @@ -113,7 +115,7 @@ protected BaseB2Request(CloseableHttpClient client, B2AuthorizeAccountResponse b

/**
* Add header to request replacing previous if any
*
*
* @param key the key to add
* @param value the value to add
*/
Expand All @@ -123,7 +125,7 @@ protected void addHeader(String key, String value) {

/**
* Add query parameter to request replacing previous if any
*
*
* @param key the key to add
* @param value the value to add
*/
Expand All @@ -133,7 +135,7 @@ protected void addParameter(String key, String value) {

/**
* Add property to JSON request body
*
*
* @param key the key to add
* @param value the value to add
*/
Expand All @@ -143,9 +145,9 @@ protected void addProperty(String key, Object value) {

/**
* Execute an HTTP HEAD request and return the response for further parsing
*
*
* @return the response object
*
*
* @throws B2ApiException if something went wrong with the call
* @throws IOException if there was an error communicating with the API service
*/
Expand Down Expand Up @@ -173,7 +175,7 @@ protected CloseableHttpResponse executeHead() throws B2ApiException, IOException

/**
* Execute a GET request, returning the data stream from the response.
*
*
* @return The response from the GET request
*
* @throws B2ApiException if there was an error with the request
Expand Down Expand Up @@ -203,9 +205,9 @@ protected CloseableHttpResponse executeGet() throws B2ApiException, IOException

/**
* Execute a POST request returning the response data as a String
*
*
* @return the response data as a string
*
*
* @throws B2ApiException if there was an error with the call, most notably
* a non OK status code (i.e. not 200)
* @throws IOException if there was an error communicating with the API service
Expand All @@ -216,7 +218,7 @@ protected CloseableHttpResponse executePost() throws B2ApiException, IOException
String postData = convertPostData();
HttpPost httpPost = new HttpPost(uri);

httpPost.setEntity(new StringEntity(postData));
httpPost.setEntity(new StringEntity(postData, APPLICATION_JSON));
CloseableHttpResponse httpResponse = this.execute(httpPost);

switch(httpResponse.getStatusLine().getStatusCode()) {
Expand All @@ -234,11 +236,11 @@ protected CloseableHttpResponse executePost() throws B2ApiException, IOException

/**
* Execute a POST request with the contents of a file.
*
*
* @param entity Content to write
*
*
* @return the string representation of the response
*
*
* @throws B2ApiException if there was an error with the call, most notably
* a non OK status code (i.e. not 200)
* @throws IOException if there was an error communicating with the API service
Expand All @@ -263,9 +265,9 @@ protected CloseableHttpResponse executePost(HttpEntity entity) throws B2ApiExcep
/**
* Convert the stringData and integerData Maps to JSON format, to be included
* in the POST body of the request.
*
*
* @return the JSON string of the data
*
*
* @throws IOException if there was an error converting the data.
*/
protected String convertPostData() throws IOException {
Expand All @@ -283,11 +285,11 @@ protected String convertPostData() throws IOException {
}

/**
* Return the URI for this request, which adds any parameters found in the
* Return the URI for this request, which adds any parameters found in the
* 'parameters' data structure
*
*
* @return The URI for this request, with properly encoded parameters
*
*
* @throws IOException If there was an error building the URI
*/
protected URI buildUri() throws IOException {
Expand All @@ -306,15 +308,15 @@ protected URI buildUri() throws IOException {
}

/**
* Set the headers safely, go through the headers Map and add them to the http
* request with properly encode values. If they already exist on the http
* Set the headers safely, go through the headers Map and add them to the http
* request with properly encode values. If they already exist on the http
* request, it will be ignored.
*
*
* To override what headers are set, this should be done in the constructor
* of the base request object.
*
*
* @param request The HTTP request to set the headers on
*
*
* @throws B2ApiException if there was an error setting the headers
*/
protected void setHeaders(HttpUriRequest request) throws B2ApiException {
Expand Down Expand Up @@ -342,9 +344,9 @@ protected CloseableHttpResponse execute(final HttpUriRequest request) throws IOE
/**
* Obfuscate the data by removing the accountId and replacing it with the
* string "[redacted]"
*
*
* @param data the data to obfuscate
*
*
* @return the obfuscated data
*/
private Object obfuscateData(String key, Object data) {
Expand Down