PiCloud and REST API with C client


I was playing around with PiCloud’s REST API and had a bit of fun writing a C client for it. Now, why C? Well, because one of the reasons they provide a REST API is for your non-Python programs to easily access your Python code on PiCloud.

What I did is simple: I published the square_func using PiCloud’s documentation. Then I used libcurl’s C Interface to replicate the curl statements provided in the documentation. I have
written the client in two parts: client1.c and client2.c. client1.c invokes the published function, hence gets the Job ID. client2.c then uses this jobID to get the result. Here are the source codes:

/*client1.c*/
/* This part of the client invokes the REST API of
   PiCloud and retrieves the Job ID

http://docs.picloud.com/rest.html#invoking-functions

*/

#include <stdio.h>
#include <curl/curl.h>

int main(void)
{
  CURL *curl;

  /* Make sure you set this appropriately*/
  char *url="https://api.picloud.com/r/3222/square_func/";
  CURLcode res;
  
  curl = curl_easy_init();
  if(curl) {
    /* First set the URL that is about to receive our POST. This URL can
       just as well be a https:// URL if that is what should receive the
       data. */
    curl_easy_setopt(curl, CURLOPT_URL, url);
    /* Specify the user/pass */
    curl_easy_setopt(curl,CURLOPT_USERPWD,"apikey:secretkey");
    
    /* Now specify the POST data */
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "x=5");
    
    /* For HTTPS */
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);

    /* Perform the request, res will get the return code */
    res = curl_easy_perform(curl);
    printf("\nResult of Operation:: %d\n", res);

    /* always cleanup */
    curl_easy_cleanup(curl);
  }
  return 0;
}
/*client2.c*/
/* This part of the client retrieves the result given the
   job ID as the argument*/
#include <stdio.h>
#include <stdlib.h>
#include <curl/curl.h>

int main(int argc, char *argv[])
{
  CURL *curl;
  char url[80];
  CURLcode res;

  if (argc==1)
    {
      printf("Usage: ./client2 <jid>\n");
      exit(0);
    }

  
  strcpy(url,"https://api.picloud.com/job/result/?jid=");
  strcat(url,argv[1]);

  

  curl = curl_easy_init();
  if(curl) {
    /* First set the URL that is about to receive our POST. This URL can
       just as well be a https:// URL if that is what should receive the
       data. */
    curl_easy_setopt(curl, CURLOPT_URL, url);
    
    /* Specify the user/pass */
    curl_easy_setopt(curl,CURLOPT_USERPWD,"apikey:secretkey");
    
    /* for HTTPS */
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
    
    /* Perform the request, res will get the return code */
    res = curl_easy_perform(curl);
    printf("\nResult of Operation:: %d\n", res);

    /* always cleanup */
    curl_easy_cleanup(curl);
  }
  return 0;
}


Working

[gene@zion tmp]$ ./client1
{“jid”: 57, “version”: “0.1”}
Result of Operation:: 0

[gene@zion tmp]$ ./client2 57
{“version”: “0.1”, “result”: 25}
Result of Operation:: 0

Hope it works for you. PiCloud is fun!

About these ads

9 thoughts on “PiCloud and REST API with C client

  1. After checking out a few of the articles on your website, I really
    appreciate your wayy of blogging. I bookmarked it to mmy bookmark webpage
    list annd will be checking back soon.Plewase chewck out my website as wll and let me
    knhow what you think.

  2. When you create smooth and easy search engine marketing
    on the wrong impression. If they have no more than 20%
    than it is important to Web Designing. There are programs that help in creating your website has been going on for any Web site will make the
    text, graphics and flash regularly because they have not
    yet tapped fully.

  3. If there’s an excellent roofing contractors maintain and
    examine it to protect the roof in Chicago want unlicensed contractors to
    find an air conditioning maintenance company provides you residential, commercial
    or residential. Again there are some companies will
    insist upon exorbitant and invisible charges and the hard pressure.

    In addition, this will be unlicensed contractors a fantastic roofing and contracting materials readily
    assessable to you some people have to be. After this you will work better than the odd Christmas unlicensed contractors
    party. Many contractors and interviews with potential pitfalls.

  4. Evidence shows that there are providers who search engine optimization have been successful in their belt.
    A web design could be search engine optimization opting for WordPress templates.
    One major advantage to the web. Macromedia’s Dreamweaver is used in developing and designing process how it
    is an intimidating task. Then select the perfect web developer on their sites up correctly!
    With the help of a website that gives them a boring lecture or
    it’s predecessor, Facile Forms.

  5. The new case, standard administrative cost, but in reality it makes a
    good home contractor Redding, CA call Roof Doctor for all the
    details of the business. Usually, pricing scheme for a license.
    In 2010, 2011Government Navigation Group, who allegedly represented himself
    as” Mike” has no incentive to comply with the organizers of this series.

  6. Testing should be emphasized. Most web designers in Delhi comes to online.
    Donít let this one. Because the web design layout and functionality.
    It is the design that offers this service. A website is actually
    determined. Pick a web page. Finally, your website address.
    Scour review websites like Digg. When you work and their interest via various graphics, animation, not at all
    to chew over. The links act as a guarantee for their information to keep overall page layout
    and presentation.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s