jmasterx
jmasterx

Reputation: 54133

Using A Web API for Business Logic?

My web application needs to be able to go and get all my projects from Paymo http://api.paymo.biz/

I am familiar with JSON and XML, but what I'm wondering is, how does one interact with the api (make calls to it).

I would ideally like to make a class in ASP .Net such as PaymoManager(int apikey....)

From there I can wrap the functionality I need. I just need to understand, how do I call functions of the API and how do I get the response. I am not familar with web apis.


Edit: Could you give me an example of this, even with some abstract url. I need this done server side in a CS file.

Basically a simple example that calls someurl.com/somerequest and then how do you receive the JSON or XML... how does this work in terms of a class. I want this in a class.

Upvotes: 0

Views: 1553

Answers (2)

kmote
kmote

Reputation: 16735

If you are using .NET 4.5, you might consider using HttpClient like so:

static async void Main()
    {
    try 
    {
      // Create a New HttpClient object.
      HttpClient client = new HttpClient();

      // fill in the details in the following string with your own KEY & TOKEN:
      string requestUrl = "https://api.paymo.biz/service/paymo.auth.logout?api_key=API_KEY&format=JSON&auth_token=AUTH_TOKEN"
      HttpResponseMessage response = await client.GetAsync(requestUrl );
      response.EnsureSuccessStatusCode();
      string responseBodyJSON = await response.Content.ReadAsStringAsync();
      // Above three lines can be replaced with new helper method in following line 
      // string body = await client.GetStringAsync(uri);

      Console.WriteLine(responseBodyJSON );
      // Now you can start parsing your JSON....

    }  
    catch(HttpRequestException e)
    {
      Console.WriteLine("\nException Caught!"); 
      Console.WriteLine("Message :{0} ",e.Message);
    }
  }

Upvotes: 2

jrummell
jrummell

Reputation: 43087

http://api.paymo.biz/docs/misc.overview.html

To perform an action using the Paymo API, you need to send a request to the Paymo webservice specifying a method and some arguments, and will receive a formatted response.

This means that you can use WebClient to download a string from a url:

WebClient client = new WebClient();
string reply = client.DownloadString (address);

Depending on the format you specify, you can parse the reply as XML or JSON.

XDocument xml = XDocument.Parse(reply);

// where ReplyType is a class that defines public 
// properties matching the format of the json string
JavaScriptSerializer serializer = new JavaScriptSerializer();
ReplyType abc = serializer.Deserialize<ReplyType>(reply);

Upvotes: 2

Related Questions