|
| 1 | +package io.apimatic.core.types.pagination; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import io.apimatic.core.types.CoreApiException; |
| 7 | +import io.apimatic.coreinterfaces.http.HttpHeaders; |
| 8 | +public class PageWrapper<I, P, E extends CoreApiException> { |
| 9 | + |
| 10 | + @SuppressWarnings("unchecked") |
| 11 | + public static <I, P, E extends CoreApiException> PageWrapper<I, P, E> CreateError( |
| 12 | + Exception exception) { |
| 13 | + if (exception instanceof CoreApiException) { |
| 14 | + return new PageWrapper<I, P, E>((E) exception); |
| 15 | + } |
| 16 | + |
| 17 | + if (exception instanceof IOException) { |
| 18 | + return new PageWrapper<I, P, E>((IOException) exception); |
| 19 | + } |
| 20 | + |
| 21 | + return null; |
| 22 | + } |
| 23 | + |
| 24 | + private final int statusCode; |
| 25 | + private final HttpHeaders headers; |
| 26 | + private P result; |
| 27 | + private List<I> items; |
| 28 | + private E apiException = null; |
| 29 | + private IOException ioException = null; |
| 30 | + |
| 31 | + private String nextLinkInput = null; |
| 32 | + private int offsetInput = -1; |
| 33 | + private int pageInput = -1; |
| 34 | + private String cursorInput = null; |
| 35 | + |
| 36 | + public PageWrapper(int statusCode, HttpHeaders headers, P result, List<I> items) { |
| 37 | + this.statusCode = statusCode; |
| 38 | + this.headers = headers; |
| 39 | + this.result = result; |
| 40 | + this.items = items; |
| 41 | + } |
| 42 | + |
| 43 | + private PageWrapper(E apiException) { |
| 44 | + this.statusCode = apiException.getResponseCode(); |
| 45 | + this.headers = apiException.getHttpContext().getResponse().getHeaders(); |
| 46 | + this.apiException = apiException; |
| 47 | + } |
| 48 | + |
| 49 | + private PageWrapper(IOException ioException) { |
| 50 | + this.statusCode = 0; |
| 51 | + this.headers = null; |
| 52 | + this.ioException = ioException; |
| 53 | + } |
| 54 | + |
| 55 | + public void setNextLinkInput(String nextLinkInput) { |
| 56 | + this.nextLinkInput = nextLinkInput; |
| 57 | + } |
| 58 | + |
| 59 | + public void setOffsetInput(int offsetInput) { |
| 60 | + this.offsetInput = offsetInput; |
| 61 | + } |
| 62 | + |
| 63 | + public void setPageInput(int pageInput) { |
| 64 | + this.pageInput = pageInput; |
| 65 | + } |
| 66 | + |
| 67 | + public void setCursorInput(String cursorInput) { |
| 68 | + this.cursorInput = cursorInput; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Gets the next link input used for link-based pagination. |
| 73 | + * @return The next page link |
| 74 | + */ |
| 75 | + public String getNextLinkInput() { |
| 76 | + return nextLinkInput; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Gets the offset input used for offset-based pagination. |
| 81 | + * @return The offset value |
| 82 | + */ |
| 83 | + public int getOffsetInput() { |
| 84 | + return offsetInput; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Gets the page number input used for page-based pagination. |
| 89 | + * @return The page number |
| 90 | + */ |
| 91 | + public int getPageInput() { |
| 92 | + return pageInput; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Gets the cursor input used for cursor-based pagination. |
| 97 | + * @return The cursor token |
| 98 | + */ |
| 99 | + public String getCursorInput() { |
| 100 | + return cursorInput; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * HTTP Status code of the api response. |
| 105 | + * @return Int status code |
| 106 | + */ |
| 107 | + public int getStatusCode() { |
| 108 | + return statusCode; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Headers of the http response. |
| 113 | + * @return Headers |
| 114 | + */ |
| 115 | + public HttpHeaders getHeaders() { |
| 116 | + return headers; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Content of the page. |
| 121 | + * @return List of items on this page. |
| 122 | + * @throws E |
| 123 | + * @throws IOException |
| 124 | + */ |
| 125 | + public List<I> getItems() throws E, IOException { |
| 126 | + if (ioException != null) { |
| 127 | + throw ioException; |
| 128 | + } |
| 129 | + |
| 130 | + if (apiException != null) { |
| 131 | + throw apiException; |
| 132 | + } |
| 133 | + |
| 134 | + return items; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Content of the page. |
| 139 | + * @return Content |
| 140 | + * @throws E |
| 141 | + * @throws IOException |
| 142 | + */ |
| 143 | + public P getResult() throws E, IOException { |
| 144 | + if (ioException != null) { |
| 145 | + throw ioException; |
| 146 | + } |
| 147 | + |
| 148 | + if (apiException != null) { |
| 149 | + throw apiException; |
| 150 | + } |
| 151 | + |
| 152 | + return result; |
| 153 | + } |
| 154 | +} |
0 commit comments