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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
package org.fox.ttrss;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.fox.ttrss.ApiCommon.ApiError;
import java.util.Objects;
/**
* Parses TT-RSS API v2 responses and maps error codes to ApiError enum.
*
* This is a pure-logic class with no Android dependencies, making it
* easy to unit test. ApiV2Client delegates to it after receiving HTTP responses.
*/
public final class ApiResponseParser {
/**
* Result of parsing a v2 API response.
*/
public static final class ParsedResponse {
public final boolean success;
public final JsonElement data;
public final ApiError error;
public final String errorMessage;
public ParsedResponse(boolean success, JsonElement data, ApiError error, String errorMessage) {
this.success = success;
this.data = data;
this.error = error;
this.errorMessage = errorMessage;
}
}
private ApiResponseParser() {
// no instances
}
/**
* Parse a v2 API response payload.
*
* Response format: {"success": true/false, "data": ..., "error": {"code": "..."}}
*
* @param payload raw JSON string from the HTTP response body
* @return parsed response with success flag, data, and error info
*/
public static ParsedResponse parse(String payload) {
try {
JsonElement result = JsonParser.parseString(payload);
JsonObject resultObj = result.getAsJsonObject();
boolean success = resultObj.get("success").getAsBoolean();
if (success) {
JsonElement data = resultObj.get("data");
return new ParsedResponse(
true,
data != null ? data : JsonParser.parseString("null"),
ApiError.SUCCESS,
null
);
} else {
JsonObject errorObj = resultObj.getAsJsonObject("error");
String errorCode = errorObj != null && errorObj.has("code")
? errorObj.get("code").getAsString()
: "UNKNOWN";
return new ParsedResponse(
false,
null,
mapApiError(errorCode),
errorCode
);
}
} catch (Exception e) {
return new ParsedResponse(
false,
null,
ApiError.PARSE_ERROR,
"Failed to parse v2 response: " + e.getMessage()
);
}
}
/**
* Map v2 API error codes to the common ApiError enum.
*
* @param errorCode error code from the v2 API response
* @return corresponding ApiError
*/
public static ApiError mapApiError(String errorCode) {
switch (Objects.requireNonNull(errorCode)) {
case "LOGIN_ERROR":
case "NOT_LOGGED_IN":
return ApiError.LOGIN_FAILED;
case "API_DISABLED":
return ApiError.API_DISABLED;
case "INCORRECT_USAGE":
return ApiError.API_INCORRECT_USAGE;
default:
return ApiError.API_UNKNOWN;
}
}
/**
* Map HTTP status codes to the common ApiError enum.
*
* @param httpCode raw HTTP response status code
* @return corresponding ApiError
*/
public static ApiError mapHttpStatusError(int httpCode) {
switch (httpCode) {
case 400:
return ApiError.HTTP_BAD_REQUEST;
case 401:
return ApiError.HTTP_UNAUTHORIZED;
case 403:
return ApiError.HTTP_FORBIDDEN;
case 404:
return ApiError.HTTP_NOT_FOUND;
case 405:
return ApiError.API_UNKNOWN_METHOD;
case 500:
case 501:
return ApiError.HTTP_SERVER_ERROR;
default:
return ApiError.HTTP_OTHER_ERROR;
}
}
}
|