-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from yahoo/fix_client
add some missing import in client
- Loading branch information
Showing
7 changed files
with
234 additions
and
9 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
// | ||
// This file is generated by test | ||
// WILL NOT be auto-generated if file has already existed. | ||
// | ||
package com.example; | ||
|
||
import com.example.parsec_generated.ResourceException; | ||
import com.example.parsec_generated.User; | ||
|
||
import com.ning.http.client.AsyncHandler; | ||
import com.yahoo.parsec.clients.DefaultAsyncCompletionHandler; | ||
import com.yahoo.parsec.clients.ParsecAsyncHttpClient; | ||
import com.yahoo.parsec.clients.ParsecAsyncHttpRequest; | ||
import com.yahoo.parsec.clients.ParsecAsyncHttpRequest.Builder; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.ws.rs.core.UriBuilder; | ||
import java.net.URI; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
public class SampleClientImpl implements SampleClient { | ||
|
||
/** Logger. */ | ||
private static final Logger LOGGER = LoggerFactory.getLogger(SampleClientImpl.class); | ||
|
||
/** ParsecAsyncHttpClient. */ | ||
private final ParsecAsyncHttpClient parsecAsyncHttpClient; | ||
|
||
/** Object mapper */ | ||
private final ObjectMapper objectMapper; | ||
|
||
/** URL. */ | ||
private String url; | ||
|
||
/** Headers. */ | ||
private final Map<String, String> headers; | ||
|
||
/** | ||
* connection timeout. | ||
*/ | ||
private static final int IDLE_CONNECTION_TIMEOUT_IN_MS = 15000; | ||
|
||
/** | ||
* total connections. | ||
*/ | ||
private static final int MAXIMUM_CONNECTIONS_TOTAL = 50; | ||
|
||
public SampleClientImpl( | ||
String url, | ||
Map<String, String> headers | ||
) { | ||
|
||
ParsecAsyncHttpClient client = null; | ||
try { | ||
client = new ParsecAsyncHttpClient.Builder() | ||
.setAcceptAnyCertificate(true) | ||
.setAllowPoolingConnections(true) | ||
.setPooledConnectionIdleTimeout(IDLE_CONNECTION_TIMEOUT_IN_MS) | ||
.setMaxConnections(MAXIMUM_CONNECTIONS_TOTAL) | ||
.build(); | ||
} catch (ExecutionException e) { | ||
LOGGER.error("create ParsecAsyncHttpClient failed. " + e.getMessage()); | ||
throw new ResourceException(ResourceException.INTERNAL_SERVER_ERROR, e.getMessage()); | ||
} | ||
this.parsecAsyncHttpClient = client; | ||
this.objectMapper = new ObjectMapper(); | ||
this.url = url; | ||
this.headers = headers; | ||
} | ||
|
||
public SampleClientImpl ( | ||
ParsecAsyncHttpClient client, | ||
ObjectMapper objectMapper, | ||
String url, | ||
Map<String, String> headers) | ||
{ | ||
this.parsecAsyncHttpClient = client; | ||
this.objectMapper = objectMapper; | ||
this.url = url; | ||
this.headers = headers; | ||
} | ||
|
||
private ParsecAsyncHttpRequest getRequest(String method, URI uri, String body) throws ResourceException { | ||
Builder builder = new Builder(); | ||
|
||
builder.setUri(uri); | ||
if (headers != null) { | ||
for (Map.Entry<String, String> entry : headers.entrySet()) { | ||
builder.addHeader(entry.getKey(), entry.getValue()); | ||
} | ||
} | ||
|
||
builder.setMethod(method); | ||
|
||
builder.setBody(body).setBodyEncoding("UTF-8"); | ||
|
||
ParsecAsyncHttpRequest request = null; | ||
try { | ||
request = builder.build(); | ||
} catch (Exception e) { | ||
LOGGER.error("builder build failed: " + e.getMessage()); | ||
throw new ResourceException(ResourceException.INTERNAL_SERVER_ERROR, e.getMessage()); | ||
} | ||
return request; | ||
} | ||
|
||
@Override | ||
public CompletableFuture<User> getUser(int id) throws ResourceException { | ||
String path = "/user/{id}"; | ||
String body = null; | ||
|
||
URI uri = UriBuilder.fromUri(url).path(path) | ||
.resolveTemplate("id", id) | ||
.build(); | ||
ParsecAsyncHttpRequest request = getRequest("GET", uri, body); | ||
|
||
|
||
AsyncHandler<User> asyncHandler = new DefaultAsyncCompletionHandler<>(User.class); | ||
|
||
return parsecAsyncHttpClient.criticalExecute(request, asyncHandler); | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
{ | ||
"namespace": "com.example", | ||
"name": "sample", | ||
"version": 1, | ||
"types": [ | ||
{ | ||
"StructTypeDef": { | ||
"type": "Struct", | ||
"name": "User", | ||
"fields": [ | ||
{ | ||
"name": "name", | ||
"type": "String", | ||
"annotations": { | ||
"x_size": "min=3" | ||
} | ||
}, | ||
{ | ||
"name": "id", | ||
"type": "String" | ||
} | ||
] | ||
} | ||
} | ||
], | ||
"resources": [ | ||
{ | ||
"type": "User", | ||
"method": "GET", | ||
"path": "/user/{id}", | ||
"inputs": [ | ||
{ | ||
"name": "id", | ||
"type": "int32", | ||
"pathParam": true | ||
} | ||
], | ||
"expected": "OK", | ||
"exceptions": { | ||
"BAD_REQUEST": { | ||
"type": "ResourceError" | ||
}, | ||
"FORBIDDEN": { | ||
"type": "ResourceError" | ||
}, | ||
"INTERNAL_SERVER_ERROR": { | ||
"type": "ResourceError" | ||
}, | ||
"UNAUTHORIZED": { | ||
"type": "ResourceError" | ||
} | ||
} | ||
} | ||
] | ||
} |