Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions gen/src/main/java/com/softlayer/api/gen/ClassWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,17 @@ public ClassWriter emitType() throws IOException {
// Now the service
if (!type.meta.noservice) {
if (type.meta.properties.containsKey("id")) {
beginMethod("Service", "asService", PUBLIC, TYPE_API_CLIENT, "client").
emitStatement("return service(client, id)").endMethod().emitEmptyLine();
if (type.meta.properties.containsKey("globalIdentifier")) {
beginMethod("Service", "asService", PUBLIC, TYPE_API_CLIENT, "client").
beginControlFlow("if (id != null)").
emitStatement("return service(client, id)").
nextControlFlow("else").
emitStatement("return service(client, globalIdentifier)").
endControlFlow().endMethod().emitEmptyLine();
} else {
beginMethod("Service", "asService", PUBLIC, TYPE_API_CLIENT, "client").
emitStatement("return service(client, id)").endMethod().emitEmptyLine();
}
}

beginMethod("Service", "service", PUBLIC_STATIC, TYPE_API_CLIENT, "client").
Expand All @@ -388,8 +397,14 @@ public ClassWriter emitType() throws IOException {

if (type.meta.properties.containsKey("id")) {
beginMethod("Service", "service", PUBLIC_STATIC, TYPE_API_CLIENT, "client", "Long", "id").
emitStatement("return client.createService(Service.class, id)").
emitStatement("return client.createService(Service.class, id == null ? null : id.toString())").
endMethod().emitEmptyLine();
if (type.meta.properties.containsKey("globalIdentifier")) {
beginMethod("Service", "service", PUBLIC_STATIC, TYPE_API_CLIENT,
"client", "String", "globalIdentifier").
emitStatement("return client.createService(Service.class, globalIdentifier)").
endMethod().emitEmptyLine();
}
}

emitService();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/softlayer/api/ApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ public interface ApiClient {
* directly, but rather invoke the service method on the type class.
* E.g. {@link com.softlayer.api.service.Account#service(ApiClient)}.
*/
public <S extends Service> S createService(Class<S> serviceClass, Long id);
public <S extends Service> S createService(Class<S> serviceClass, String id);
}
12 changes: 6 additions & 6 deletions src/main/java/com/softlayer/api/RestApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ protected String getHttpMethodFromMethodName(String methodName) {
}
}

protected String getFullUrl(String serviceName, String methodName, Long id,
protected String getFullUrl(String serviceName, String methodName, String id,
ResultLimit resultLimit, String maskString) {
StringBuilder url = new StringBuilder(baseUrl + serviceName);
// ID present? add it
Expand Down Expand Up @@ -188,21 +188,21 @@ protected void logResponse(String url, int statusCode, String body) {

@Override
@SuppressWarnings("unchecked")
public <S extends Service> S createService(Class<S> serviceClass, Long id) {
public <S extends Service> S createService(Class<S> serviceClass, String id) {
return (S) Proxy.newProxyInstance(getClass().getClassLoader(),
new Class<?>[] { serviceClass }, new ServiceProxy<S>(serviceClass, id));
}

class ServiceProxy<S extends Service> implements InvocationHandler {

final Class<S> serviceClass;
final Long id;
final String id;
Mask mask;
String maskString;
ResultLimit resultLimit;
Integer lastResponseTotalItemCount;

public ServiceProxy(Class<S> serviceClass, Long id) {
public ServiceProxy(Class<S> serviceClass, String id) {
this.serviceClass = serviceClass;
this.id = id;
}
Expand Down Expand Up @@ -283,7 +283,7 @@ public Object invokeService(Method method, final Object[] args) throws Throwable
}
String methodName = methodInfo.value().isEmpty() ? method.getName() : methodInfo.value();
final String httpMethod = getHttpMethodFromMethodName(methodName);
Long methodId = methodInfo.instanceRequired() ? this.id : null;
String methodId = methodInfo.instanceRequired() ? this.id : null;
final String url = getFullUrl(serviceClass.getAnnotation(ApiService.class).value(),
methodName, methodId, resultLimit, mask == null ? maskString : mask.getMask());
final HttpClient client = getHttpClientFactory().getHttpClient(credentials, httpMethod, url, HEADERS);
Expand Down Expand Up @@ -322,7 +322,7 @@ public Object invokeServiceAsync(final Method asyncMethod, final Object[] args)
}
String methodName = methodInfo.value().isEmpty() ? method.getName() : methodInfo.value();
final String httpMethod = getHttpMethodFromMethodName(methodName);
Long methodId = methodInfo.instanceRequired() ? this.id : null;
String methodId = methodInfo.instanceRequired() ? this.id : null;
final String url = getFullUrl(serviceClass.getAnnotation(ApiService.class).value(),
methodName, methodId, resultLimit, mask == null ? maskString : mask.getMask());
final HttpClient client = getHttpClientFactory().getHttpClient(credentials, httpMethod, url, HEADERS);
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/com/softlayer/api/RestApiClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ public void testGetFullUrl() {
assertEquals("http://example.com/SomeService/someMethod.json",
client.getFullUrl("SomeService", "someMethod", null, null, null));
assertEquals("http://example.com/SomeService/1234/someMethod.json",
client.getFullUrl("SomeService", "someMethod", 1234L, null, null));
client.getFullUrl("SomeService", "someMethod", "1234", null, null));
assertEquals("http://example.com/SomeService/1234/someMethod.json?resultLimit=5,6",
client.getFullUrl("SomeService", "someMethod", 1234L, new ResultLimit(5, 6), null));
client.getFullUrl("SomeService", "someMethod", "1234", new ResultLimit(5, 6), null));
assertEquals("http://example.com/SomeService/1234/someMethod.json?resultLimit=5,6&objectMask=someMask%26%26",
client.getFullUrl("SomeService", "someMethod", 1234L, new ResultLimit(5, 6), "someMask&&"));
client.getFullUrl("SomeService", "someMethod", "1234", new ResultLimit(5, 6), "someMask&&"));
assertEquals("http://example.com/SomeService/1234/someMethod.json?objectMask=someMask%26%26",
client.getFullUrl("SomeService", "someMethod", 1234L, null, "someMask&&"));
client.getFullUrl("SomeService", "someMethod", "1234", null, "someMask&&"));
assertEquals("http://example.com/SomeService/Something.json",
client.getFullUrl("SomeService", "getSomething", null, null, null));
assertEquals("http://example.com/SomeService.json",
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/softlayer/api/service/TestEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public static Service service(ApiClient client) {
}

public static Service service(ApiClient client, Long id) {
return client.createService(Service.class, id);
return client.createService(Service.class, id == null ? null : id.toString());
}

@ApiService("SoftLayer_TestEntity")
Expand Down