|
12 | 12 |
|
13 | 13 | /** |
14 | 14 | * Parses Admin API responses tolerantly: entries that are not objects or have no key_id are |
15 | | - * skipped, missing optional fields (labels, usage limits, dates) are null, and other missing or |
16 | | - * malformed fields get neutral defaults. A response of the wrong overall shape, or a single key |
17 | | - * without key_id, is an error. |
| 15 | + * skipped, missing optional fields (labels, usage limits, dates, a report's total usage and |
| 16 | + * speech-to-speech minutes) are null, and other missing or malformed fields get neutral defaults. A |
| 17 | + * response of the wrong overall shape, or a single key without key_id, is an error. |
18 | 18 | * |
19 | 19 | * <p>This class is internal; you should not use this class directly. |
20 | 20 | */ |
@@ -84,7 +84,7 @@ private static JsonObject requireObject(@Nullable JsonElement json, String what) |
84 | 84 | AdminUsageReport parseUsageReport(JsonElement json) { |
85 | 85 | JsonObject report = asObject(requireObject(json, "Usage report").get("usage_report")); |
86 | 86 | return new AdminUsageReport( |
87 | | - parseBreakdown(report.get("total_usage")), |
| 87 | + breakdownOrNull(report.get("total_usage")), |
88 | 88 | utcDay(report, "start_date"), |
89 | 89 | utcDay(report, "end_date"), |
90 | 90 | stringOrNull(report, "group_by"), |
@@ -135,7 +135,11 @@ private AdminUsageBreakdown parseBreakdown(@Nullable JsonElement json) { |
135 | 135 | count(object, "document_translation_characters"), |
136 | 136 | count(object, "text_improvement_characters"), |
137 | 137 | number(object, "speech_to_text_minutes"), |
138 | | - number(object, "speech_to_speech_minutes")); |
| 138 | + numberOrNull(object, "speech_to_speech_minutes")); |
| 139 | + } |
| 140 | + |
| 141 | + private @Nullable AdminUsageBreakdown breakdownOrNull(@Nullable JsonElement json) { |
| 142 | + return json != null && json.isJsonObject() ? parseBreakdown(json) : null; |
139 | 143 | } |
140 | 144 |
|
141 | 145 | private CustomTagUsageBreakdown parseCustomTagBreakdown(@Nullable JsonElement json) { |
@@ -199,11 +203,16 @@ private static long count(JsonObject object, String name) { |
199 | 203 | } |
200 | 204 |
|
201 | 205 | private static double number(JsonObject object, String name) { |
| 206 | + Double value = numberOrNull(object, name); |
| 207 | + return value != null ? value : 0; |
| 208 | + } |
| 209 | + |
| 210 | + private static @Nullable Double numberOrNull(JsonObject object, String name) { |
202 | 211 | JsonElement element = object.get(name); |
203 | 212 | try { |
204 | | - return element == null || element.isJsonNull() ? 0 : element.getAsDouble(); |
| 213 | + return element == null || element.isJsonNull() ? null : element.getAsDouble(); |
205 | 214 | } catch (RuntimeException e) { |
206 | | - return 0; |
| 215 | + return null; |
207 | 216 | } |
208 | 217 | } |
209 | 218 |
|
|
0 commit comments