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

Commit e29760f

Browse files
SynapticloopSynapticloop
authored andcommitted
Merge branch 'pr/16'
2 parents 8e86566 + 765a678 commit e29760f

37 files changed

+260
-386
lines changed

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

Lines changed: 49 additions & 81 deletions
Large diffs are not rendered by default.

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

Lines changed: 23 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,8 @@ public class B2ApiException extends Exception {
2525
private String code;
2626
private String message;
2727
private int status = -1;
28-
private String originalMessage;
2928

30-
/**
31-
* Create a new B2Api Exception
32-
*/
33-
public B2ApiException() {
34-
super();
35-
}
29+
private final String json;
3630

3731
/**
3832
* Create a new B2 Exception with a message. If the message is in JSON (as
@@ -45,11 +39,12 @@ public B2ApiException() {
4539
* <li>message - A human-readable message, in English, saying what went wrong.</li>
4640
* </ul>
4741
*
48-
* @param message The message of the exception
42+
* @param json The message of the exception
4943
*/
50-
public B2ApiException(String message) {
51-
super(message);
52-
parseMessage(message);
44+
public B2ApiException(String json) {
45+
super(json);
46+
this.json = json;
47+
this.parse(json);
5348
}
5449

5550
/**
@@ -63,44 +58,28 @@ public B2ApiException(String message) {
6358
* <li>message - A human-readable message, in English, saying what went wrong.</li>
6459
* </ul>
6560
*
66-
* @param message The message of the exception
61+
* @param json The message of the exception
6762
* @param cause the root cause of the exception
6863
*/
69-
public B2ApiException(String message, Throwable cause) {
70-
super(message, cause);
71-
parseMessage(message);
72-
}
73-
74-
/**
75-
* Create a new B2Api Exception with a root cause
76-
*
77-
* @param cause The root cause of the exception
78-
*/
79-
public B2ApiException(Throwable cause) {
80-
super(cause);
64+
public B2ApiException(String json, Throwable cause) {
65+
super(json, cause);
66+
this.json = json;
67+
this.parse(json);
8168
}
8269

83-
private void parseMessage(String json) {
84-
this.originalMessage = json;
85-
86-
if(null == json) {
87-
return;
88-
}
8970

90-
try {
91-
JSONObject jsonObject = new JSONObject(json);
71+
private void parse(String json) {
72+
if (null != json) {
73+
try {
74+
JSONObject jsonObject = new JSONObject(json);
75+
this.message = jsonObject.optString("message", null);
76+
this.status = jsonObject.optInt("status", -1);
77+
this.code = jsonObject.optString("code", null);
9278

93-
String tempMessage = jsonObject.getString("message");
94-
if(null != tempMessage) {
95-
this.message = tempMessage;
79+
} catch (JSONException ex) {
80+
// Ignore
81+
this.message = json;
9682
}
97-
98-
this.status = jsonObject.optInt("status");
99-
this.code = jsonObject.optString("code", null);
100-
101-
} catch (JSONException ex) {
102-
this.code = "not_json";
103-
this.message = json;
10483
}
10584
}
10685

@@ -129,8 +108,8 @@ public String getMessage() {
129108
*
130109
* @return the original message text
131110
*/
132-
public String getOriginalMessage() {
133-
return this.originalMessage;
111+
public String getJson() {
112+
return this.json;
134113
}
135114

136115
/**

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,8 @@ public B2AuthorizeAccountRequest(CloseableHttpClient client, String accountId,
7272
*
7373
* @throws B2ApiException if there was an error with the call
7474
*/
75-
public B2AuthorizeAccountResponse getResponse() throws B2ApiException {
75+
public B2AuthorizeAccountResponse getResponse() throws B2ApiException, IOException {
7676
final CloseableHttpResponse httpResponse = executeGet();
77-
try {
78-
return(new B2AuthorizeAccountResponse(EntityUtils.toString(httpResponse.getEntity())));
79-
} catch(IOException e) {
80-
throw new B2ApiException(e);
81-
}
77+
return new B2AuthorizeAccountResponse(EntityUtils.toString(httpResponse.getEntity()));
8278
}
8379
}

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,7 @@ public B2CreateBucketRequest(CloseableHttpClient client, B2AuthorizeAccountRespo
7272
*
7373
* @throws B2ApiException if there was an error with the call
7474
*/
75-
public B2BucketResponse getResponse() throws B2ApiException {
76-
try {
77-
return(new B2BucketResponse(EntityUtils.toString(executePost().getEntity())));
78-
} catch(IOException e) {
79-
throw new B2ApiException(e);
80-
}
75+
public B2BucketResponse getResponse() throws B2ApiException, IOException {
76+
return new B2BucketResponse(EntityUtils.toString(executePost().getEntity()));
8177
}
8278
}

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,7 @@ public B2DeleteBucketRequest(CloseableHttpClient client, B2AuthorizeAccountRespo
6161
* @throws B2ApiException if there was an error with the call, or if you are
6262
* trying to delete a bucket which is not empty
6363
*/
64-
public B2BucketResponse getResponse() throws B2ApiException {
65-
try {
66-
return(new B2BucketResponse(EntityUtils.toString(executePost().getEntity())));
67-
} catch(IOException e) {
68-
throw new B2ApiException(e);
69-
}
64+
public B2BucketResponse getResponse() throws B2ApiException, IOException {
65+
return new B2BucketResponse(EntityUtils.toString(executePost().getEntity()));
7066
}
7167
}

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,7 @@ public B2DeleteFileVersionRequest(CloseableHttpClient client, B2AuthorizeAccount
6666
*
6767
* @throws B2ApiException if there was an error with the call
6868
*/
69-
public B2DeleteFileVersionResponse getResponse() throws B2ApiException {
70-
try {
71-
return(new B2DeleteFileVersionResponse(EntityUtils.toString(executePost().getEntity())));
72-
} catch(IOException e) {
73-
throw new B2ApiException(e);
74-
}
69+
public B2DeleteFileVersionResponse getResponse() throws B2ApiException, IOException {
70+
return new B2DeleteFileVersionResponse(EntityUtils.toString(executePost().getEntity()));
7571
}
7672
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import synapticloop.b2.response.B2AuthorizeAccountResponse;
2424
import synapticloop.b2.response.B2DownloadFileResponse;
2525

26+
import java.io.IOException;
27+
2628
/**
2729
* <p>Downloads one file from B2.</p>
2830
*
@@ -112,7 +114,7 @@ public B2DownloadFileByIdRequest(CloseableHttpClient client, B2AuthorizeAccountR
112114
* @return The download file response
113115
* @throws B2ApiException If there was an error with the call
114116
*/
115-
public B2DownloadFileResponse getResponse() throws B2ApiException {
116-
return (new B2DownloadFileResponse(executeGet()));
117+
public B2DownloadFileResponse getResponse() throws B2ApiException, IOException {
118+
return new B2DownloadFileResponse(executeGet());
117119
}
118120
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import synapticloop.b2.response.B2DownloadFileResponse;
2525
import synapticloop.b2.util.URLEncoder;
2626

27+
import java.io.IOException;
28+
2729
/**
2830
* <p>Downloads one file by providing the name of the bucket and the name of the file.</p>
2931
*
@@ -91,7 +93,7 @@ public B2DownloadFileByNameRequest(CloseableHttpClient client, B2AuthorizeAccoun
9193
*
9294
* @throws B2ApiException If there was an error with the call
9395
*/
94-
public B2DownloadFileResponse getResponse() throws B2ApiException {
95-
return(new B2DownloadFileResponse(executeGet()));
96+
public B2DownloadFileResponse getResponse() throws B2ApiException, IOException {
97+
return new B2DownloadFileResponse(executeGet());
9698
}
9799
}

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,7 @@ public B2GetFileInfoRequest(CloseableHttpClient client, B2AuthorizeAccountRespon
6262
*
6363
* @throws B2ApiException if there was an error with the call
6464
*/
65-
public B2FileResponse getResponse() throws B2ApiException {
66-
try {
67-
return(new B2FileResponse(EntityUtils.toString(executePost().getEntity())));
68-
} catch(IOException e) {
69-
throw new B2ApiException(e);
70-
}
65+
public B2FileResponse getResponse() throws B2ApiException, IOException {
66+
return new B2FileResponse(EntityUtils.toString(executePost().getEntity()));
7167
}
7268
}

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,7 @@ public B2GetUploadUrlRequest(CloseableHttpClient client, B2AuthorizeAccountRespo
6161
*
6262
* @throws B2ApiException if something went wrong
6363
*/
64-
public B2GetUploadUrlResponse getResponse() throws B2ApiException {
65-
try {
66-
return(new B2GetUploadUrlResponse(EntityUtils.toString(executePost().getEntity())));
67-
} catch(IOException e) {
68-
throw new B2ApiException(e);
69-
}
64+
public B2GetUploadUrlResponse getResponse() throws B2ApiException, IOException {
65+
return new B2GetUploadUrlResponse(EntityUtils.toString(executePost().getEntity()));
7066
}
7167
}

0 commit comments

Comments
 (0)