| 51 | } |
| 52 | |
| 53 | @Nullable |
| 54 | public static <T> T get(HttpResponse response, Type typeOfT) { |
| 55 | try (Reader reader = reader(response); |
| 56 | JsonInput input = JSON.newInput(reader)) { |
| 57 | |
| 58 | // Alright then. We might be dealing with the object we expected, or we might have an |
| 59 | // error. We shall assume that a non-200 http status code indicates that something is |
| 60 | // wrong. |
| 61 | if (response.getStatus() != 200) { |
| 62 | throw ERRORS.decode(JSON.toType(string(response), MAP_TYPE)); |
| 63 | } |
| 64 | |
| 65 | if (Void.class.equals(typeOfT) && input.peek() == END) { |
| 66 | return null; |
| 67 | } |
| 68 | |
| 69 | input.beginObject(); |
| 70 | |
| 71 | while (input.hasNext()) { |
| 72 | if ("value".equals(input.nextName())) { |
| 73 | return input.read(typeOfT); |
| 74 | } else { |
| 75 | input.skipValue(); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | throw new IllegalStateException("Unable to locate value: " + response.contentAsString()); |
| 80 | } catch (IOException e) { |
| 81 | throw new UncheckedIOException(e); |
| 82 | } |
| 83 | } |
| 84 | } |