Link to home
Start Free TrialLog in
Avatar of ross13
ross13Flag for United Kingdom of Great Britain and Northern Ireland

asked on

Help with Web API

Looking at integrating a vb.net application to pull orders from a web API. In the past I have added a reference and used the WSDL. The software I am trying to integrate is:

http://docs.handshake.com/?json-doc#quickstart

It would be better if I could use a WSDL type integration but from the documentation it looks like you have to do a HTTP request.

Really looking for the best way to move this forward. don't know if I should use the HttpWebRequest.

The documentation says :
# Use the command below to dump the JSON data for your orders. Replace the `2c493d74` with your own API key (which will be much longer)
curl -u 2c493d74:X "https://app.handshake.com/api/latest/orders"


I am a bit unsure if I keep my httpwebrequest as:        
Dim myRequest As HttpWebRequest = CType(WebRequest.Create("https://app.handshake.com/api/latest/orders"), HttpWebRequest)

If I try and anything else it fails. I can call myRequest.Credentials but is this what I should be trying to do?


Best Regarsd,

Ross
Example.JPG
Avatar of kaufmed
kaufmed
Flag of United States of America image

It would be better if I could use a WSDL type integration but from the documentation it looks like you have to do a HTTP request.
WSDLs are only for SOAP-based services. This API is more of a RESTful service, which as you've correctly determined, must use HTTP verbs and payloads to interact with it.

These days, the preferred tool for interacting with RESTful web services is the the HttpClient:

Async Function GetOrdersAsync() As Task(of Example)
    Dim client as New HttpClient()
    Dim username As String = "your api key"
    Dim password As String = "X" ' Per the documentation
    Dim auth As String = username & ":" & password
    Dim authBytes() As Byte = Encoding.UTF8.GetBytes(auth)
    Dim encodedAuth As String = Convert.ToBase64String(authBytes)
    Dim ordersJson As String
    Dim responseObject As Example

    client.DefaultRequestHeaders.Authorization = New AuthenticationHeaderValue("Basic", encodedAuth) ' Per the documentation, use basic authentication

    ordersJson = Await client.PostAsync("https://app.handshake.com/api/latest/orders?format=json", )
    responseObject = JsonConvert.DeserializeObject(Of Example)(ordersJson)

    Return responseObject
End Function

Open in new window


The site you linked specifies that it uses basic authentication (which should be fine since it looks like they require SSL connections--I wouldn't trust this API security-wise if they allow you to use the site without SSL, because your API key will be sent in clear-text across the Interent). Basic auth requires a specific header in your request. That is established in line 11. The stuff that line 11 needs is set up at the beginning of the function. Once you get the auth configured, it should be a matter of simply making the request, as I'm doing in line 13. Now, what you get back--using the particular GetStringAsync that I used--will be a string. This represents the JSON that the service returns. If you want to programmatically work with that data, then you need to deserialize the data into classes. You can use a site like https://jsonutils.com/ to paste in a chunk of data and have it spit out some VB.NET classes to represent the structure of that data. (You can also build the classes by hand, if you want better control over naming and whatnot.)

Public Class Meta
    Public Property limit As Integer
    Public Property next As Object
    Public Property offset As Integer
    Public Property previous As Object
    Public Property total_count As Integer
End Class

Public Class BillTo
    Public Property btCustomer As String
    Public Property city As String
    Public Property country As String
    Public Property fax As String
    Public Property objID As Integer
    Public Property phone As String
    Public Property postcode As String
    Public Property resource_uri As String
    Public Property stCustomer As Object
    Public Property state As String
    Public Property street As String
    Public Property street2 As String
End Class

Public Class CreditCard
    Public Property customer As String
    Public Property cvv As String
    Public Property lastFour As String
    Public Property month As String
    Public Property name As String
    Public Property number As String
    Public Property objID As Integer
    Public Property resource_uri As String
    Public Property type As String
    Public Property year As String
End Class

Public Class CreditCard
    Public Property customer As String
    Public Property cvv As String
    Public Property lastFour As String
    Public Property month As String
    Public Property name As String
    Public Property number As String
    Public Property objID As Integer
    Public Property resource_uri As String
    Public Property type As String
    Public Property year As String
End Class

Public Class CustomerGroup
    Public Property id As String
    Public Property manufacturer As Object
    Public Property name As String
    Public Property objID As Integer
    Public Property parent As Object
    Public Property resource_uri As String
    Public Property subType As String
    Public Property subcategories As Object()
End Class

Public Class DefaultShipTo
    Public Property btCustomer As String
    Public Property city As String
    Public Property country As String
    Public Property fax As String
    Public Property objID As Integer
    Public Property phone As String
    Public Property postcode As String
    Public Property resource_uri As String
    Public Property stCustomer As Object
    Public Property state As String
    Public Property street As String
    Public Property street2 As String
End Class

Public Class ShipTo
    Public Property btCustomer As Object
    Public Property city As String
    Public Property country As String
    Public Property fax As String
    Public Property objID As Integer
    Public Property phone As String
    Public Property postcode As String
    Public Property resource_uri As String
    Public Property stCustomer As String
    Public Property state As String
    Public Property street As String
    Public Property street2 As String
End Class

Public Class UserGroup
    Public Property id As String
    Public Property manufacturer As Object
    Public Property name As String
    Public Property objID As Integer
    Public Property parent As String
    Public Property resource_uri As String
    Public Property subType As String
    Public Property subcategories As Object()
End Class

Public Class Customer
    Public Property billTo As 
    Public Property cdate As String
    Public Property contact As String
    Public Property creditCards As CreditCard()
    Public Property ctime As DateTime
    Public Property customerGroup As CustomerGroup
    Public Property defaultShipTo As DefaultShipTo
    Public Property email As String
    Public Property entityType As String
    Public Property id As String
    Public Property mtime As DateTime
    Public Property name As String
    Public Property objID As Integer
    Public Property owner As String
    Public Property paymentTerms As String
    Public Property resource_uri As String
    Public Property shipTos As ShipTo()
    Public Property shippingMethod As String
    Public Property userGroup As UserGroup
    Public Property uuid As String
End Class

Public Class Line
    Public Property barcode As String
    Public Property cdate As String
    Public Property ctime As DateTime
    Public Property description As String
    Public Property dimensions As Object
    Public Property entityType As String
    Public Property isku As String
    Public Property item As String
    Public Property manufacturer As String
    Public Property mtime As DateTime
    Public Property notes As String
    Public Property objID As Integer
    Public Property order As String
    Public Property owner As String
    Public Property qty As Integer
    Public Property resource_uri As String
    Public Property sku As String
    Public Property unitPrice As String
    Public Property uuid As String
    Public Property vsku As Object
End Class

Public Class ShipTo
    Public Property btCustomer As Object
    Public Property city As String
    Public Property country As String
    Public Property fax As String
    Public Property objID As Integer
    Public Property phone As String
    Public Property postcode As String
    Public Property resource_uri As String
    Public Property stCustomer As Object
    Public Property state As String
    Public Property street As String
    Public Property street2 As Object
End Class

Public Class Object2
    Public Property billTo As BillTo
    Public Property cancelDate As Object
    Public Property category As String
    Public Property cdate As String
    Public Property creditCard As CreditCard
    Public Property ctime As DateTime
    Public Property customer As Customer
    Public Property customerPO As Object
    Public Property entityType As String
    Public Property freeShipping As Boolean
    Public Property lastEdited As DateTime
    Public Property lastExported As Object
    Public Property lines As Line()
    Public Property mtime As DateTime
    Public Property notes As String
    Public Property objID As Integer
    Public Property owner As String
    Public Property paymentTerms As String
    Public Property resource_uri As String
    Public Property shipPartial As Boolean
    Public Property shipTo As ShipTo
    Public Property shippingMethod As String
    Public Property startShipDate As Object
    Public Property status As String
    Public Property userGroup As String
    Public Property uuid As String
End Class

Public Class Example
    Public Property meta As Meta
    Public Property objects As Object()
End Class

Open in new window


You can install JSON.NET from NuGet. That will give you the JsonConvert that I uses in line 14. You can see that the end result is an instance of the Example class that https://jsonutils.com/ created for me.

GETs, POSTs, DELETEs, and PUTs should all function mostly the same. You're generally just changing the method you invoke in line 13, and how you append the parameters. With PostAsync and PutAsync, there is an additional parameter to the method. You'll most likely have to serialize that data into either XML or JSON (depending on your preference) and send the parameter as a string.

Keep in mind that HttpClient was designed to be reused. While it has a Dispose method, you generally don't want to use either a Using or to call Dispose until you are done with any and all interactions with the service. HttpClient is designed to be resilient to failures.
Avatar of ross13

ASKER

Thanks for your reply. Trying to do some research to get this working from what you sent. I am trying to find out what the second param should be on the postasync call. I have probably spent more time than I should on this. As soon as I get a response back I will look at the jason part.

Best Regards,

Ross
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.