-
Notifications
You must be signed in to change notification settings - Fork 926
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
RestClient
for Java, Kotlin Coroutine and Scala (#4263)
Motivation: `WebClient` and `WebClientPreparation` has powerful and fluent features. RESTful API is classic but it is still the dominant protocol for sharing web resources. The `WebClient` is able to handle not only REST responses but also a stream response and a file response. The API design of `WebClient`, in turn, is not optimized for REST API. See #4250 Modifications: - Add `RestClient` for exchange RESTful APIs. - It assume that the response has a JSON body. - Add `RestClientBuilder` so as to fluently build a `RestClient`. - Refactor the intefaces for request preparations to accommodate `RestClientPreparation`. - Method and path setters are extracted to `RequestMethodSetters`. - Query and path param setters are moved to `PathAndQueryParamSetters` - Added `HttpMessageSetters` for setting `HttpMessage` properties. - Add `execute()` extension methods for Kotlin Coroutine. - Add `ScalaRestClient` for Scala. - Tried to add extension methods using an implicit class but Scala compiler failed to find the overloaded method which has a generic parameter. - Provide a extension method to convert a `WebClient` to `ScalaRestClient`. - Add `restClient()` to `ServerExtension` and `ServerRule`. Result: You can now easily send and receive RESTful APIs using `RestClient` - Java ```java RestClient restClient = RestClient.of("..."); CompletableFuture<ResponseEntity<Customer>> response = restClient.get("/api/v1/customers/{customerId}") .pathParam("customerId", "0000001") .execute(Customer.class); ``` - Kotlin ```kt val restClient: RestClient = RestClient.of("..."); val response: ResponseEntity<Customer> = restClient .get("/api/v1/customers/{customerId}") .pathParam("customerId", "0000001") .execute<Customer>() // a suspend function ``` - Scala ```scala val restClient: ScalaRestClient = ScalaRestClient("...") val response: Future[ResponseEntity[Result]] = restClient.post("/api/v1/customers") .contentJson(new Customer(...)) .execute[Result]() ```
- Loading branch information
Showing
37 changed files
with
2,443 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
core/src/main/java/com/linecorp/armeria/client/DefaultRestClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Copyright 2022 LINE Corporation | ||
* | ||
* LINE Corporation licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package com.linecorp.armeria.client; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import java.net.URI; | ||
|
||
import com.linecorp.armeria.client.endpoint.EndpointGroup; | ||
import com.linecorp.armeria.common.HttpMethod; | ||
import com.linecorp.armeria.common.Scheme; | ||
|
||
final class DefaultRestClient implements RestClient { | ||
|
||
static final RestClient DEFAULT = new DefaultRestClient(WebClient.of()); | ||
|
||
private final WebClient delegate; | ||
|
||
DefaultRestClient(WebClient delegate) { | ||
requireNonNull(delegate, "delegate"); | ||
this.delegate = delegate; | ||
} | ||
|
||
@Override | ||
public RestClientPreparation path(HttpMethod method, String path) { | ||
requireNonNull(method, "method"); | ||
requireNonNull(path, "path"); | ||
return new RestClientPreparation(delegate, method, path); | ||
} | ||
|
||
@Override | ||
public Scheme scheme() { | ||
return delegate.scheme(); | ||
} | ||
|
||
@Override | ||
public EndpointGroup endpointGroup() { | ||
return delegate.endpointGroup(); | ||
} | ||
|
||
@Override | ||
public String absolutePathRef() { | ||
return delegate.absolutePathRef(); | ||
} | ||
|
||
@Override | ||
public URI uri() { | ||
return delegate.uri(); | ||
} | ||
|
||
@Override | ||
public Class<?> clientType() { | ||
return delegate.clientType(); | ||
} | ||
|
||
@Override | ||
public ClientOptions options() { | ||
return delegate.options(); | ||
} | ||
|
||
@Override | ||
public HttpClient unwrap() { | ||
return delegate.unwrap(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.