-
Notifications
You must be signed in to change notification settings - Fork 0
/
ApiResponse.java
51 lines (42 loc) · 1.33 KB
/
ApiResponse.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package com.gauravbhola.flickry.util;
import com.gauravbhola.flickry.data.remote.GetRecentResponse;
import androidx.annotation.Nullable;
import java.io.IOException;
import retrofit2.Response;
/**
* Common class used by API responses (GetRecentPhotos in our case).
* It represents the result of an api response, httpCode, body and errorMessage received
*/
public class ApiResponse {
public final int httpCode;
@Nullable
public GetRecentResponse body;
@Nullable
public String errorMessage;
public ApiResponse(Throwable error) {
httpCode = 500;
body = null;
errorMessage = error.getMessage();
if (error instanceof IOException) {
errorMessage = "Oops! We can't reach Flickr";
}
}
public ApiResponse(Response<GetRecentResponse> response) {
httpCode = response.code();
if(response.isSuccessful()) {
body = response.body();
if (body.getStat().equals("fail")) {
errorMessage = body.getMessage();
body = null;
} else {
errorMessage = null;
}
} else {
errorMessage = "Oops! We can't reach Flickr";
body = null;
}
}
public boolean isSuccessful() {
return httpCode >= 200 && httpCode < 300 && body != null;
}
}