|
| 1 | +/** |
| 2 | + * This file was auto-generated by Fern from our API Definition. |
| 3 | + */ |
| 4 | +package com.polytomic.api.resources.notifications; |
| 5 | + |
| 6 | +import com.polytomic.api.core.ApiError; |
| 7 | +import com.polytomic.api.core.ClientOptions; |
| 8 | +import com.polytomic.api.core.MediaTypes; |
| 9 | +import com.polytomic.api.core.ObjectMappers; |
| 10 | +import com.polytomic.api.core.RequestOptions; |
| 11 | +import com.polytomic.api.resources.notifications.requests.V4GlobalErrorSubscribersRequest; |
| 12 | +import com.polytomic.api.types.V4GlobalErrorSubscribersResponse; |
| 13 | +import java.io.IOException; |
| 14 | +import okhttp3.Headers; |
| 15 | +import okhttp3.HttpUrl; |
| 16 | +import okhttp3.OkHttpClient; |
| 17 | +import okhttp3.Request; |
| 18 | +import okhttp3.RequestBody; |
| 19 | +import okhttp3.Response; |
| 20 | +import okhttp3.ResponseBody; |
| 21 | + |
| 22 | +public class NotificationsClient { |
| 23 | + protected final ClientOptions clientOptions; |
| 24 | + |
| 25 | + public NotificationsClient(ClientOptions clientOptions) { |
| 26 | + this.clientOptions = clientOptions; |
| 27 | + } |
| 28 | + |
| 29 | + public V4GlobalErrorSubscribersResponse getGlobalErrorSubscribers() { |
| 30 | + return getGlobalErrorSubscribers(null); |
| 31 | + } |
| 32 | + |
| 33 | + public V4GlobalErrorSubscribersResponse getGlobalErrorSubscribers(RequestOptions requestOptions) { |
| 34 | + HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl()) |
| 35 | + .newBuilder() |
| 36 | + .addPathSegments("api/notifications/global-error-subscribers") |
| 37 | + .build(); |
| 38 | + Request okhttpRequest = new Request.Builder() |
| 39 | + .url(httpUrl) |
| 40 | + .method("GET", null) |
| 41 | + .headers(Headers.of(clientOptions.headers(requestOptions))) |
| 42 | + .addHeader("Content-Type", "application/json") |
| 43 | + .build(); |
| 44 | + try { |
| 45 | + OkHttpClient client = clientOptions.httpClient(); |
| 46 | + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { |
| 47 | + client = clientOptions.httpClientWithTimeout(requestOptions); |
| 48 | + } |
| 49 | + Response response = client.newCall(okhttpRequest).execute(); |
| 50 | + ResponseBody responseBody = response.body(); |
| 51 | + if (response.isSuccessful()) { |
| 52 | + return ObjectMappers.JSON_MAPPER.readValue( |
| 53 | + responseBody.string(), V4GlobalErrorSubscribersResponse.class); |
| 54 | + } |
| 55 | + throw new ApiError( |
| 56 | + response.code(), |
| 57 | + ObjectMappers.JSON_MAPPER.readValue( |
| 58 | + responseBody != null ? responseBody.string() : "{}", Object.class)); |
| 59 | + } catch (IOException e) { |
| 60 | + throw new RuntimeException(e); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + public V4GlobalErrorSubscribersResponse setGlobalErrorSubscribers() { |
| 65 | + return setGlobalErrorSubscribers( |
| 66 | + V4GlobalErrorSubscribersRequest.builder().build()); |
| 67 | + } |
| 68 | + |
| 69 | + public V4GlobalErrorSubscribersResponse setGlobalErrorSubscribers(V4GlobalErrorSubscribersRequest request) { |
| 70 | + return setGlobalErrorSubscribers(request, null); |
| 71 | + } |
| 72 | + |
| 73 | + public V4GlobalErrorSubscribersResponse setGlobalErrorSubscribers( |
| 74 | + V4GlobalErrorSubscribersRequest request, RequestOptions requestOptions) { |
| 75 | + HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl()) |
| 76 | + .newBuilder() |
| 77 | + .addPathSegments("api/notifications/global-error-subscribers") |
| 78 | + .build(); |
| 79 | + RequestBody body; |
| 80 | + try { |
| 81 | + body = RequestBody.create( |
| 82 | + ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON); |
| 83 | + } catch (Exception e) { |
| 84 | + throw new RuntimeException(e); |
| 85 | + } |
| 86 | + Request okhttpRequest = new Request.Builder() |
| 87 | + .url(httpUrl) |
| 88 | + .method("PUT", body) |
| 89 | + .headers(Headers.of(clientOptions.headers(requestOptions))) |
| 90 | + .addHeader("Content-Type", "application/json") |
| 91 | + .build(); |
| 92 | + try { |
| 93 | + OkHttpClient client = clientOptions.httpClient(); |
| 94 | + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { |
| 95 | + client = clientOptions.httpClientWithTimeout(requestOptions); |
| 96 | + } |
| 97 | + Response response = client.newCall(okhttpRequest).execute(); |
| 98 | + ResponseBody responseBody = response.body(); |
| 99 | + if (response.isSuccessful()) { |
| 100 | + return ObjectMappers.JSON_MAPPER.readValue( |
| 101 | + responseBody.string(), V4GlobalErrorSubscribersResponse.class); |
| 102 | + } |
| 103 | + throw new ApiError( |
| 104 | + response.code(), |
| 105 | + ObjectMappers.JSON_MAPPER.readValue( |
| 106 | + responseBody != null ? responseBody.string() : "{}", Object.class)); |
| 107 | + } catch (IOException e) { |
| 108 | + throw new RuntimeException(e); |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments