Skip to content

Commit

Permalink
Merge pull request #22 from yahoo/make_client_decide_headers_in_function
Browse files Browse the repository at this point in the history
Make client decide headers in function
  • Loading branch information
joejoe321321 authored Feb 10, 2017
2 parents f809fea + 4d290d5 commit ddd7121
Show file tree
Hide file tree
Showing 5 changed files with 227 additions and 46 deletions.
95 changes: 71 additions & 24 deletions cmd/rdl-gen-parsec-java-client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,19 @@ func (gen *javaClientGenerator) processTemplate(templateSource string) error {
"header": func() string { return utils.JavaGenerationHeader(gen.banner) },
"package": func() string { return utils.JavaGenerationPackage(gen.schema, gen.ns) },
"comment": commentFun,
"methodSig": func(r *rdl.Resource) string { return "public "+ gen.clientMethodSignature(r) },
"methodSigWithHeader":
func(r *rdl.Resource) string { return "public "+ gen.clientMethodSignature(r, true) },
"methodSig": func(r *rdl.Resource) string { return "public "+ gen.clientMethodSignature(r, false) },
"ContentOfNoHeaderMethod":
func(r *rdl.Resource) string { return gen.clientMethodOverloadContent(r) },
"name": func() string { return gen.name },
"cName": func() string { return utils.Capitalize(gen.name) },
"lName": func() string { return utils.Uncapitalize(gen.name) },
"needBody": needBodyFunc,
"bodyObj": func(r *rdl.Resource) string { return gen.getBodyObj(r) },
"iMethod": func(r *rdl.Resource) string { return gen.clientMethodSignature(r) + ";" },
"iMethodWithHeader":
func(r *rdl.Resource) string { return gen.clientMethodSignature(r, true) + ";" },
"iMethod": func(r *rdl.Resource) string { return gen.clientMethodSignature(r, false) + ";" },
"builderExt": func(r *rdl.Resource) string { return gen.builderExt(r) },
"origPackage": func() string { return utils.JavaGenerationOrigPackage(gen.schema, gen.ns) },
"origHeader": func() string { return utils.JavaGenerationOrigHeader(gen.banner) },
Expand Down Expand Up @@ -198,14 +204,17 @@ func (gen *javaClientGenerator) needBody(r *rdl.Resource) bool {
const javaClientInterfaceTemplate = `{{origHeader}}
package {{origPackage}}.parsec_generated;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import {{package}}.ResourceException;
{{range .Types}}{{if .StructTypeDef}}{{if .StructTypeDef.Name}}import {{package}}.{{.StructTypeDef.Name}};
{{end}}{{end}}{{end}}
public interface {{cName}}Client {
{{range .Resources}}
{{iMethod .}}{{end}}
{{iMethod .}}
{{iMethodWithHeader .}}{{end}}
}
`
const javaClientTemplate = `{{origHeader}}
Expand All @@ -229,6 +238,7 @@ import javax.ws.rs.core.UriBuilder;
import java.net.URI;
{{if needImportHashSet .Resources}}import java.util.HashSet;
import java.util.Set;{{end}}
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
Expand All @@ -248,7 +258,7 @@ public class {{cName}}ClientImpl implements {{cName}}Client {
private String url;
/** Headers. */
private final Map<String, String> headers;
private final Map<String, List<String>> defaultHeaders;
/**
* connection timeout.
Expand All @@ -260,9 +270,13 @@ public class {{cName}}ClientImpl implements {{cName}}Client {
*/
private static final int MAXIMUM_CONNECTIONS_TOTAL = 50;
public {{cName}}ClientImpl(String url) {
this(url, null);
}
public {{cName}}ClientImpl(
String url,
Map<String, String> headers
Map<String, List<String>> headers
) {
ParsecAsyncHttpClient client = null;
Expand All @@ -280,28 +294,36 @@ public class {{cName}}ClientImpl implements {{cName}}Client {
this.parsecAsyncHttpClient = client;
this.objectMapper = new ObjectMapper();
this.url = url;
this.headers = headers;
this.defaultHeaders = headers;
}
public {{cName}}ClientImpl(
ParsecAsyncHttpClient client,
ObjectMapper objectMapper,
String url,
Map<String, String> headers
Map<String, List<String>> headers
) {
this.parsecAsyncHttpClient = client;
this.objectMapper = objectMapper;
this.url = url;
this.headers = headers;
this.defaultHeaders = headers;
}
private ParsecAsyncHttpRequest getRequest(String method, URI uri, String body) throws ResourceException {
private ParsecAsyncHttpRequest getRequest(
String method,
Map<String, List<String>> headers,
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());
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
String headerKey = entry.getKey();
for (String headerValue: entry.getValue()) {
builder.addHeader(headerKey, headerValue);
}
}
}
Expand All @@ -318,9 +340,18 @@ public class {{cName}}ClientImpl implements {{cName}}Client {
}
return request;
}
public Map<String, List<String>> getDefaultHeaders() {
return defaultHeaders;
}
{{range .Resources}}
@Override
{{methodSig .}} {
{{ContentOfNoHeaderMethod .}}
}
@Override
{{methodSigWithHeader .}} {
String path = "{{.Path}}";
String body = null;
{{if needBody .}}
Expand All @@ -332,7 +363,10 @@ public class {{cName}}ClientImpl implements {{cName}}Client {
}
{{end}}
URI uri = UriBuilder.fromUri(url).path(path){{builderExt .}}
ParsecAsyncHttpRequest request = getRequest("{{.Method}}", uri, body);
if (headers == null) {
headers = getDefaultHeaders();
}
ParsecAsyncHttpRequest request = getRequest("{{.Method}}", headers, uri, body);
{{if needExpect .}}
Set<Integer> expectedStatus = new HashSet<>();
Expand All @@ -356,7 +390,7 @@ func safeTypeVarName(rtype rdl.TypeRef) rdl.TypeName {
}

// todo: duplicate with server code, need integrate
func javaMethodName(reg rdl.TypeRegistry, r *rdl.Resource) (string, []string) {
func javaMethodName(reg rdl.TypeRegistry, r *rdl.Resource, needParamWithType bool) (string, []string) {
var params []string
bodyType := string(safeTypeVarName(r.Type))
for _, v := range r.Inputs {
Expand All @@ -369,7 +403,11 @@ func javaMethodName(reg rdl.TypeRegistry, r *rdl.Resource) (string, []string) {
bodyType = string(safeTypeVarName(v.Type))
}
optional := false // but different with server code, how?
params = append(params, utils.JavaType(reg, v.Type, optional, "", "")+" "+javaName(k))
if (needParamWithType) {
params = append(params, utils.JavaType(reg, v.Type, optional, "", "") + " " + javaName(k))
} else {
params = append(params, javaName(k))
}
}
return strings.ToLower(string(r.Method)) + string(bodyType), params
}
Expand All @@ -384,22 +422,31 @@ func javaName(name rdl.Identifier) string {
}
}

func (gen *javaClientGenerator) clientMethodSignature(r *rdl.Resource) string {
func (gen *javaClientGenerator) clientMethodSignature(r *rdl.Resource, needHeader bool) string {
reg := gen.registry
returnType := utils.JavaType(reg, r.Type, true, "", "")
methName, params := javaMethodName(reg, r)
needParamWithType := true
methName, params := javaMethodName(reg, r, needParamWithType)
sparams := ""
if len(params) > 0 {
sparams = strings.Join(params, ", ")
if (needHeader) {
sparams = "Map<String, List<String>> headers"
}
if len(r.Outputs) > 0 {
if sparams == "" {
sparams = "java.util.Map<String,java.util.List<String>> headers"
} else {
sparams = sparams + ", java.util.Map<String,java.util.List<String>> headers"
if len(params) > 0 {
if (sparams != "") {
sparams = sparams + ", "
}
sparams = sparams + strings.Join(params, ", ")
}
return "CompletableFuture<" + returnType + "> " + methName + "(" + sparams + ") throws ResourceException"
}


func (gen *javaClientGenerator) clientMethodOverloadContent(r *rdl.Resource) string {
reg := gen.registry
needParamWithType := false
methName, params := javaMethodName(reg, r, needParamWithType)
paramsWithNull := "null"
if len(params) > 0 {
paramsWithNull = paramsWithNull + ", " + strings.Join(params, ", ")
}
return "return " + methName + "(" + paramsWithNull + ");"
}
66 changes: 57 additions & 9 deletions testdata/Sample2ClientImpl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import org.slf4j.LoggerFactory;
import javax.ws.rs.core.UriBuilder;
import java.net.URI;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
Expand All @@ -39,7 +40,7 @@ public class SampleClientImpl implements SampleClient {
private String url;

/** Headers. */
private final Map<String, String> headers;
private final Map<String, List<String>> defaultHeaders;

/**
* connection timeout.
Expand All @@ -51,9 +52,13 @@ public class SampleClientImpl implements SampleClient {
*/
private static final int MAXIMUM_CONNECTIONS_TOTAL = 50;

public SampleClientImpl(String url) {
this(url, null);
}

public SampleClientImpl(
String url,
Map<String, String> headers
Map<String, List<String>> headers
) {

ParsecAsyncHttpClient client = null;
Expand All @@ -71,28 +76,36 @@ public class SampleClientImpl implements SampleClient {
this.parsecAsyncHttpClient = client;
this.objectMapper = new ObjectMapper();
this.url = url;
this.headers = headers;
this.defaultHeaders = headers;
}

public SampleClientImpl(
ParsecAsyncHttpClient client,
ObjectMapper objectMapper,
String url,
Map<String, String> headers
Map<String, List<String>> headers
) {
this.parsecAsyncHttpClient = client;
this.objectMapper = objectMapper;
this.url = url;
this.headers = headers;
this.defaultHeaders = headers;
}

private ParsecAsyncHttpRequest getRequest(String method, URI uri, String body) throws ResourceException {
private ParsecAsyncHttpRequest getRequest(
String method,
Map<String, List<String>> headers,
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());
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
String headerKey = entry.getKey();
for (String headerValue: entry.getValue()) {
builder.addHeader(headerKey, headerValue);
}
}
}

Expand All @@ -110,15 +123,50 @@ public class SampleClientImpl implements SampleClient {
return request;
}

public Map<String, List<String>> getDefaultHeaders() {
return defaultHeaders;
}

@Override
public CompletableFuture<User> getUser(int id) throws ResourceException {
return getUser(null, id);
}

@Override
public CompletableFuture<User> getUser(Map<String, List<String>> headers, 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);
if (headers == null) {
headers = getDefaultHeaders();
}
ParsecAsyncHttpRequest request = getRequest("GET", headers, uri, body);


AsyncHandler<User> asyncHandler = new DefaultAsyncCompletionHandler<>(User.class);

return parsecAsyncHttpClient.criticalExecute(request, asyncHandler);
}

@Override
public CompletableFuture<User> postUser() throws ResourceException {
return postUser(null);
}

@Override
public CompletableFuture<User> postUser(Map<String, List<String>> headers) throws ResourceException {
String path = "/wssid";
String body = null;

URI uri = UriBuilder.fromUri(url).path(path)
.build();
if (headers == null) {
headers = getDefaultHeaders();
}
ParsecAsyncHttpRequest request = getRequest("POST", headers, uri, body);


AsyncHandler<User> asyncHandler = new DefaultAsyncCompletionHandler<>(User.class);
Expand Down
7 changes: 7 additions & 0 deletions testdata/SampleClient.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
//
package com.example.parsec_generated;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import com.example.parsec_generated.ResourceException;
import com.example.parsec_generated.User;
Expand All @@ -13,8 +15,13 @@ import com.example.parsec_generated.Users;
public interface SampleClient {

CompletableFuture<User> getUser(int id) throws ResourceException;
CompletableFuture<User> getUser(Map<String, List<String>> headers, int id) throws ResourceException;
CompletableFuture<User> postUser(User user) throws ResourceException;
CompletableFuture<User> postUser(Map<String, List<String>> headers, User user) throws ResourceException;
CompletableFuture<User> putUser(int id, User user) throws ResourceException;
CompletableFuture<User> putUser(Map<String, List<String>> headers, int id, User user) throws ResourceException;
CompletableFuture<User> deleteUser(int id) throws ResourceException;
CompletableFuture<User> deleteUser(Map<String, List<String>> headers, int id) throws ResourceException;
CompletableFuture<Users> getUsers(String ids) throws ResourceException;
CompletableFuture<Users> getUsers(Map<String, List<String>> headers, String ids) throws ResourceException;
}
Loading

0 comments on commit ddd7121

Please sign in to comment.