|
| 1 | +package com.raven.api; |
| 2 | + |
| 3 | +import com.raven.api.core.ClientOptions; |
| 4 | +import com.raven.api.core.Environment; |
| 5 | +import com.raven.api.core.ObjectMappers; |
| 6 | +import com.raven.api.core.Suppliers; |
| 7 | +import com.raven.api.resources.device.DeviceClient; |
| 8 | +import com.raven.api.resources.device.DeviceClientImpl; |
| 9 | +import com.raven.api.resources.requests.BulkSendEventRequest; |
| 10 | +import com.raven.api.resources.requests.SendEventRequest; |
| 11 | +import com.raven.api.resources.types.SendEventResponse; |
| 12 | +import com.raven.api.resources.user.UserClient; |
| 13 | +import com.raven.api.resources.user.UserClientImpl; |
| 14 | +import java.lang.Exception; |
| 15 | +import java.lang.Object; |
| 16 | +import java.lang.Override; |
| 17 | +import java.lang.RuntimeException; |
| 18 | +import java.lang.String; |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.function.Supplier; |
| 22 | +import okhttp3.Headers; |
| 23 | +import okhttp3.HttpUrl; |
| 24 | +import okhttp3.MediaType; |
| 25 | +import okhttp3.Request; |
| 26 | +import okhttp3.RequestBody; |
| 27 | +import okhttp3.Response; |
| 28 | + |
| 29 | +public final class RavenApiClientImpl implements RavenApiClient { |
| 30 | + private final ClientOptions clientOptions; |
| 31 | + |
| 32 | + private final Supplier<DeviceClient> deviceClient; |
| 33 | + |
| 34 | + private final Supplier<UserClient> userClient; |
| 35 | + |
| 36 | + public RavenApiClientImpl(ClientOptions clientOptions) { |
| 37 | + this.clientOptions = clientOptions; |
| 38 | + this.deviceClient = Suppliers.memoize(() -> new DeviceClientImpl(clientOptions)); |
| 39 | + this.userClient = Suppliers.memoize(() -> new UserClientImpl(clientOptions)); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public SendEventResponse send(String appId, SendEventRequest request) { |
| 44 | + HttpUrl.Builder _httpUrlBuilder = HttpUrl.parse(this.clientOptions.environment().getUrl()).newBuilder() |
| 45 | + .addPathSegments("v1/apps") |
| 46 | + .addPathSegment(appId) |
| 47 | + .addPathSegments("events/send") |
| 48 | + ;HttpUrl _httpUrl = _httpUrlBuilder.build(); |
| 49 | + Map<String, Object> _requestBodyProperties = new HashMap<>(); |
| 50 | + _requestBodyProperties.put("event", request.getEvent()); |
| 51 | + _requestBodyProperties.put("data", request.getData()); |
| 52 | + _requestBodyProperties.put("user", request.getUser()); |
| 53 | + _requestBodyProperties.put("scheduleAt", request.getScheduleAt()); |
| 54 | + _requestBodyProperties.put("override", request.getOverride()); |
| 55 | + RequestBody _requestBody; |
| 56 | + try { |
| 57 | + _requestBody = RequestBody.create(ObjectMappers.JSON_MAPPER.writeValueAsBytes(_requestBodyProperties), MediaType.parse("application/json")); |
| 58 | + } |
| 59 | + catch(Exception e) { |
| 60 | + throw new RuntimeException(e); |
| 61 | + } |
| 62 | + Request.Builder _requestBuilder = new Request.Builder() |
| 63 | + .url(_httpUrl) |
| 64 | + .method("POST", _requestBody) |
| 65 | + .headers(Headers.of(clientOptions.headers())); |
| 66 | + if (request.getIdempotencyKey().isPresent()) { |
| 67 | + _requestBuilder.addHeader("Idempotency-Key", request.getIdempotencyKey().get()); |
| 68 | + } |
| 69 | + Request _request = _requestBuilder.build(); |
| 70 | + try { |
| 71 | + Response _response = clientOptions.httpClient().newCall(_request).execute(); |
| 72 | + if (_response.isSuccessful()) { |
| 73 | + return ObjectMappers.JSON_MAPPER.readValue(_response.body().string(), SendEventResponse.class); |
| 74 | + } |
| 75 | + throw new RuntimeException(); |
| 76 | + } |
| 77 | + catch (Exception e) { |
| 78 | + throw new RuntimeException(e); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public SendEventResponse sendBulk(String appId, BulkSendEventRequest request) { |
| 84 | + HttpUrl.Builder _httpUrlBuilder = HttpUrl.parse(this.clientOptions.environment().getUrl()).newBuilder() |
| 85 | + .addPathSegments("v1/apps") |
| 86 | + .addPathSegment(appId) |
| 87 | + .addPathSegments("events/bulk_send") |
| 88 | + ;HttpUrl _httpUrl = _httpUrlBuilder.build(); |
| 89 | + Map<String, Object> _requestBodyProperties = new HashMap<>(); |
| 90 | + _requestBodyProperties.put("event", request.getEvent()); |
| 91 | + _requestBodyProperties.put("batch", request.getBatch()); |
| 92 | + RequestBody _requestBody; |
| 93 | + try { |
| 94 | + _requestBody = RequestBody.create(ObjectMappers.JSON_MAPPER.writeValueAsBytes(_requestBodyProperties), MediaType.parse("application/json")); |
| 95 | + } |
| 96 | + catch(Exception e) { |
| 97 | + throw new RuntimeException(e); |
| 98 | + } |
| 99 | + Request.Builder _requestBuilder = new Request.Builder() |
| 100 | + .url(_httpUrl) |
| 101 | + .method("POST", _requestBody) |
| 102 | + .headers(Headers.of(clientOptions.headers())); |
| 103 | + if (request.getIdempotencyKey().isPresent()) { |
| 104 | + _requestBuilder.addHeader("Idempotency-Key", request.getIdempotencyKey().get()); |
| 105 | + } |
| 106 | + Request _request = _requestBuilder.build(); |
| 107 | + try { |
| 108 | + Response _response = clientOptions.httpClient().newCall(_request).execute(); |
| 109 | + if (_response.isSuccessful()) { |
| 110 | + return ObjectMappers.JSON_MAPPER.readValue(_response.body().string(), SendEventResponse.class); |
| 111 | + } |
| 112 | + throw new RuntimeException(); |
| 113 | + } |
| 114 | + catch (Exception e) { |
| 115 | + throw new RuntimeException(e); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public DeviceClient device() { |
| 121 | + return this.deviceClient.get(); |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public UserClient user() { |
| 126 | + return this.userClient.get(); |
| 127 | + } |
| 128 | + |
| 129 | + public static final class Builder implements RavenApiClient.Builder { |
| 130 | + ClientOptions.Builder clientOptionsBuilder = ClientOptions.builder(); |
| 131 | + |
| 132 | + Environment environment = Environment.PROD; |
| 133 | + |
| 134 | + @Override |
| 135 | + public RavenApiClient.Builder authKey(String authKey) { |
| 136 | + this.clientOptionsBuilder.addHeader("Authorization", authKey); |
| 137 | + return this; |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public RavenApiClient.Builder environment(Environment environment) { |
| 142 | + this.environment = environment; |
| 143 | + return this; |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public RavenApiClient.Builder url(String url) { |
| 148 | + this.environment = Environment.custom(url); |
| 149 | + return this; |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public RavenApiClient build() { |
| 154 | + clientOptionsBuilder.environment(this.environment); |
| 155 | + return new RavenApiClientImpl(clientOptionsBuilder.build()); |
| 156 | + } |
| 157 | + } |
| 158 | + } |
0 commit comments