Invoking an API through client application

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Invoking an API through client application

Isuru Uyanage
Hi All, 

I want to invoke an API(created through a SOAP Endpoint) using the SDK (generated from WSO2 API Manager) in a Java class. I did following steps. 

1. A simple SOAP service is created which takes height(double) and weight(double) as inputs and outputs the BMI(formatted string) as the result.

2. An API is created through SOAP Endpoint by providing the WSDL of the web service. 
API was invoked through SOAP UI and it outputs the correct result. 

3. SDK for Java is generated through API Manager. 
4. Built the SDK using Maven. 
5. Included following dependency in my own maven project.
<dependency>
<groupId>org.wso2</groupId>
<artifactId>org.wso2.client.api.BmiMeter</artifactId>
<version>1.0.0</version>
<scope>compile</scope>
</dependency> 
5. Manually installed following jars to the maven project. 
  • org.wso2.client.BmiMeter-1.0.0-tests.jar
  • jars in the target>lib folder
6. Created a java classs in the Maven project and tried to invoke the api. 

public class Main {
public static void main(String[] args) throws ApiException {
DefaultApi defaultApi = new DefaultApi();
String accessToken = "a92a1d4f-ac2a-398f-80b0-49f79b5b4de1";

Map<String, String> headers = new HashMap<String, String>();
headers.put("Accept", "application/json");
headers.put("Authorization", "Bearer " + accessToken);
ApiClient apiClient = defaultApi.getApiClient();
apiClient.addDefaultHeader("Accept", "application/json");

apiClient.addDefaultHeader("Authorization", "Bearer " + accessToken);
apiClient.setLenientOnJson(true);
apiClient.setBasePath("http://10.100.5.136:8280/bmimeter/1.0.0");
defaultApi.setApiClient(apiClient);

}

 7. Since I created the API through SOAP Endpoint, my default method is a POST which I need to send a SOAP request as below.



Once I tried to invoke it through the code, (defaultApi.post) method, it returns a void. 

My question is how to invoke this kind of an API through a SDK in Java code. Your thoughts about this are highly appreciated. Please correct me if I have gone wrong in above steps. 




Thanks and Best Regards,

Isuru Uyanage
Software Engineer - QA | WSO2
Mobile : <a href="tel:+94%2077%20767%201807" value="+94777671807" style="color:rgb(17,85,204)" target="_blank">+94 77 55 30752




_______________________________________________
Dev mailing list
[hidden email]
http://wso2.org/cgi-bin/mailman/listinfo/dev
Reply | Threaded
Open this post in threaded view
|

Re: Invoking an API through client application

Chamin Dias
Hi Isuru,

You can find a sample in this article [1].

It has all the steps for this kind of scenario.


Thanks.

On Tue, Nov 28, 2017 at 2:48 PM, Isuru Uyanage <[hidden email]> wrote:
Hi All, 

I want to invoke an API(created through a SOAP Endpoint) using the SDK (generated from WSO2 API Manager) in a Java class. I did following steps. 

1. A simple SOAP service is created which takes height(double) and weight(double) as inputs and outputs the BMI(formatted string) as the result.

2. An API is created through SOAP Endpoint by providing the WSDL of the web service. 
API was invoked through SOAP UI and it outputs the correct result. 

3. SDK for Java is generated through API Manager. 
4. Built the SDK using Maven. 
5. Included following dependency in my own maven project.
<dependency>
<groupId>org.wso2</groupId>
<artifactId>org.wso2.client.api.BmiMeter</artifactId>
<version>1.0.0</version>
<scope>compile</scope>
</dependency> 
5. Manually installed following jars to the maven project. 
  • org.wso2.client.BmiMeter-1.0.0-tests.jar
  • jars in the target>lib folder
6. Created a java classs in the Maven project and tried to invoke the api. 

public class Main {
public static void main(String[] args) throws ApiException {
DefaultApi defaultApi = new DefaultApi();
String accessToken = "a92a1d4f-ac2a-398f-80b0-49f79b5b4de1";

Map<String, String> headers = new HashMap<String, String>();
headers.put("Accept", "application/json");
headers.put("Authorization", "Bearer " + accessToken);
ApiClient apiClient = defaultApi.getApiClient();
apiClient.addDefaultHeader("Accept", "application/json");

apiClient.addDefaultHeader("Authorization", "Bearer " + accessToken);
apiClient.setLenientOnJson(true);
apiClient.setBasePath("http://10.100.5.136:8280/bmimeter/1.0.0");
defaultApi.setApiClient(apiClient);

}

 7. Since I created the API through SOAP Endpoint, my default method is a POST which I need to send a SOAP request as below.



Once I tried to invoke it through the code, (defaultApi.post) method, it returns a void. 

My question is how to invoke this kind of an API through a SDK in Java code. Your thoughts about this are highly appreciated. Please correct me if I have gone wrong in above steps. 




Thanks and Best Regards,

Isuru Uyanage
Software Engineer - QA | WSO2
Mobile : <a href="tel:+94%2077%20767%201807" value="+94777671807" style="color:rgb(17,85,204)" target="_blank">+94 77 55 30752




_______________________________________________
Dev mailing list
[hidden email]
http://wso2.org/cgi-bin/mailman/listinfo/dev




--
Chamin Dias
Mobile : 0716097455


_______________________________________________
Dev mailing list
[hidden email]
http://wso2.org/cgi-bin/mailman/listinfo/dev
Reply | Threaded
Open this post in threaded view
|

Re: Invoking an API through client application

Fazlan Nazeem
In reply to this post by Isuru Uyanage

Hi Isuru,

On Tue, Nov 28, 2017 at 2:48 PM, Isuru Uyanage <[hidden email]> wrote:
Hi All, 

I want to invoke an API(created through a SOAP Endpoint) using the SDK (generated from WSO2 API Manager) in a Java class. I did following steps. 

1. A simple SOAP service is created which takes height(double) and weight(double) as inputs and outputs the BMI(formatted string) as the result.

2. An API is created through SOAP Endpoint by providing the WSDL of the web service. 
API was invoked through SOAP UI and it outputs the correct result. 

3. SDK for Java is generated through API Manager. 
4. Built the SDK using Maven. 
5. Included following dependency in my own maven project.
<dependency>
<groupId>org.wso2</groupId>
<artifactId>org.wso2.client.api.BmiMeter</artifactId>
<version>1.0.0</version>
<scope>compile</scope>
</dependency> 
5. Manually installed following jars to the maven project. 
  • org.wso2.client.BmiMeter-1.0.0-tests.jar
  • jars in the target>lib folder
6. Created a java classs in the Maven project and tried to invoke the api. 

public class Main {
public static void main(String[] args) throws ApiException {
DefaultApi defaultApi = new DefaultApi();
String accessToken = "a92a1d4f-ac2a-398f-80b0-49f79b5b4de1";

Map<String, String> headers = new HashMap<String, String>();
headers.put("Accept", "application/json");
headers.put("Authorization", "Bearer " + accessToken);
ApiClient apiClient = defaultApi.getApiClient();
apiClient.addDefaultHeader("Accept", "application/json");

apiClient.addDefaultHeader("Authorization", "Bearer " + accessToken);
apiClient.setLenientOnJson(true);
apiClient.setBasePath("http://10.100.5.136:8280/bmimeter/1.0.0");
defaultApi.setApiClient(apiClient);

}
Isn't this code snippet missing the actual post call? I do not see where it is invoked. 

 7. Since I created the API through SOAP Endpoint, my default method is a POST which I need to send a SOAP request as below.



Once I tried to invoke it through the code, (defaultApi.post) method, it returns a void. 

My question is how to invoke this kind of an API through a SDK in Java code. Your thoughts about this are highly appreciated. Please correct me if I have gone wrong in above steps. 




Thanks and Best Regards,

Isuru Uyanage
Software Engineer - QA | WSO2
Mobile : <a href="tel:+94%2077%20767%201807" value="+94777671807" style="color:rgb(17,85,204)" target="_blank">+94 77 55 30752




_______________________________________________
Dev mailing list
[hidden email]
http://wso2.org/cgi-bin/mailman/listinfo/dev




--
Thanks & Regards,

Fazlan Nazeem
Senior Software Engineer
WSO2 Inc
Mobile : <a href="tel:%2B94%20%280%29%20773%20451194" value="+94773451194" target="_blank">+94772338839

_______________________________________________
Dev mailing list
[hidden email]
http://wso2.org/cgi-bin/mailman/listinfo/dev
Reply | Threaded
Open this post in threaded view
|

Re: Invoking an API through client application

Isuru Uyanage
Hi Fazlan, 
Yes, I did not include the post call in above example since it does return a void. It is as below. 
defaultApi.post(Stirng soapRequest, String soapAction);

Thanks and Best Regards,

Isuru Uyanage
Software Engineer - QA | WSO2
Mobile : <a href="tel:+94%2077%20767%201807" value="+94777671807" style="color:rgb(17,85,204)" target="_blank">+94 77 55 30752




On Tue, Nov 28, 2017 at 4:41 PM, Fazlan Nazeem <[hidden email]> wrote:

Hi Isuru,

On Tue, Nov 28, 2017 at 2:48 PM, Isuru Uyanage <[hidden email]> wrote:
Hi All, 

I want to invoke an API(created through a SOAP Endpoint) using the SDK (generated from WSO2 API Manager) in a Java class. I did following steps. 

1. A simple SOAP service is created which takes height(double) and weight(double) as inputs and outputs the BMI(formatted string) as the result.

2. An API is created through SOAP Endpoint by providing the WSDL of the web service. 
API was invoked through SOAP UI and it outputs the correct result. 

3. SDK for Java is generated through API Manager. 
4. Built the SDK using Maven. 
5. Included following dependency in my own maven project.
<dependency>
<groupId>org.wso2</groupId>
<artifactId>org.wso2.client.api.BmiMeter</artifactId>
<version>1.0.0</version>
<scope>compile</scope>
</dependency> 
5. Manually installed following jars to the maven project. 
  • org.wso2.client.BmiMeter-1.0.0-tests.jar
  • jars in the target>lib folder
6. Created a java classs in the Maven project and tried to invoke the api. 

public class Main {
public static void main(String[] args) throws ApiException {
DefaultApi defaultApi = new DefaultApi();
String accessToken = "a92a1d4f-ac2a-398f-80b0-49f79b5b4de1";

Map<String, String> headers = new HashMap<String, String>();
headers.put("Accept", "application/json");
headers.put("Authorization", "Bearer " + accessToken);
ApiClient apiClient = defaultApi.getApiClient();
apiClient.addDefaultHeader("Accept", "application/json");

apiClient.addDefaultHeader("Authorization", "Bearer " + accessToken);
apiClient.setLenientOnJson(true);
apiClient.setBasePath("http://10.100.5.136:8280/bmimeter/1.0.0");
defaultApi.setApiClient(apiClient);

}
Isn't this code snippet missing the actual post call? I do not see where it is invoked. 

 7. Since I created the API through SOAP Endpoint, my default method is a POST which I need to send a SOAP request as below.



Once I tried to invoke it through the code, (defaultApi.post) method, it returns a void. 

My question is how to invoke this kind of an API through a SDK in Java code. Your thoughts about this are highly appreciated. Please correct me if I have gone wrong in above steps. 




Thanks and Best Regards,

Isuru Uyanage
Software Engineer - QA | WSO2
Mobile : <a href="tel:+94%2077%20767%201807" value="+94777671807" style="color:rgb(17,85,204)" target="_blank">+94 77 55 30752




_______________________________________________
Dev mailing list
[hidden email]
http://wso2.org/cgi-bin/mailman/listinfo/dev




--
Thanks & Regards,

Fazlan Nazeem
Senior Software Engineer
WSO2 Inc
Mobile : <a href="tel:%2B94%20%280%29%20773%20451194" value="+94773451194" target="_blank">+94772338839


_______________________________________________
Dev mailing list
[hidden email]
http://wso2.org/cgi-bin/mailman/listinfo/dev
Reply | Threaded
Open this post in threaded view
|

Re: Invoking an API through client application

Malintha Amarasinghe
Hi,

The code is generated from the swagger generated for the SOAP API (Not sure we tested something like this before.) Maybe some response related schema missing from the swagger which causes the code generator to generate a "void" method.

Can you attach the swagger file of the particular API generated via APIM?

Thanks!



On Tue, Nov 28, 2017 at 4:48 PM, Isuru Uyanage <[hidden email]> wrote:
Hi Fazlan, 
Yes, I did not include the post call in above example since it does return a void. It is as below. 
defaultApi.post(Stirng soapRequest, String soapAction);

Thanks and Best Regards,

Isuru Uyanage
Software Engineer - QA | WSO2
Mobile : <a href="tel:+94%2077%20767%201807" value="+94777671807" style="color:rgb(17,85,204)" target="_blank">+94 77 55 30752




On Tue, Nov 28, 2017 at 4:41 PM, Fazlan Nazeem <[hidden email]> wrote:

Hi Isuru,

On Tue, Nov 28, 2017 at 2:48 PM, Isuru Uyanage <[hidden email]> wrote:
Hi All, 

I want to invoke an API(created through a SOAP Endpoint) using the SDK (generated from WSO2 API Manager) in a Java class. I did following steps. 

1. A simple SOAP service is created which takes height(double) and weight(double) as inputs and outputs the BMI(formatted string) as the result.

2. An API is created through SOAP Endpoint by providing the WSDL of the web service. 
API was invoked through SOAP UI and it outputs the correct result. 

3. SDK for Java is generated through API Manager. 
4. Built the SDK using Maven. 
5. Included following dependency in my own maven project.
<dependency>
<groupId>org.wso2</groupId>
<artifactId>org.wso2.client.api.BmiMeter</artifactId>
<version>1.0.0</version>
<scope>compile</scope>
</dependency> 
5. Manually installed following jars to the maven project. 
  • org.wso2.client.BmiMeter-1.0.0-tests.jar
  • jars in the target>lib folder
6. Created a java classs in the Maven project and tried to invoke the api. 

public class Main {
public static void main(String[] args) throws ApiException {
DefaultApi defaultApi = new DefaultApi();
String accessToken = "a92a1d4f-ac2a-398f-80b0-49f79b5b4de1";

Map<String, String> headers = new HashMap<String, String>();
headers.put("Accept", "application/json");
headers.put("Authorization", "Bearer " + accessToken);
ApiClient apiClient = defaultApi.getApiClient();
apiClient.addDefaultHeader("Accept", "application/json");

apiClient.addDefaultHeader("Authorization", "Bearer " + accessToken);
apiClient.setLenientOnJson(true);
apiClient.setBasePath("http://10.100.5.136:8280/bmimeter/1.0.0");
defaultApi.setApiClient(apiClient);

}
Isn't this code snippet missing the actual post call? I do not see where it is invoked. 

 7. Since I created the API through SOAP Endpoint, my default method is a POST which I need to send a SOAP request as below.



Once I tried to invoke it through the code, (defaultApi.post) method, it returns a void. 

My question is how to invoke this kind of an API through a SDK in Java code. Your thoughts about this are highly appreciated. Please correct me if I have gone wrong in above steps. 




Thanks and Best Regards,

Isuru Uyanage
Software Engineer - QA | WSO2
Mobile : <a href="tel:+94%2077%20767%201807" value="+94777671807" style="color:rgb(17,85,204)" target="_blank">+94 77 55 30752




_______________________________________________
Dev mailing list
[hidden email]
http://wso2.org/cgi-bin/mailman/listinfo/dev




--
Thanks & Regards,

Fazlan Nazeem
Senior Software Engineer
WSO2 Inc
Mobile : <a href="tel:%2B94%20%280%29%20773%20451194" value="+94773451194" target="_blank">+94772338839


_______________________________________________
Dev mailing list
[hidden email]
http://wso2.org/cgi-bin/mailman/listinfo/dev




--
Malintha Amarasinghe
WSO2, Inc. - lean | enterprise | middleware

Mobile : +94 712383306

_______________________________________________
Dev mailing list
[hidden email]
http://wso2.org/cgi-bin/mailman/listinfo/dev
Reply | Threaded
Open this post in threaded view
|

Re: Invoking an API through client application

Viduranga Gunarathne
Hi Isuru,

Please find a sample code of a java SDK that was generated with API-M and note the authentication scheme that is applied based on the scope of the resource.
import org.wso2.client.api.*;
import org.wso2.client.api.auth.*;
import org.wso2.client.api.model.*;
import org.wso2.client.api.SwaggerPetstore.PetApi;

import java.io.File;
import java.util.*;

public class PetApiExample {

public static void main(String[] args) {
ApiClient defaultClient = Configuration.getDefaultApiClient();

// Configure OAuth2 access token for authorization: petstore_auth
OAuth petstore_auth = (OAuth) defaultClient.getAuthentication("petstore_auth");
petstore_auth.setAccessToken("YOUR ACCESS TOKEN");

PetApi apiInstance = new PetApi();
Pet body = new Pet(); // Pet | Pet object that needs to be added to the store
try {
apiInstance.addPet(body);
} catch (ApiException e) {
System.err.println("Exception when calling PetApi#addPet");
e.printStackTrace();
}
}
}

Thanks,
Viduranga

On Tue, Nov 28, 2017 at 5:05 PM, Malintha Amarasinghe <[hidden email]> wrote:
Hi,

The code is generated from the swagger generated for the SOAP API (Not sure we tested something like this before.) Maybe some response related schema missing from the swagger which causes the code generator to generate a "void" method.

Can you attach the swagger file of the particular API generated via APIM?

Thanks!



On Tue, Nov 28, 2017 at 4:48 PM, Isuru Uyanage <[hidden email]> wrote:
Hi Fazlan, 
Yes, I did not include the post call in above example since it does return a void. It is as below. 
defaultApi.post(Stirng soapRequest, String soapAction);

Thanks and Best Regards,

Isuru Uyanage
Software Engineer - QA | WSO2
Mobile : <a href="tel:+94%2077%20767%201807" value="+94777671807" style="color:rgb(17,85,204)" target="_blank">+94 77 55 30752




On Tue, Nov 28, 2017 at 4:41 PM, Fazlan Nazeem <[hidden email]> wrote:

Hi Isuru,

On Tue, Nov 28, 2017 at 2:48 PM, Isuru Uyanage <[hidden email]> wrote:
Hi All, 

I want to invoke an API(created through a SOAP Endpoint) using the SDK (generated from WSO2 API Manager) in a Java class. I did following steps. 

1. A simple SOAP service is created which takes height(double) and weight(double) as inputs and outputs the BMI(formatted string) as the result.

2. An API is created through SOAP Endpoint by providing the WSDL of the web service. 
API was invoked through SOAP UI and it outputs the correct result. 

3. SDK for Java is generated through API Manager. 
4. Built the SDK using Maven. 
5. Included following dependency in my own maven project.
<dependency>
<groupId>org.wso2</groupId>
<artifactId>org.wso2.client.api.BmiMeter</artifactId>
<version>1.0.0</version>
<scope>compile</scope>
</dependency> 
5. Manually installed following jars to the maven project. 
  • org.wso2.client.BmiMeter-1.0.0-tests.jar
  • jars in the target>lib folder
6. Created a java classs in the Maven project and tried to invoke the api. 

public class Main {
public static void main(String[] args) throws ApiException {
DefaultApi defaultApi = new DefaultApi();
String accessToken = "a92a1d4f-ac2a-398f-80b0-49f79b5b4de1";

Map<String, String> headers = new HashMap<String, String>();
headers.put("Accept", "application/json");
headers.put("Authorization", "Bearer " + accessToken);
ApiClient apiClient = defaultApi.getApiClient();
apiClient.addDefaultHeader("Accept", "application/json");

apiClient.addDefaultHeader("Authorization", "Bearer " + accessToken);
apiClient.setLenientOnJson(true);
apiClient.setBasePath("http://10.100.5.136:8280/bmimeter/1.0.0");
defaultApi.setApiClient(apiClient);

}
Isn't this code snippet missing the actual post call? I do not see where it is invoked. 

 7. Since I created the API through SOAP Endpoint, my default method is a POST which I need to send a SOAP request as below.



Once I tried to invoke it through the code, (defaultApi.post) method, it returns a void. 

My question is how to invoke this kind of an API through a SDK in Java code. Your thoughts about this are highly appreciated. Please correct me if I have gone wrong in above steps. 




Thanks and Best Regards,

Isuru Uyanage
Software Engineer - QA | WSO2
Mobile : <a href="tel:+94%2077%20767%201807" value="+94777671807" style="color:rgb(17,85,204)" target="_blank">+94 77 55 30752




_______________________________________________
Dev mailing list
[hidden email]
http://wso2.org/cgi-bin/mailman/listinfo/dev




--
Thanks & Regards,

Fazlan Nazeem
Senior Software Engineer
WSO2 Inc
Mobile : <a href="tel:%2B94%20%280%29%20773%20451194" value="+94773451194" target="_blank">+94772338839


_______________________________________________
Dev mailing list
[hidden email]
http://wso2.org/cgi-bin/mailman/listinfo/dev




--
Malintha Amarasinghe
WSO2, Inc. - lean | enterprise | middleware

Mobile : <a href="tel:+94%2071%20238%203306" value="+94712383306" target="_blank">+94 712383306

_______________________________________________
Dev mailing list
[hidden email]
http://wso2.org/cgi-bin/mailman/listinfo/dev




--
Regards,
Viduranga Gunarathne
Software Engineer Intern
WSO2

Email : [hidden email]
Mobile : +94712437484
Web : http://wso2.com
https://wso2.com/signature

_______________________________________________
Dev mailing list
[hidden email]
http://wso2.org/cgi-bin/mailman/listinfo/dev