|
1 | | -# icepanel-java |
| 1 | +# IcePanel Java Library |
| 2 | + |
| 3 | +[](https://buildwithfern.com?utm_source=github&utm_medium=github&utm_campaign=readme&utm_source=https%3A%2F%2Fgithub.com%2FIcePanel%2Ficepanel-java) |
| 4 | +[](https://central.sonatype.com/artifact/com.icepanel/sdk) |
| 5 | + |
| 6 | +The IcePanel Java library provides convenient access to the IcePanel APIs from Java. |
| 7 | + |
| 8 | +## Table of Contents |
| 9 | + |
| 10 | +- [Installation](#installation) |
| 11 | +- [Reference](#reference) |
| 12 | +- [Environments](#environments) |
| 13 | +- [Base Url](#base-url) |
| 14 | +- [Exception Handling](#exception-handling) |
| 15 | +- [Advanced](#advanced) |
| 16 | + - [Custom Client](#custom-client) |
| 17 | + - [Retries](#retries) |
| 18 | + - [Timeouts](#timeouts) |
| 19 | + - [Custom Headers](#custom-headers) |
| 20 | + - [Access Raw Response Data](#access-raw-response-data) |
| 21 | +- [Contributing](#contributing) |
| 22 | + |
| 23 | +## Installation |
| 24 | + |
| 25 | +### Gradle |
| 26 | + |
| 27 | +Add the dependency in your `build.gradle` file: |
| 28 | + |
| 29 | +```groovy |
| 30 | +dependencies { |
| 31 | + implementation 'com.icepanel:sdk' |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +### Maven |
| 36 | + |
| 37 | +Add the dependency in your `pom.xml` file: |
| 38 | + |
| 39 | +```xml |
| 40 | +<dependency> |
| 41 | + <groupId>com.icepanel</groupId> |
| 42 | + <artifactId>sdk</artifactId> |
| 43 | + <version>0.0.440</version> |
| 44 | +</dependency> |
| 45 | +``` |
| 46 | + |
| 47 | +## Reference |
| 48 | + |
| 49 | +A full reference for this library is available [here](https://github.com/IcePanel/icepanel-java/blob/HEAD/./reference.md). |
| 50 | + |
| 51 | +## Environments |
| 52 | + |
| 53 | +This SDK allows you to configure different environments for API requests. |
| 54 | + |
| 55 | +```java |
| 56 | +import com.icepanel.api.IcePanelClient; |
| 57 | +import com.icepanel.api.core.Environment; |
| 58 | + |
| 59 | +IcePanelClient client = IcePanelClient |
| 60 | + .builder() |
| 61 | + .environment(Environment.v1) |
| 62 | + .build(); |
| 63 | +``` |
| 64 | + |
| 65 | +## Base Url |
| 66 | + |
| 67 | +You can set a custom base URL when constructing the client. |
| 68 | + |
| 69 | +```java |
| 70 | +import com.icepanel.api.IcePanelClient; |
| 71 | + |
| 72 | +IcePanelClient client = IcePanelClient |
| 73 | + .builder() |
| 74 | + .url("https://example.com") |
| 75 | + .build(); |
| 76 | +``` |
| 77 | + |
| 78 | +## Exception Handling |
| 79 | + |
| 80 | +When the API returns a non-success status code (4xx or 5xx response), an API exception will be thrown. |
| 81 | + |
| 82 | +```java |
| 83 | +import com.icepanel.api.core.IcepanelApiApiException; |
| 84 | + |
| 85 | +try{ |
| 86 | + client.model().objects().list(...); |
| 87 | +} catch (IcepanelApiApiException e){ |
| 88 | + // Do something with the API exception... |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +## Advanced |
| 93 | + |
| 94 | +### Custom Client |
| 95 | + |
| 96 | +This SDK is built to work with any instance of `OkHttpClient`. By default, if no client is provided, the SDK will construct one. |
| 97 | +However, you can pass your own client like so: |
| 98 | + |
| 99 | +```java |
| 100 | +import com.icepanel.api.IcePanelClient; |
| 101 | +import okhttp3.OkHttpClient; |
| 102 | + |
| 103 | +OkHttpClient customClient = ...; |
| 104 | + |
| 105 | +IcePanelClient client = IcePanelClient |
| 106 | + .builder() |
| 107 | + .httpClient(customClient) |
| 108 | + .build(); |
| 109 | +``` |
| 110 | + |
| 111 | +### Retries |
| 112 | + |
| 113 | +The SDK is instrumented with automatic retries with exponential backoff. A request will be retried as long |
| 114 | +as the request is deemed retryable and the number of retry attempts has not grown larger than the configured |
| 115 | +retry limit (default: 2). Before defaulting to exponential backoff, the SDK will first attempt to respect |
| 116 | +the `Retry-After` header (as either in seconds or as an HTTP date), and then the `X-RateLimit-Reset` header |
| 117 | +(as a Unix timestamp in epoch seconds); failing both of those, it will fall back to exponential backoff. |
| 118 | + |
| 119 | +A request is deemed retryable when any of the following HTTP status codes is returned: |
| 120 | + |
| 121 | +- [408](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/408) (Timeout) |
| 122 | +- [429](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429) (Too Many Requests) |
| 123 | +- [5XX](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500) (Internal Server Errors) |
| 124 | + |
| 125 | +Use the `maxRetries` client option to configure this behavior. |
| 126 | + |
| 127 | +```java |
| 128 | +import com.icepanel.api.IcePanelClient; |
| 129 | + |
| 130 | +IcePanelClient client = IcePanelClient |
| 131 | + .builder() |
| 132 | + .maxRetries(1) |
| 133 | + .build(); |
| 134 | +``` |
| 135 | + |
| 136 | +### Timeouts |
| 137 | + |
| 138 | +The SDK defaults to a 60 second timeout. You can configure this with a timeout option at the client or request level. |
| 139 | +```java |
| 140 | +import com.icepanel.api.IcePanelClient; |
| 141 | +import com.icepanel.api.core.RequestOptions; |
| 142 | + |
| 143 | +// Client level |
| 144 | +IcePanelClient client = IcePanelClient |
| 145 | + .builder() |
| 146 | + .timeout(60) |
| 147 | + .build(); |
| 148 | + |
| 149 | +// Request level |
| 150 | +client.model().objects().list( |
| 151 | + ..., |
| 152 | + RequestOptions |
| 153 | + .builder() |
| 154 | + .timeout(60) |
| 155 | + .build() |
| 156 | +); |
| 157 | +``` |
| 158 | + |
| 159 | +### Custom Headers |
| 160 | + |
| 161 | +The SDK allows you to add custom headers to requests. You can configure headers at the client level or at the request level. |
| 162 | + |
| 163 | +```java |
| 164 | +import com.icepanel.api.IcePanelClient; |
| 165 | +import com.icepanel.api.core.RequestOptions; |
| 166 | + |
| 167 | +// Client level |
| 168 | +IcePanelClient client = IcePanelClient |
| 169 | + .builder() |
| 170 | + .addHeader("X-Custom-Header", "custom-value") |
| 171 | + .addHeader("X-Request-Id", "abc-123") |
| 172 | + .build(); |
| 173 | +; |
| 174 | + |
| 175 | +// Request level |
| 176 | +client.model().objects().list( |
| 177 | + ..., |
| 178 | + RequestOptions |
| 179 | + .builder() |
| 180 | + .addHeader("X-Request-Header", "request-value") |
| 181 | + .build() |
| 182 | +); |
| 183 | +``` |
| 184 | + |
| 185 | +### Access Raw Response Data |
| 186 | + |
| 187 | +The SDK provides access to raw response data, including headers, through the `withRawResponse()` method. |
| 188 | +The `withRawResponse()` method returns a raw client that wraps all responses with `body()` and `headers()` methods. |
| 189 | +(A normal client's `response` is identical to a raw client's `response.body()`.) |
| 190 | + |
| 191 | +```java |
| 192 | +ListHttpResponse response = client.model().objects().withRawResponse().list(...); |
| 193 | + |
| 194 | +System.out.println(response.body()); |
| 195 | +System.out.println(response.headers().get("X-My-Header")); |
| 196 | +``` |
| 197 | + |
| 198 | +## Contributing |
| 199 | + |
| 200 | +While we value open-source contributions to this SDK, this library is generated programmatically. |
| 201 | +Additions made directly to this library would have to be moved over to our generation code, |
| 202 | +otherwise they would be overwritten upon the next generated release. Feel free to open a PR as |
| 203 | +a proof of concept, but know that we will not be able to merge it as-is. We suggest opening |
| 204 | +an issue first to discuss with us! |
| 205 | + |
| 206 | +On the other hand, contributions to the README are always very welcome! |
0 commit comments