armeria icon indicating copy to clipboard operation
armeria copied to clipboard

Provide a way to send a JSON object with WebClient

Open karellen-kim opened this issue 4 years ago • 7 comments

Motivation: It would also be nice to have APIs that automatically convert a request body into bytestring and converts a response into an object.

Modifications:

  • Add method ofJson in HttpRequest
  • Add method aggregateAs in HttpResponse

Result:

  • Closes #3707
  • Users can convert a request body into bytestring automatically :
HttpRequest.ofJson(HttpMethod.POST, "/receiveContent", requestContent)
  • Users can convert a response into an object automatically :
Content content = HttpResponse.of(MediaType.JSON_UTF_8, contentString)
                              .aggregateAs(Content.class)
                              .join(); 

ErrorMessage error = HttpResponse.of(MediaType.JSON_UTF_8, contentString)
                                 .aggregateAs(HttpStatusClass.SERVER_ERROR, ErrorMessage.class)
                                 .join();

ErrorMessage error = HttpResponse.of(MediaType.JSON_UTF_8, contentString)
                                 .aggregateAs(status -> status != HttpStatus.OK, ErrorMessage.class)
                                 .join();

Wrapper<Content> wrapperContent = HttpResponse.of(MediaType.JSON_UTF_8, contentString)
                                              .aggregateAs(new TypeReference<Wrapper<Content>>(){})
                                              .join();

karellen-kim avatar Aug 09 '21 11:08 karellen-kim

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Aug 09 '21 11:08 CLAassistant

Codecov Report

Attention: Patch coverage is 35.38462% with 42 lines in your changes missing coverage. Please review.

Project coverage is 73.16%. Comparing base (829992a) to head (9a74a7b). Report is 1036 commits behind head on main.

Files Patch % Lines
...armeria/common/HttpUnsupportedStatusException.java 0.00% 14 Missing :warning:
...p/armeria/common/HttpResponseContentException.java 0.00% 12 Missing :warning:
...eria/common/HttpUnsupportedMediaTypeException.java 58.33% 8 Missing and 2 partials :warning:
.../java/com/linecorp/armeria/common/HttpRequest.java 60.00% 5 Missing and 1 partial :warning:
Additional details and impacted files
@@             Coverage Diff              @@
##               main    #3757      +/-   ##
============================================
- Coverage     73.18%   73.16%   -0.03%     
- Complexity    16126    16133       +7     
============================================
  Files          1405     1408       +3     
  Lines         61669    61734      +65     
  Branches       7765     7771       +6     
============================================
+ Hits          45134    45165      +31     
- Misses        12612    12631      +19     
- Partials       3923     3938      +15     

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov[bot] avatar Aug 09 '21 12:08 codecov[bot]

@karellen-kim I think #4021 takes over the features of this PR. Let me close this PR. 🙇‍♂️ I hope to see the next PR in the near future. 😆

ikhoon avatar Jan 27 '22 08:01 ikhoon

But still we can't send a JSON object as it is, right? #4021 only takes care of the receiving part.

trustin avatar Jan 27 '22 08:01 trustin

Ah, this PR includes HttpRequest.ofJson() also. I missed that. Let me reopen and continue to develop the sending part.

ikhoon avatar Jan 27 '22 08:01 ikhoon

Gentle ping. @karellen-kim

ikhoon avatar Jan 27 '22 12:01 ikhoon