WCF REST Starter Kit Consumer quick example
Category: DotNet
Here I will summarize some quick points about using the WCF Rest Starter Kit P2 to consume REST services. If you are not familiar with it, here is a good article from msdn: http://msdn.microsoft.com/en-us/library/ee391967.aspx
// quick code example .net accessing shopify api
private orders InitialOrderSync(string appURL, string apiKey, string pwd)
{
// note here we can use http or https
HttpClient client = new HttpClient("https://" + appURL);
// set optional credentials
client.TransportSettings.Credentials = new NetworkCredential(apiKey, pwd);
// the uri of the REST path to get, can also pass in parameters
HttpResponseMessage message = client.Get("admin/orders.xml");
message.EnsureStatusIsSuccessful(); // make sure all ok
// serialize into object
orders shopifyOrders = message.Content.ReadAsXmlSerializable();
return shopifyOrders;
}
You may be wondering how did I build the orders object based on the xml. This is very easy. Just copy the xml you will be serializing from start to finish, go into Visual Studio that has wcf rest kit installed, add a new Class, and go menu Edit -> Paste as XML Types. That's it.
As far as using requirements, add references to your project to all 3 binaries in the WCF REST kit installation directory folder.
in your code you'll need these guys for above to work:
using System.Xml.Serialization;
using System.Xml;
using System.Runtime.Serialization;
using Microsoft.Http;
using Microsoft.ServiceModel;
using System.Net;