Skip to content

Commit

Permalink
add query parameter support
Browse files Browse the repository at this point in the history
  • Loading branch information
hehe.thh committed Apr 16, 2018
1 parent 5a1d11f commit 703c06c
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 16 deletions.
14 changes: 4 additions & 10 deletions src/main/java/com/aliyuncs/fc/client/FunctionComputeClient.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
package com.aliyuncs.fc.client;

import com.aliyuncs.fc.constants.Const;
import com.aliyuncs.fc.constants.HeaderKeys;
import com.aliyuncs.fc.request.*;
import com.aliyuncs.fc.config.Config;
import com.aliyuncs.fc.constants.HeaderKeys;
import com.aliyuncs.fc.exceptions.ClientException;
import com.aliyuncs.fc.exceptions.ServerException;
import com.aliyuncs.fc.http.HttpResponse;
import com.aliyuncs.fc.model.FunctionMetadata;
import com.aliyuncs.fc.model.FunctionCodeMetadata;
import com.aliyuncs.fc.model.FunctionMetadata;
import com.aliyuncs.fc.model.ServiceMetadata;
import com.aliyuncs.fc.model.TriggerMetadata;
import com.aliyuncs.fc.request.*;
import com.aliyuncs.fc.response.*;
import com.aliyuncs.fc.utils.Base64Helper;
import com.google.common.base.Preconditions;
import com.google.gson.Gson;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Map;
import java.util.logging.Logger;

import static com.aliyuncs.fc.constants.Const.HTTP_INVOKE_FUNCTION_PATH;
import static com.aliyuncs.fc.constants.Const.INVOCATION_TYPE_HTTP;
import static com.google.common.base.Strings.isNullOrEmpty;
import static com.google.common.base.Strings.nullToEmpty;

/**
* TODO: add javadoc
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import com.aliyuncs.fc.constants.Const;
import com.aliyuncs.fc.constants.HeaderKeys;
import com.aliyuncs.fc.utils.UriBuilder;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;

import static com.aliyuncs.fc.constants.Const.INVOCATION_TYPE_HTTP;
Expand All @@ -13,11 +16,10 @@
*/
public class HttpInvokeFunctionRequest extends InvokeFunctionRequest {

private final String path;
private final UriBuilder uriBuilder;

private final String method;


private final String authType;

public HttpInvokeFunctionRequest(String serviceName, String functionName, String authType, String method) {
Expand All @@ -27,15 +29,30 @@ public HttpInvokeFunctionRequest(String serviceName, String functionName, String
public HttpInvokeFunctionRequest(String serviceName, String functionName, String authType, String method, String path) {
super(serviceName, functionName);
this.setInvocationType(Const.INVOCATION_TYPE_HTTP);
this.path = path.startsWith("/") ? path.substring(1) : path;

try {
uriBuilder = UriBuilder.fromUri(
new URI(format(Const.HTTP_INVOKE_FUNCTION_PATH,
Const.API_VERSION,
getServiceName(),
getFunctionName(),
path.startsWith("/") ? path.substring(1) : path)));
} catch (URISyntaxException e) {
throw new RuntimeException("path is not valid.", e);
}

this.authType = authType;
this.method = method;
}

public void addQuery(String name, String value) {
uriBuilder.queryParam(name, value);
}

@Override
public String getPath() {
return format(Const.HTTP_INVOKE_FUNCTION_PATH, Const.API_VERSION, getServiceName(), getFunctionName(), path);
}
return uriBuilder.build();
}

public String getMethod() {
return method;
Expand Down
71 changes: 71 additions & 0 deletions src/main/java/com/aliyuncs/fc/utils/UriBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.aliyuncs.fc.utils;

import java.net.URI;

import static com.google.common.base.Strings.isNullOrEmpty;

public class UriBuilder {
private String scheme;
private String host;
private int port;
private String path;
private StringBuilder query;
private String fragment;

private UriBuilder(URI uri) {
this(uri.getScheme(), uri.getHost(), uri.getPort(), uri.getPath(), uri.getQuery(), uri.getFragment());
}

private UriBuilder(String scheme, String host, int port, String path, String query, String fragment) {
this.scheme = scheme;
this.host = host;
this.port = port;
this.path = path;
this.query = query == null ? new StringBuilder() : new StringBuilder(query);
this.fragment = fragment;
}

public static UriBuilder fromUri(URI uri) {
return new UriBuilder(uri);
}

public UriBuilder queryParam(String name, String value) {
if (query.length() != 0) {
query.append("&");
}

query.append(name).append("=").append(value);

return this;
}

public String build() {
StringBuilder builder = new StringBuilder();

if ( ! isNullOrEmpty(scheme) ) {
builder.append(scheme).append("://");
}

if ( ! isNullOrEmpty(host) ) {
builder.append(host);
}

if ( port != -1 ) {
builder.append(":").append(port);
}

if ( ! isNullOrEmpty(path) ) {
builder.append(path);
}

if ( query.length() != 0 ) {
builder.append("?").append(query.toString());
}

if ( ! isNullOrEmpty(fragment) ) {
builder.append("#").append(fragment);
}

return builder.toString();
}
}
5 changes: 4 additions & 1 deletion src/test/java/com/aliyuncs/fc/FunctionComputeClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1090,8 +1090,11 @@ public void testHttpInvokeFunction() throws IOException {
createHttpTrigger(TRIGGER_NAME, auth, new String[] {"GET", "POST"});

// Invoke the function
// HttpInvokeFunctionRequest request = new HttpInvokeFunctionRequest(SERVICE_NAME, FUNCTION_NAME, auth, "POST", "/test/path?a=1&b=2"); // todo: wait for bug fix
HttpInvokeFunctionRequest request = new HttpInvokeFunctionRequest(SERVICE_NAME, FUNCTION_NAME, auth, "POST", "/test/path");

// request.addQuery("a", "1");// todo: wait for bug fix
// request.addQuery("b", "2");

request.setHeader("Test-Header-Key", "testHeaderValue");
request.setHeader("Content-Type", "application/json");

Expand Down
43 changes: 43 additions & 0 deletions src/test/java/com/aliyuncs/fc/utils/UriBuilderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.aliyuncs.fc.utils;

import org.junit.Test;

import java.net.URI;
import java.net.URISyntaxException;

import static org.junit.Assert.*;

public class UriBuilderTest {

@Test
public void testBuildUri() throws URISyntaxException {
UriBuilder uriBuilder = UriBuilder.fromUri(new URI("http://www.test.com"));

assertEquals("http://www.test.com", uriBuilder.build());

uriBuilder = UriBuilder.fromUri(new URI("www.test.com"));

assertEquals("www.test.com", uriBuilder.build());

uriBuilder = UriBuilder.fromUri(new URI("http://123.fc.shanghai.aliyuncs.com:90/version/proxy/service/function/action/action?p=1&v=2#123"));

assertEquals("http://123.fc.shanghai.aliyuncs.com:90/version/proxy/service/function/action/action?p=1&v=2#123", uriBuilder.build());

uriBuilder = UriBuilder.fromUri(new URI("http://123.fc.shanghai.aliyuncs.com:90/version/proxy/service/function/action/action?p=1&v=2#123"))
.queryParam("testParam1", "testValue1")
.queryParam("testParam2", "testValue2");

assertEquals("http://123.fc.shanghai.aliyuncs.com:90/version/proxy/service/function/action/action?p=1&v=2&testParam1=testValue1&testParam2=testValue2#123", uriBuilder.build());


uriBuilder = UriBuilder.fromUri(new URI("http://123.fc.shanghai.aliyuncs.com/version/proxy/service/function/action/action#123"))
.queryParam("testParam1", "testValue1")
.queryParam("testParam2", "testValue2");

assertEquals("http://123.fc.shanghai.aliyuncs.com/version/proxy/service/function/action/action?testParam1=testValue1&testParam2=testValue2#123", uriBuilder.build());

uriBuilder = UriBuilder.fromUri(new URI("http://123.fc.shanghai.aliyuncs.com/version/proxy/service/function/action/action"));

assertEquals("http://123.fc.shanghai.aliyuncs.com/version/proxy/service/function/action/action", uriBuilder.build());
}
}

0 comments on commit 703c06c

Please sign in to comment.