Skip to content
This repository was archived by the owner on Dec 6, 2024. It is now read-only.

Commit 0a189b6

Browse files
SynapticloopSynapticloop
authored andcommitted
updated documentation, minor logging changes
1 parent 3aa1d24 commit 0a189b6

File tree

8 files changed

+168
-32
lines changed

8 files changed

+168
-32
lines changed

README.md

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
<img src="https://travis-ci.org/synapticloop/backblaze-b2-java-api.svg?branch=master" />
2+
13
A Java API for the truly excellent backblaze B2 storage
24

35
# Usage
@@ -67,7 +69,7 @@ And now for the dependency
6769
<dependency>
6870
<groupId>synapticloop</groupId>
6971
<artifactId>backblaze-b2-java-api</artifactId>
70-
<version>v1.0.0</version>
72+
<version>v1.0.2</version>
7173
<type>jar</type>
7274
</dependency>
7375

@@ -90,15 +92,15 @@ Repository
9092

9193
and then include the dependency:
9294

93-
runtime(group: 'synapticloop', name: 'backblaze-b2-java-api', version: 'v1.0.0', ext: 'jar')
95+
runtime(group: 'synapticloop', name: 'backblaze-b2-java-api', version: 'v1.0.2', ext: 'jar')
9496

95-
compile(group: 'synapticloop', name: 'backblaze-b2-java-api', version: 'v1.0.0', ext: 'jar')
97+
compile(group: 'synapticloop', name: 'backblaze-b2-java-api', version: 'v1.0.2', ext: 'jar')
9698

9799
or
98100

99-
runtime 'synapticloop:backblaze-b2-java-api:v1.0.0'
101+
runtime 'synapticloop:backblaze-b2-java-api:v1.0.2'
100102

101-
compile 'synapticloop:backblaze-b2-java-api:v1.0.0'
103+
compile 'synapticloop:backblaze-b2-java-api:v1.0.2'
102104

103105
## Other
104106

@@ -115,7 +117,15 @@ which can be found by searching here: [http://mvnrepository.com/](http://mvnrepo
115117

116118
# Running the Tests
117119

118-
`gradled --info test`
120+
`gradlew build`
121+
122+
this will output the artefacts into the `build/libs/` directory.
123+
124+
Note that this will also run all of the tests (see notes below)
125+
126+
# Running the Tests
127+
128+
`gradlew --info test`
119129

120130
Which will also print out the logging
121131

@@ -132,4 +142,5 @@ export B2_ACCOUNT_ID="your-account-id"
132142
export B2_APPLICATION_KEY="your-application-key"
133143
```
134144

135-
**WARNING:** These tests make API calls against your account which contribute to your call limits, which may lead to a cost.
145+
**WARNING:** These tests make API calls against your account which contribute to your call limits, which may lead to a cost.
146+

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ group = 'synapticloop'
1111
archivesBaseName = 'backblaze-b2-java-api'
1212
description = """An api for backblaze b2 storage in java"""
1313

14-
version = 'v1.0.1'
14+
version = 'v1.0.2'
1515

1616
description = """backblaze-b2-java-api"""
1717

src/main/java/synapticloop/b2/Action.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package synapticloop.b2;
22

33
/**
4-
* The action for an associated file.
4+
* The action for an associated file, either 'hide' or 'upload'
55
*
66
* @author synapticloop
77
*/

src/main/java/synapticloop/b2/B2ApiClient.java

Lines changed: 104 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public class B2ApiClient {
4444
private String accountId = null;
4545
private String applicationKey = null;
4646

47-
B2AuthorizeAccountResponse b2AuthorizeAccountResponse = null;
47+
private B2AuthorizeAccountResponse b2AuthorizeAccountResponse = null;
4848

4949
/**
5050
* Instantiate a new B2ApiClient object
@@ -225,12 +225,22 @@ public B2UploadFileRequest uploadFile(String bucketId, String fileName, File fil
225225
return(new B2UploadFileRequest(getB2AuthorizeAccountResponse(), b2GetUploadUrlResponse, fileName, file));
226226
}
227227

228+
/**
229+
* Delete a version of a file
230+
*
231+
* @param fileName The name of the file to delete
232+
* @param fileId The version of the file to delete
233+
*
234+
* @return the deleted file response
235+
*
236+
* @throws B2ApiException if there was an error deleting the file
237+
*/
228238
public B2DeleteFileVersionResponse deleteFileVersion(String fileName, String fileId) throws B2ApiException {
229239
return(new B2DeleteFileVersionRequest(getB2AuthorizeAccountResponse(), fileName, fileId).getResponse());
230240
}
231241

232242
/**
233-
* Update a busket to be a specified type
243+
* Update a bucket to be a specified type
234244
*
235245
* @param bucketId the id of the bucket to set
236246
* @param bucketType the type of the bucket
@@ -274,14 +284,46 @@ public B2ListFilesResponse listFileNames(String bucketId, String startFileName,
274284
return(new B2ListFileNamesRequest(getB2AuthorizeAccountResponse(), bucketId, startFileName, maxFileCount).getResponse());
275285
}
276286

287+
/**
288+
* List the files and versions for a specific bucket
289+
*
290+
* @param bucketId the id of the bucket to list
291+
*
292+
* @return the list file response
293+
*
294+
* @throws B2ApiException if there was an error with the call
295+
*/
277296
public B2ListFilesResponse listFileVersions(String bucketId) throws B2ApiException {
278297
return(new B2ListFileVersionsRequest(getB2AuthorizeAccountResponse(), bucketId).getResponse());
279298
}
280299

300+
/**
301+
* List the file versions in a bucket starting at a specific file name
302+
*
303+
* @param bucketId the id of the bucket
304+
* @param startFileName the file name to start with
305+
*
306+
* @return the list file response
307+
*
308+
* @throws B2ApiException if there was an error with the call
309+
*/
281310
public B2ListFilesResponse listFileVersions(String bucketId, String startFileName) throws B2ApiException {
282311
return(new B2ListFileVersionsRequest(getB2AuthorizeAccountResponse(), bucketId, startFileName, null, null).getResponse());
283312
}
284313

314+
/**
315+
* List the file versions in a bucket starting at a specific file name and file id
316+
*
317+
* @param bucketId the id of the bucket
318+
* @param startFileName the file name to start with
319+
* @param startFileId the id of the file to start with
320+
* @param maxFileCount the maximum number of files to return (must be less
321+
* than or equal to 1000, else an error is thrown)
322+
*
323+
* @return the list files response
324+
*
325+
* @throws B2ApiException if there was an error with the call
326+
*/
285327
public B2ListFilesResponse listFileVersions(String bucketId, String startFileName, String startFileId, Integer maxFileCount) throws B2ApiException {
286328
return(new B2ListFileVersionsRequest(getB2AuthorizeAccountResponse(), bucketId, startFileName, startFileId, maxFileCount).getResponse());
287329
}
@@ -369,27 +411,83 @@ public B2DownloadFileResponse downloadFileByName(String bucketName, String fileN
369411
return(new B2DownloadFileByNameRequest(getB2AuthorizeAccountResponse(), bucketName, fileName).getResponse());
370412
}
371413

372-
public void downloadFileByIdToFile(String bucketId, String FileId, File file) {
414+
public void downloadFileByIdToFile(String bucketId, String fileId, File file) throws B2ApiException {
415+
try {
416+
FileUtils.copyInputStreamToFile(new B2DownloadFileByIdRequest(getB2AuthorizeAccountResponse(), fileId).getResponse().getContent(), file);
417+
} catch (IOException ex) {
418+
throw new B2ApiException("Could not download to file", ex);
419+
}
373420
}
374421

375-
public byte[] downloadByFileIdToBytes(String bucketId, String fileId) {
376-
return(new byte[] {});
422+
/**
423+
* Download a file to a byte[]
424+
*
425+
* Note: This will not return any of the headers that accompanied the download.
426+
* See downloadFileByName to retrieve the complete response including sha1,
427+
* content length, content type and all headers.
428+
*
429+
* @param fileId the id of the file to download
430+
*
431+
* @return the array of bytes for the file
432+
*
433+
* @throws B2ApiException if there was an error with the call
434+
*/
435+
public byte[] downloadByFileIdToBytes(String fileId) throws B2ApiException {
436+
try {
437+
return(IOUtils.toByteArray(new B2DownloadFileByIdRequest(getB2AuthorizeAccountResponse(), fileId).getResponse().getContent()));
438+
} catch (IOException ex) {
439+
throw new B2ApiException("Could not download to bytes", ex);
440+
}
377441
}
378442

443+
/**
444+
* Download a file to an input stream
445+
*
446+
* Note: This will not return any of the headers that accompanied the download.
447+
* See downloadFileByName to retrieve the complete response including sha1,
448+
* content length, content type and all headers.
449+
*
450+
* @param fileId the id of the file to download
451+
*
452+
* @return the input stream for the file
453+
*
454+
* @throws B2ApiException if there was an error with the call
455+
*/
379456
public InputStream downloadFileByIdToStream(String fileId) throws B2ApiException {
380457
return(new B2DownloadFileByIdRequest(getB2AuthorizeAccountResponse(), fileId).getResponse().getContent());
381458
}
382459

460+
/**
461+
* Download a file
462+
*
463+
* @param fileId the id of the file to download
464+
*
465+
* @return the download file response
466+
*
467+
* @throws B2ApiException if there was an error with the call
468+
*/
383469
public B2DownloadFileResponse downloadFileById(String fileId) throws B2ApiException {
384470
return(new B2DownloadFileByIdRequest(getB2AuthorizeAccountResponse(), fileId).getResponse());
385471
}
386472

473+
/**
474+
* Perform a HEAD request on a file which will return the information
475+
* associated with it.
476+
*
477+
* @param fileId the id of the file to retrieve the information for
478+
*
479+
* @return the download file response
480+
*
481+
* @throws B2ApiException if there was an error with the call
482+
*/
483+
387484
public B2DownloadFileResponse headFileById(String fileId) throws B2ApiException {
388485
return(new B2HeadFileByIdRequest(getB2AuthorizeAccountResponse(), fileId).getResponse());
389486
}
390487

391488
/**
392-
* Return an authorize account response and cache it, or create a new one
489+
* return the authorize account response. This is only done once and is
490+
* cached for further use.
393491
*
394492
* @return the authorize account response
395493
* @throws B2ApiException if there was an error authenticating

src/main/java/synapticloop/b2/exception/B2ApiException.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ public String getCode() {
9898
}
9999

100100
/**
101-
* If the original message was in valid JSON format, return the 'message' part,
102-
* else the message for the exception.
101+
* If the original message was in valid JSON format, return the 'message'
102+
* part, else the message for the exception.
103103
*
104104
* @return the message
105105
*/
@@ -108,7 +108,8 @@ public String getMessage() {
108108
}
109109

110110
/**
111-
* Return the original message for the exception.
111+
* Return the original message for the exception, which is a JSON formatted
112+
* string
112113
*
113114
* @return the original message text
114115
*/

src/main/java/synapticloop/b2/package-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* This package contains the B2Api Client and enums
2+
* This package contains the B2 API Client and enums
33
*
44
* @author synapticloop
55
*

src/main/java/synapticloop/b2/request/BaseB2Request.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,10 @@ protected CloseableHttpResponse executeHead() throws B2ApiException {
108108
CloseableHttpResponse httpResponse = closeableHttpClient.execute(httpHead);
109109
int statusCode = httpResponse.getStatusLine().getStatusCode();
110110

111-
LOG.debug("Received status code of:{}, for HEAD request to url '{}'", statusCode, uri);
111+
LOG.debug("Received status code of: {}, for HEAD request to url '{}'", statusCode, uri);
112112

113113
if(statusCode != 200) {
114-
throw new B2ApiException("Received status code of " + statusCode + " for request.");
114+
throw new B2ApiException("Received non 'OK' status code of " + statusCode + " for request.");
115115
} else {
116116
return(httpResponse);
117117
}
@@ -120,6 +120,13 @@ protected CloseableHttpResponse executeHead() throws B2ApiException {
120120
}
121121
}
122122

123+
/**
124+
* Execute a GET request
125+
*
126+
* @return The response from the GET request
127+
*
128+
* @throws B2ApiException if there was an error with the request
129+
*/
123130
protected String executeGet() throws B2ApiException {
124131
CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
125132

@@ -136,7 +143,7 @@ protected String executeGet() throws B2ApiException {
136143
int statusCode = httpResponse.getStatusLine().getStatusCode();
137144
String response = EntityUtils.toString(httpResponse.getEntity());
138145

139-
LOG.debug("Received status code of:{}, for GET request to url '{}'", statusCode, url);
146+
LOG.debug("Received status code of: {}, for GET request to url '{}'", statusCode, url);
140147

141148
if(statusCode != 200) {
142149
throw new B2ApiException(response);
@@ -163,10 +170,10 @@ protected CloseableHttpResponse executeGetWithData() throws B2ApiException {
163170
CloseableHttpResponse httpResponse = closeableHttpClient.execute(httpGet);
164171
int statusCode = httpResponse.getStatusLine().getStatusCode();
165172

166-
LOG.debug("Received status code of:{}, for GET request to url '{}'", statusCode, url);
173+
LOG.debug("Received status code of: {}, for GET request to url '{}'", statusCode, url);
167174

168175
if(statusCode != 200) {
169-
throw new B2ApiException("Received status code of " + statusCode + " for request.");
176+
throw new B2ApiException("Received non 'OK' status code of " + statusCode + " for request.");
170177
} else {
171178
return(httpResponse);
172179
}
@@ -189,7 +196,7 @@ protected String executePost() throws B2ApiException {
189196
CloseableHttpResponse httpResponse = closeableHttpClient.execute(httpPost);
190197
int statusCode = httpResponse.getStatusLine().getStatusCode();
191198

192-
LOG.debug("Received status code of:{}, for POST request to url '{}'", statusCode, url);
199+
LOG.debug("Received status code of: {}, for POST request to url '{}'", statusCode, url);
193200

194201
String response = EntityUtils.toString(httpResponse.getEntity());
195202

@@ -222,7 +229,7 @@ protected String executePost(File file) throws B2ApiException {
222229
int statusCode = httpResponse.getStatusLine().getStatusCode();
223230
String response = EntityUtils.toString(httpResponse.getEntity());
224231

225-
LOG.debug("Received status code of:{}, for POST request to url '{}'", statusCode, url);
232+
LOG.debug("Received status code of: {}, for POST request to url '{}'", statusCode, url);
226233

227234
if(statusCode != 200) {
228235
throw new B2ApiException(response);

src/main/java/synapticloop/b2/util/Helper.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import synapticloop.b2.exception.B2ApiException;
1515

1616
public class Helper {
17+
private static final String UTF_8 = "UTF-8";
18+
1719
/**
1820
* Calculate and return the sha1 sum of a file
1921
*
@@ -48,21 +50,38 @@ public static String calculateSha1(File file) throws B2ApiException {
4850
}
4951
}
5052

51-
public static String urlEncode(String s) {
53+
/**
54+
* UTF-8 url encoding wrapper method. If there was an unsupported encoding
55+
* exception, return the un-encoded url
56+
*
57+
* @param url the URL to encode
58+
*
59+
* @return the encoded URL
60+
*/
61+
public static String urlEncode(String url) {
5262
try {
53-
return java.net.URLEncoder.encode(s, "UTF-8");
63+
return java.net.URLEncoder.encode(url, UTF_8);
5464
} catch (UnsupportedEncodingException ex) {
5565
// highly unlikely
56-
return(s);
66+
return(url);
5767
}
5868
}
5969

60-
public static String urlDecode(String s) {
70+
/**
71+
* UTF-8 url decoding wrapper method. If there was an unsupported encoding
72+
* exception, return the encoded url
73+
*
74+
* @param url the URL to decode
75+
*
76+
* @return the decoded URL
77+
*/
78+
79+
public static String urlDecode(String url) {
6180
try {
62-
return java.net.URLDecoder.decode(s, "UTF-8");
81+
return java.net.URLDecoder.decode(url, UTF_8);
6382
} catch (UnsupportedEncodingException ex) {
6483
// highly unlikey
65-
return(s);
84+
return(url);
6685
}
6786
}
6887

0 commit comments

Comments
 (0)