1. Ahmed Abdalla
  2. PowerBuilder
  3. Thursday, 13 September 2018 08:56 AM UTC

 

 

I have a small application written in c# consuming a webservice which returns a list of companies.

I would like to use the same in PB in order to integrate the web service consumption with my business requirements. The only problem that I have faced is how to add the message header. Below is the C# code, appreciate converting it to PB script

private void button1_Click(object sender, EventArgs e)
{
//token variable to be used in soap header
string token = "";
//API key to be used in soap header
string API_KEY = "xxx_xxxx_XX@";
//My webService client
AmanUnionWebService.AmanUnionServiceClient client = new AmanUnionWebService.AmanUnionServiceClient();
try
{
using (new OperationContextScope(client.InnerChannel))
{
//add a soap header for API Key
MessageHeader ApiMessageHeader = MessageHeader.CreateHeader("API_KEY", "http://tempuri.org", API_KEY);
System.ServiceModel.OperationContext.Current.OutgoingMessageHeaders.Add(ApiMessageHeader);
string userName = "username";
string passWord = "Pasword.";
string Provider = "provider";
//calls the authenticate user
//if the authentication is successful it will assign the token variable the appropriate value
client.AUEAuthenticateUser(userName, passWord, Provider, ref token);
//add a soap header for authentication key
//I add my token to the soap header
MessageHeader AuthMessageHeader = MessageHeader.CreateHeader("AUTH_TOKEN", "http://tempuri.org", token);
System.ServiceModel.OperationContext.Current.OutgoingMessageHeaders.Add(AuthMessageHeader);
label1.Text = null;
DateTime date = DateTime.Today;
DataSet companySearchResults = client.AUEGetSearchResults("SAL", "LE", -1 );
if (companySearchResults.Tables.Count > 0)
{
dataGridView1.DataSource = companySearchResults.Tables[0];
dataGridView1.Refresh();
}

}
}
catch (Exception ex)
{
var message = ex.InnerException.Message;
label1.Text = message;

}

}

Ahmed Abdalla Accepted Answer Pending Moderation
  1. Tuesday, 18 September 2018 08:13 AM UTC
  2. PowerBuilder
  3. # 1

I am trying to use httpclient object to consume the webserice. I took the following link as an example and trying to modify it.

https://www.appeon.com/support/documents/appeon_online_help/pb2017r3/application_techniques/ch19s01.html

 

HttpClient lhc_Client
CoderObject lco_Code
Jsonpackage ljpg_json
String ls_ClientID, ls_Sercet, ls_Auth, ls_Url, ls_PostData, ls_UserName, ls_Password, ls_scope, ls_Body, ls_Error
String ls_Token, ls_TokenType, ls_AccessToken

 

 


//Step 1: Get the RESTful server access token.
//Url
ls_Url = "http://dev.cosine.com.cy/AmanUnionWS/WebServices/AmanUnionService.svc"
//Authorization
ls_ClientID = "367c4163ddc1427d96655cd220c6714b"
ls_Sercet = secret
lblb_data = Blob ( ls_ClientID + ":" + ls_Sercet, EncodingUTF8! )
ls_Auth = lco_Code.Base64Encode( lblb_data )
lhc_Client.SetRequestHeader( "API_KEY", API_KEY )
//lhc_Client.SetRequestHeader( "API_KEY", "http://tempuri.org" )
lhc_Client.SetRequestHeader( "Content-Type", "text/xml;charset=utf-8" )

//ltr_Request.tokenlocation = "http://dev.cosine.com.cy/AmanUnionWS/WebServices/AmanUnionService.svc?singleWsdl"


//PostData
ls_UserName = userName
ls_Password = passWord
ls_scope = "testcode"
//ls_PostData = "grant_type=password&username="+ls_UserName+"&password="+ls_Password+"&scope=" + lco_Code.UrlEncode( Blob(ls_scope,EncodingUTF8!))
ls_PostData = "AUEAuthenticateUser (&ECA_ID="+ECA_ID
ls_PostData += "&username=" + userName
ls_PostData += "&password=" + passWord
ls_PostData += "&Auth_Token=" + lco_Code.UrlEncode( Blob(ls_scope,EncodingUTF8!)) + ")"
ll_return = lhc_Client.SendRequest( "POST", ls_Url, ls_PostData )
integer li_return
li_return = lhc_Client.GetResponsestatusCode()
If ll_return = 1 And lhc_Client.GetResponsestatusCode() = 200 Then
lhc_Client.GetResponseBody ( ls_body )
ls_Error = ljpg_json.loadString ( ls_body )
If ls_Error = "" then
ls_TokenType = ljpg_json.GetValue("token_type")
ls_Token = ljpg_json.GetValue("access_token")
ls_AccessToken = ls_TokenType + " " + ls_Token

//Step 2: Get the RESTful server resource.
ls_Url = "https://authserver.appeon.com/order/getall"
lhc_Client.ClearRequestHeaders()
lhc_Client.SetRequestHeader( "Authorization", ls_AccessToken )
ll_return = lhc_Client.SendRequest( "GET", ls_Url )
If ll_return = 1 And lhc_Client.GetResponsestatusCode() = 200 Then
lhc_Client.GetResponseBody ( ls_body )
MessageBox ( "Resource", ls_body )
Else
MessageBox( "ResourceResponse Falied", "Return :" + String ( ll_return ) + "~r~n" + lhc_Client.GetResponsestatusText() )
End If

Else
MessageBox( "Error", ls_Error )
End If
Else
ls_Error = lhc_Client.GetResponsestatusText()
MessageBox( "AccessToken Falied", "Return :" + String ( ll_return ) + "~r~n" + lhc_Client.GetResponsestatusText() )
End If

If IsValid ( lco_Code ) Then DesTroy ( lco_Code )
If IsValid ( ljpg_json ) Then DesTroy ( ljpg_json )
If IsValid ( lhc_Client ) Then DesTroy ( lhc_Client )

 

References
  1. https://www.appeon.com/support/documents/appeon_online_help/pb2017r3/application_techniques/ch19s01.html
Comment
There are no comments made yet.
Ahmed Abdalla Accepted Answer Pending Moderation
  1. Tuesday, 18 September 2018 08:06 AM UTC
  2. PowerBuilder
  3. # 2

below is the detail from the webservice documentation:

METHODS:

To access the wsdl please follow the link: http://dev.cosine.com.cy/AmanUnionWS/WebServices/AmanUnionService.svc?singleWsdl

API KEY:  Cosine_xxxxx

1.AUEAuthenticateUser: (Login)

Request

Method

URL           

POST

http://dev.cosine.com.cy/AmanUnionWS/WebServices/AmanUnionService.svc

 

Type

Params

Values

HEAD

POST

POST

POST

POST

API_KEY

ECA ID

username

password

Auth_Token

string

string

string(30)

string

string(by Ref)

 

RETURN VALUE:  BOOLEAN(true/False)

The service will require the API_KEY in the soap header, with tag name  ”API_KEY” and it’s appropriate value, which will be a value that we will provide for you.

The service will also have a by _ref parameter which will be the auth_token to be used later on when the rest services will be called. If the login was successful then it will assign the Auth_Token a value.  The authentication token expires after 10 minutes. In that case a new token must be acquired by  calling the AUEAuthenticateUser again. This is a security measure .

 

 

Response

Status

Response

200

Ok

403

{"error":"API key is missing."}

400

{"error":"Please provide ECA ID."}

400

{"error":"Please provide username."}

400

{"error":"Please provide password."}

401

{"error":"Invalid API key."}

401

{“ error”:”Incorrect combination of username/password/ECA_ID.”}

500

{"error":"Something went wrong. Please try again later."}

 

 

 

 

 

 

 

 

 

 

 

 

2.AUEGetSearchResults: (GetSearchResults)
Request

Method

URL           

POST

http://dev.cosine.com.cy/AmanUnionWS/WebServices/AmanUnionService.svc

 

Type

Params

Values

HEAD

HEAD

POST

POST

POST

AUTH_TOKEN

API_KEY

CompanyName

CountryCode

CompanyId

string

string

string(200) (default Value=””)

char(2) (defaultValue=””)

integer(defaultValue=-1)


The default value , is the value to be entered if the procedure is not to take into consideration the specific field.

Example: If I were to search only by company Name and not provide the Company Name and Company Id , the function will be called as follows:

AUEGetSearchResults(“Cosine”, ”” ,-1)
The function will then proceed by searching only by company Name.

Return Value: A dataset will be provided as a return value, containing all the companies containing the search criteria. If the dataset is null, then it means there was not any company to match the criteria provided.

Response

Status

Response

200

OK

400

{"error":"Auth_Token or API_KEY is invalid"}

500

{"error":"Something went wrong. Please try again later."}

Comment
There are no comments made yet.
Ahmed Abdalla Accepted Answer Pending Moderation
  1. Sunday, 16 September 2018 04:31 AM UTC
  2. PowerBuilder
  3. # 3

Thank you for the reply. The above code is a sample  for consuming the web service in c#. I want to consume the web service in Powerbuilder. The web service has many methods, one of them is AUEGetSearchResults which returns a dataset. My problem is that I am unable to pass the message header. The following is a description of the method from official documentation of  the web service provider:

2.AUEGetSearchResults: (GetSearchResults)
Request

Method

URL           

POST

http://dev.cosine.com.cy/AmanUnionWS/WebServices/AmanUnionService.svc

 

Type

Params

Values

HEAD

HEAD

POST

POST

POST

AUTH_TOKEN

API_KEY

CompanyName

CountryCode

CompanyId

string

string

string(200) (default Value=””)

char(2) (defaultValue=””)

integer(defaultValue=-1)


The default value , is the value to be entered if the procedure is not to take into consideration the specific field.

Example: If I were to search only by company Name and not provide the Company Name and Company Id , the function will be called as follows:

AUEGetSearchResults(“Cosine”, ”” ,-1)
The function will then proceed by searching only by company Name.

Return Value: A dataset will be provided as a return value, containing all the companies containing the search criteria. If the dataset is null, then it means there was not any company to match the criteria provided.

Response

Status

Response

200

OK

400

{"error":"Auth_Token or API_KEY is invalid"}

500

{"error":"Something went wrong. Please try again later."}

Comment
There are no comments made yet.
Bruce Armstrong Accepted Answer Pending Moderation
  1. Friday, 14 September 2018 15:15 PM UTC
  2. PowerBuilder
  3. # 4

What is this webservice returning? If it's returning a C# specific data type ( DataSet ) you won't be able to use it from anything other than C#.

Well designed web services return data in language agnostic type so that it can be called from a variety of clients, not just the language that was used to create it.

Comment
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.