As a summer intern at JMP, my project involved working with APIs on the web. I had a bit of a learning curve in the first few weeks of the project as I was trying to get different APIs to work in JMP or if it was even possible. I was able to reach a point of understanding when I saw this analogy in a video called What is a RESTful API? Explanation of REST & HTTP:
Imagine you are at a restaurant. You are the consumer/user, the person interacting to get something that you want back from someone else. The waiter who comes to take your order can be likened to an API; the waiter will come and take your order, and take (send) it back to the kitchen (or in this case, the server, or any sort of software the API may be communicating with). When the order is complete (or the software or server gives a response), the waiter will come back and give you your order (fulfill your request). The request is complete!
After I found this analogy, I had a good mental model for how APIs worked.
The focus of my project was to create a JMP add-in that could access REST APIs on the web. I needed to show some working examples and also teach users about REST APIs and help them understand what’s happening behind the scenes as they use the add-in.
The motivation behind this project was the new feature of HTTP Request() in JMP 14. APIs, specifically the architectural style of REST, is something that is still considered to be relatively new. My first challenge was to learn about REST:
A REST, or RESTful API is an API with the components and qualities of a REST architectural style. REST stands for Representation State Transfer and defines a protocol and set of standard operations for how well-designed applications should behave. REST is primarily used by browsers and the framework could be called “the language of the internet”. To be considered RESTful, an application must have six major attributes: client-server architecture, statelessness, cacheability, be a layered system, ability to execute code on demand, and have a uniform interface. From the perspective of the end user, it doesn’t matter much whether an API is RESTful or not, but there are a lot of advantages on the programming side to being RESTful, which is why it has become the standard for APIs.
The next challenge I had was with JSL. I wasn’t sure how I wanted to go about creating a UI for the add-in in JSL, so I took a look at Appbuilder for JMP. I decided to work on the add-in using Appbuilder as I could separate the script for the back end from the script for the front end (or in this case, Appbuilder) and organize everything visually.
This is what the final product looks like:
I did research and compiled it into the Help button at the top, for those who want more description of the different authentication methods. It provides a description of each type of authentication we will tackle.
Next was the fun part. I found different APIs to access to utilize within my add-in. The CAT API is my personal favorite, so I used it twice (once for without authentication and once for with). Each time you click Send, a new cat pops up!
After I got the Cat API working, I found several other good examples and included the ones I like best in the add-in.
I especially enjoyed working on the Tumblr example, since it retrieves so much information (the entire tumblr blog). I wasn’t able to make the information that’s retrieved look that pretty, but I think it still does a good job. For anyone wishing to retrieve information on a different Tumblr post, they can switch out the Tumblr blog address in the JSL script here:
The last example was something special. Dropbox is an example that one of the other principal developers had already worked on. I wanted to integrate this example into my add-in to try and make it as user-friendly as possible so that anyone without programming experience could use it. The fact that it can remotely upload a file and folder with the click of a button through JMP still astounds me.
I hope you will try it out. Please leave any questions or comments you have.
For instructions on utilizing the add-in itself, please refer to my post Getting Started With REST.
... View more
These are the instructions on how to utilize the Getting Started With REST APIs add-in in the best way to learn how HTTP Requests and REST APIs in JMP work.
Download the add-in and launch it via Addins->API Toolbar. This is what it should look like:
You can go through the examples in any which order you’d like. They are listed in order of least complex to most complex.
The Random cat example is simplest as it requires no authentication. The example is already preloaded with the URL and HTTP Request method to utilize (GET).
Press the send button to see the images of cats appear on the screen.
Press the Show JSL button to see the JSL script going on behind the scenes. After you’re done with the example, click back to go back to the title screen.
Next is the More Random Cats This example appears to be the exact same as the previous one, except that now there’s a textbox for entering in an APIKey.
Obtain an APIKey from the aforementioned link, and paste it into the textbox as stated.
Send the request. A log file with an overview of the information retrieved for the specific APIKey/Account that you are using should come back.
Press the Show JSL button to see how this code differs from the previous example (you’ll be surprised).
Next is the Weather This has the same level of authentication as the previous “More cats” example, and requires the use of an APIKey to have it successfully work.
Click the “Get my Key” button and register for an account; after you obtain your key, paste it into the keybox below.
Click “Send.” A log file with the data for a specific city should come up, in this case it’s London, UK.
Click the “Show JSL” button to see the code behind the script. You can find the weather information for any city or location that you want by simply searching it up on the API, and replacing the URL in the JSL script.
Look at the Tumblr This has a more complex level of Authentication known as OAuth (specifically OAuth 1.0a); it requires the use of a Consumer Key, Consumer Secret, Token Key, Token Secret, and depending on the API being used, more or fewer parameters to access or post information.
In order for this to successfully work, register for a Tumblr account, and register an application to utilize with OAuthentication.
After all of this is complete, copy the OAuth Consumer Key and paste it into the textbox below. Click “Send.” Information regarding the current Tumblr account should be obtained in a Log.
Click the “Show JSL” button to see the code behind the script.
Look at the Dropbox This is the most up to date level of OAuth currently available (OAuth 2.0), and is currently the most complex level of authentication I will cover. It requires the same things as OAuth 1.0a, except there is the possibility of a refresh token and access token as well.
In order for this example to successfully work, sign into your Dropbox account and obtain an access token from the given URL.
Obtain and copy the access token and paste it into the text box below. Click “Send” once you’ve done everything. This example “POSTS” a file to your directory. A new “JMP” folder should be created in your Dropbox with the file “Big Class” in it.
Click the “Show JSL” button to see the code behind the script.
If there are any questions withstanding for how to use the add-in, or any issues that you may run into, please don’t hesitate to leave a comment.
... View more