Calling an API from within a Xojo API

I am writing an API in which I will need to make calls to an external API over HTTPS.

In a standard web app, I do the following.

Name: HTTPS1
Super: HTTPSecureSocket

pgSignin

Name: HTTPSSignIn
Super: HTTPS1

[code]Sub SendHTTPSPostRequest(form As Dictionary, Object As String)
Dim Request As String
Dim bSend As Boolean = True

HTTPSSignIn.SetFormData(form)
HTTPSSignIn.SetRequestHeader(“Authorization”, “Key”)

Select Case Uppercase(Object)
Case “NEWCUSTOMER”
Request = “customers”

Case Else
bSend = False
End Select

if bSend then HTTPSSignIn.Post(AppOption(enumAppOption.URL).StringValue + Request)

End Sub

Sub PageReceived(url as string, httpStatus as integer, headers as internetHeaders, content as string)
Dim ctx As New WebSessionContext(App.SessionForControl(Self))
if ctx <> nil then
if ctx.Session.CurrentPage IsA pgSignin then
pgSignin(ctx.Session.CurrentPage).ccRegisterNewAccount1.PageReceived(url, httpStatus, DefineEncoding(content, Encodings.UTF8))
end if
end if

End Sub[/code]

ccRegisterNewAccount1

Sub PageReceived(url As String, httpStatus As Integer, data As String) CreateAccount(httpStatus, data) End Sub

How would I do the same with from within my API?

I’m not sure what you mean. Are you saying that you want your app to host an api as well?

The app I’m writing is strictly an API (no web pages, containers, dialogs, etc.). I need to make API calls to an external RESTful API for billing, subscriptions, invoicing, etc.

I have it working in one of my standard web apps but since an API has no web pages, containers, etc. to place the HTTPS1 on, I’m quite foggy on how to implement it.

You would just instantiate your class in code and keep a reference to it in a property for as long as you need it.

[quote=179247:@Scott Rich]The app I’m writing is strictly an API (no web pages, containers, dialogs, etc.). I need to make API calls to an external RESTful API for billing, subscriptions, invoicing, etc.

I have it working in one of my standard web apps but since an API has no web pages, containers, etc. to place the HTTPS1 on, I’m quite foggy on how to implement it.[/quote]
You could put your API in The HandleSpecialUrl or HandleUrl event.

Greg - I am writing the API solely based on the HandleSpecialUrl event. There is no “GUI” per se. Is there an advantage to building the API into the main web application vs. a free-standing API application? I have additional web apps that will be making calls to the API.

Kevin - Classes have always been a bit of a challenge for me. Will you give me a basic example? I was able to switch from async to sync for communications with the external API. From what I have read/seen, async is preferable. I’m concerned that using the sync method, I will run into a port busy situation.

[code] Dim HTTPSStripe As new HTTPS1
Dim URL As String = AppOption(enumAppOption.StripeURL).StringValue

if Len(Resource) > 0 then URL = URL + Resource

HTTPSStripe.SetFormData(form)
HTTPSStripe.SetRequestHeader(“Authorization”, "Bearer " + App.StripeKey)
HTTPSStripe.ConnectionType = 3
HTTPSStripe.Secure = True
HTTPSStripe.Yield = True

Select Case Uppercase(method)
Case “POST”
jResponse.Value(“response”) = (HTTPSStripe.Post(URL, 5))

Case “DELETE”
jResponse.Value(“response”) = HTTPSStripe.SendRequest(“DELETE”, URL, 5)

Case Else
jResponse.Value(“response”) = (HTTPSStripe.Get(URL, 5))

end select
[/code]

The only advantage is that a web app already has a web server built in. There’s nothing to say that you couldn’t do all that yourself, but it’s not a small feat.

Scott, you are creating the https1 class instance in code now. The difference to go async would be that you would need to store your reference somewhere so it doesn’t drop out of scope and get deallocated. Then when calling the post or whichever method you use, you would not assign the results directly. You would need code in your https1 class that would handle the results when they came in.

I’m not sure, but there may be an example of this in the LR,Guide, or one of the example projects. I think I’ve seen it before.

Basically you would put your code the pageReceived event of your class and handle the response there. You might also need some other bits to handle timeouts and errors.

Thank you Kevin. Your explanation clears the foggy image.