1010import so .trophy .core .ClientOptions ;
1111import so .trophy .core .MediaTypes ;
1212import so .trophy .core .ObjectMappers ;
13+ import so .trophy .core .QueryStringMapper ;
1314import so .trophy .core .RequestOptions ;
1415import so .trophy .core .TrophyApiApiException ;
1516import so .trophy .core .TrophyApiException ;
3334import okhttp3 .Response ;
3435import okhttp3 .ResponseBody ;
3536import org .jetbrains .annotations .NotNull ;
37+ import so .trophy .resources .achievements .requests .AchievementsAllRequest ;
3638import so .trophy .resources .achievements .requests .AchievementsCompleteRequest ;
3739import so .trophy .types .AchievementCompletionResponse ;
3840import so .trophy .types .AchievementWithStatsResponse ;
@@ -49,138 +51,147 @@ public AsyncRawAchievementsClient(ClientOptions clientOptions) {
4951 * Get all achievements and their completion stats.
5052 */
5153 public CompletableFuture <TrophyApiHttpResponse <List <AchievementWithStatsResponse >>> all () {
52- return all (null );
54+ return all (AchievementsAllRequest . builder (). build () );
5355 }
5456
5557 /**
5658 * Get all achievements and their completion stats.
5759 */
5860 public CompletableFuture <TrophyApiHttpResponse <List <AchievementWithStatsResponse >>> all (
59- RequestOptions requestOptions ) {
60- HttpUrl httpUrl = HttpUrl .parse (this .clientOptions .environment ().getApiURL ()).newBuilder ()
61+ AchievementsAllRequest request ) {
62+ return all (request ,null );
63+ }
6164
62- . addPathSegments ( "achievements" )
63- . build ();
64- Request okhttpRequest = new Request . Builder ()
65- . url ( httpUrl )
66- . method ( "GET" , null )
67- . headers ( Headers . of ( clientOptions .headers ( requestOptions )) )
68- . addHeader ( "Accept" , "application/json" )
69- .build ();
70- OkHttpClient client = clientOptions . httpClient ( );
71- if ( requestOptions != null && requestOptions . getTimeout (). isPresent ()) {
72- client = clientOptions . httpClientWithTimeout ( requestOptions );
73- }
74- CompletableFuture < TrophyApiHttpResponse < List < AchievementWithStatsResponse >>> future = new CompletableFuture <>();
75- client . newCall ( okhttpRequest ). enqueue ( new Callback () {
76- @ Override
77- public void onResponse ( @ NotNull Call call , @ NotNull Response response ) throws IOException {
78- try ( ResponseBody responseBody = response . body ()) {
79- if ( response . isSuccessful ()) {
80- future . complete ( new TrophyApiHttpResponse <>( ObjectMappers . JSON_MAPPER . readValue ( responseBody . string (), new TypeReference < List < AchievementWithStatsResponse >>() {}), response ) );
81- return ;
82- }
83- String responseBodyString = responseBody != null ? responseBody . string () : "{}" ;
84- try {
85- switch ( response . code ()) {
86- case 401 : future . completeExceptionally ( new UnauthorizedError ( ObjectMappers . JSON_MAPPER . readValue ( responseBodyString , ErrorBody . class ), response ));
87- return ;
88- case 422 : future .completeExceptionally (new UnprocessableEntityError (ObjectMappers .JSON_MAPPER .readValue (responseBodyString , ErrorBody . class ), response ));
65+ /**
66+ * Get all achievements and their completion stats.
67+ */
68+ public CompletableFuture < TrophyApiHttpResponse < List < AchievementWithStatsResponse >>> all (
69+ AchievementsAllRequest request , RequestOptions requestOptions ) {
70+ HttpUrl . Builder httpUrl = HttpUrl . parse ( this . clientOptions .environment (). getApiURL ()). newBuilder ( )
71+
72+ .addPathSegments ( "achievements" ); if ( request . getUserAttributes (). isPresent ()) {
73+ QueryStringMapper . addQueryParameter ( httpUrl , "userAttributes" , request . getUserAttributes (). get (), false );
74+ }
75+ Request . Builder _requestBuilder = new Request . Builder ()
76+ . url ( httpUrl . build ())
77+ . method ( "GET" , null )
78+ . headers ( Headers . of ( clientOptions . headers ( requestOptions )))
79+ . addHeader ( "Accept" , "application/json" );
80+ Request okhttpRequest = _requestBuilder . build ();
81+ OkHttpClient client = clientOptions . httpClient ();
82+ if ( requestOptions != null && requestOptions . getTimeout (). isPresent ()) {
83+ client = clientOptions . httpClientWithTimeout ( requestOptions );
84+ }
85+ CompletableFuture < TrophyApiHttpResponse < List < AchievementWithStatsResponse >>> future = new CompletableFuture <>();
86+ client . newCall ( okhttpRequest ). enqueue ( new Callback () {
87+ @ Override
88+ public void onResponse ( @ NotNull Call call , @ NotNull Response response ) throws IOException {
89+ try ( ResponseBody responseBody = response . body ()) {
90+ if ( response . isSuccessful ()) {
91+ future .complete (new TrophyApiHttpResponse <> (ObjectMappers .JSON_MAPPER .readValue (responseBody . string (), new TypeReference < List < AchievementWithStatsResponse >>() {} ), response ));
8992 return ;
9093 }
94+ String responseBodyString = responseBody != null ? responseBody .string () : "{}" ;
95+ try {
96+ switch (response .code ()) {
97+ case 401 :future .completeExceptionally (new UnauthorizedError (ObjectMappers .JSON_MAPPER .readValue (responseBodyString , ErrorBody .class ), response ));
98+ return ;
99+ case 422 :future .completeExceptionally (new UnprocessableEntityError (ObjectMappers .JSON_MAPPER .readValue (responseBodyString , ErrorBody .class ), response ));
100+ return ;
101+ }
102+ }
103+ catch (JsonProcessingException ignored ) {
104+ // unable to map error response, throwing generic error
105+ }
106+ future .completeExceptionally (new TrophyApiApiException ("Error with status code " + response .code (), response .code (), ObjectMappers .JSON_MAPPER .readValue (responseBodyString , Object .class ), response ));
107+ return ;
91108 }
92- catch (JsonProcessingException ignored ) {
93- // unable to map error response, throwing generic error
109+ catch (IOException e ) {
110+ future . completeExceptionally ( new TrophyApiException ( "Network error executing HTTP request" , e ));
94111 }
95- future .completeExceptionally (new TrophyApiApiException ("Error with status code " + response .code (), response .code (), ObjectMappers .JSON_MAPPER .readValue (responseBodyString , Object .class ), response ));
96- return ;
97112 }
98- catch (IOException e ) {
113+
114+ @ Override
115+ public void onFailure (@ NotNull Call call , @ NotNull IOException e ) {
99116 future .completeExceptionally (new TrophyApiException ("Network error executing HTTP request" , e ));
100117 }
101- }
102-
103- @ Override
104- public void onFailure (@ NotNull Call call , @ NotNull IOException e ) {
105- future .completeExceptionally (new TrophyApiException ("Network error executing HTTP request" , e ));
106- }
107- });
108- return future ;
109- }
118+ });
119+ return future ;
120+ }
110121
111- /**
112- * Mark an achievement as completed for a user.
113- */
114- public CompletableFuture <TrophyApiHttpResponse <AchievementCompletionResponse >> complete (
115- String key , AchievementsCompleteRequest request ) {
116- return complete (key ,request ,null );
117- }
122+ /**
123+ * Mark an achievement as completed for a user.
124+ */
125+ public CompletableFuture <TrophyApiHttpResponse <AchievementCompletionResponse >> complete (
126+ String key , AchievementsCompleteRequest request ) {
127+ return complete (key ,request ,null );
128+ }
118129
119- /**
120- * Mark an achievement as completed for a user.
121- */
122- public CompletableFuture <TrophyApiHttpResponse <AchievementCompletionResponse >> complete (
123- String key , AchievementsCompleteRequest request , RequestOptions requestOptions ) {
124- HttpUrl httpUrl = HttpUrl .parse (this .clientOptions .environment ().getApiURL ()).newBuilder ()
130+ /**
131+ * Mark an achievement as completed for a user.
132+ */
133+ public CompletableFuture <TrophyApiHttpResponse <AchievementCompletionResponse >> complete (
134+ String key , AchievementsCompleteRequest request , RequestOptions requestOptions ) {
135+ HttpUrl httpUrl = HttpUrl .parse (this .clientOptions .environment ().getApiURL ()).newBuilder ()
125136
126- .addPathSegments ("achievements" )
127- .addPathSegment (key )
128- .addPathSegments ("complete" )
129- .build ();
130- RequestBody body ;
131- try {
132- body = RequestBody .create (ObjectMappers .JSON_MAPPER .writeValueAsBytes (request ), MediaTypes .APPLICATION_JSON );
133- }
134- catch (JsonProcessingException e ) {
135- throw new TrophyApiException ("Failed to serialize request" , e );
136- }
137- Request okhttpRequest = new Request .Builder ()
138- .url (httpUrl )
139- .method ("POST" , body )
140- .headers (Headers .of (clientOptions .headers (requestOptions )))
141- .addHeader ("Content-Type" , "application/json" )
142- .addHeader ("Accept" , "application/json" )
143- .build ();
144- OkHttpClient client = clientOptions .httpClient ();
145- if (requestOptions != null && requestOptions .getTimeout ().isPresent ()) {
146- client = clientOptions .httpClientWithTimeout (requestOptions );
147- }
148- CompletableFuture <TrophyApiHttpResponse <AchievementCompletionResponse >> future = new CompletableFuture <>();
149- client .newCall (okhttpRequest ).enqueue (new Callback () {
150- @ Override
151- public void onResponse (@ NotNull Call call , @ NotNull Response response ) throws IOException {
152- try (ResponseBody responseBody = response .body ()) {
153- if (response .isSuccessful ()) {
154- future .complete (new TrophyApiHttpResponse <>(ObjectMappers .JSON_MAPPER .readValue (responseBody .string (), AchievementCompletionResponse .class ), response ));
155- return ;
156- }
157- String responseBodyString = responseBody != null ? responseBody .string () : "{}" ;
158- try {
159- switch (response .code ()) {
160- case 401 :future .completeExceptionally (new UnauthorizedError (ObjectMappers .JSON_MAPPER .readValue (responseBodyString , ErrorBody .class ), response ));
161- return ;
162- case 404 :future .completeExceptionally (new NotFoundError (ObjectMappers .JSON_MAPPER .readValue (responseBodyString , ErrorBody .class ), response ));
163- return ;
164- case 422 :future .completeExceptionally (new UnprocessableEntityError (ObjectMappers .JSON_MAPPER .readValue (responseBodyString , ErrorBody .class ), response ));
137+ .addPathSegments ("achievements" )
138+ .addPathSegment (key )
139+ .addPathSegments ("complete" )
140+ .build ();
141+ RequestBody body ;
142+ try {
143+ body = RequestBody .create (ObjectMappers .JSON_MAPPER .writeValueAsBytes (request ), MediaTypes .APPLICATION_JSON );
144+ }
145+ catch (JsonProcessingException e ) {
146+ throw new TrophyApiException ("Failed to serialize request" , e );
147+ }
148+ Request okhttpRequest = new Request .Builder ()
149+ .url (httpUrl )
150+ .method ("POST" , body )
151+ .headers (Headers .of (clientOptions .headers (requestOptions )))
152+ .addHeader ("Content-Type" , "application/json" )
153+ .addHeader ("Accept" , "application/json" )
154+ .build ();
155+ OkHttpClient client = clientOptions .httpClient ();
156+ if (requestOptions != null && requestOptions .getTimeout ().isPresent ()) {
157+ client = clientOptions .httpClientWithTimeout (requestOptions );
158+ }
159+ CompletableFuture <TrophyApiHttpResponse <AchievementCompletionResponse >> future = new CompletableFuture <>();
160+ client .newCall (okhttpRequest ).enqueue (new Callback () {
161+ @ Override
162+ public void onResponse (@ NotNull Call call , @ NotNull Response response ) throws IOException {
163+ try (ResponseBody responseBody = response .body ()) {
164+ if (response .isSuccessful ()) {
165+ future .complete (new TrophyApiHttpResponse <>(ObjectMappers .JSON_MAPPER .readValue (responseBody .string (), AchievementCompletionResponse .class ), response ));
165166 return ;
166167 }
168+ String responseBodyString = responseBody != null ? responseBody .string () : "{}" ;
169+ try {
170+ switch (response .code ()) {
171+ case 401 :future .completeExceptionally (new UnauthorizedError (ObjectMappers .JSON_MAPPER .readValue (responseBodyString , ErrorBody .class ), response ));
172+ return ;
173+ case 404 :future .completeExceptionally (new NotFoundError (ObjectMappers .JSON_MAPPER .readValue (responseBodyString , ErrorBody .class ), response ));
174+ return ;
175+ case 422 :future .completeExceptionally (new UnprocessableEntityError (ObjectMappers .JSON_MAPPER .readValue (responseBodyString , ErrorBody .class ), response ));
176+ return ;
177+ }
178+ }
179+ catch (JsonProcessingException ignored ) {
180+ // unable to map error response, throwing generic error
181+ }
182+ future .completeExceptionally (new TrophyApiApiException ("Error with status code " + response .code (), response .code (), ObjectMappers .JSON_MAPPER .readValue (responseBodyString , Object .class ), response ));
183+ return ;
167184 }
168- catch (JsonProcessingException ignored ) {
169- // unable to map error response, throwing generic error
185+ catch (IOException e ) {
186+ future . completeExceptionally ( new TrophyApiException ( "Network error executing HTTP request" , e ));
170187 }
171- future .completeExceptionally (new TrophyApiApiException ("Error with status code " + response .code (), response .code (), ObjectMappers .JSON_MAPPER .readValue (responseBodyString , Object .class ), response ));
172- return ;
173188 }
174- catch (IOException e ) {
189+
190+ @ Override
191+ public void onFailure (@ NotNull Call call , @ NotNull IOException e ) {
175192 future .completeExceptionally (new TrophyApiException ("Network error executing HTTP request" , e ));
176193 }
177- }
178-
179- @ Override
180- public void onFailure (@ NotNull Call call , @ NotNull IOException e ) {
181- future .completeExceptionally (new TrophyApiException ("Network error executing HTTP request" , e ));
182- }
183- });
184- return future ;
194+ });
195+ return future ;
196+ }
185197 }
186- }
0 commit comments