WPRM Example 4: Mashape APIs – Market Data API

You can access the Mashape APIs website from here. Today, we will implement MARKET DATA API.

  1. Establishing Connection:
    To make a request to the service server you have to use a URL which have 2 parts: base url and parameters. The first part is static and the second is dynamic in most cases which means that you can change the results if you changed the parameters’ values.
    To establish a connection you need to know 3 thing about your API:

    • Its base url (your custom while 1 and 2 can be replaced with the paraameters): https://stockviz.p.mashape.com/marketdata/{1}
    • the results (response) format [Json, XML, etc ..]: JSON in this example
    • The parameters Keys: the name of the parameters like data (or you can all it data_type).
    • Check the checkbox to confirm that your url is dynamic with numbers like {1}.
    • In Authorization and header settings section, You need to add X-Mashape-Key as token key. Get one using the API’s documentation.

    Once you now that, go to Restful Manager > Connection, add a new connection, give it a relevant name(you will need it soon)  and add the data as in the following screenshot then click Save.
    mashconn

  2. Create request and Connection Settings:
    Next, we need to assign those parameters to suitable data so that the web service can query it and return the required data response.
    Now, go to Restful Manager > Requests, add a new request. and give it a name, then select the connection name that you have just created. When the parameters show up, populate the parameter values as you see in the following screenshot. Both are fixed values(however you can make it so it retrieve from database).
    As you can see, there are a lot of options in Parameter values setting, most of them are to set the dynamic data retrieval which is one of the features that makes this plugin powerful(we will discuss it more in other tutorial).
    Once you populated those fields, click save.
    mashreq
  3. Request’s Display Settings:
    After you save your values, you will find new sections are generated in the request page. One of them is ‘Display Settings’ which responsible for what you want to do with the response results:

    • Display Vs. Save then Display: WPRM give a chance to save selected items from the results in database or just display it in runtime. Most of times we will just choose ‘Display Only’.
    •  Where to display results: decide where to show the results template by selecting post type and then before or after content or you can just use a shortcode to add it wherever you like. Choose ‘shortcode’ and save the code that shows up.
    • Showing repeatable results: select where or not to loop through results. This used only when you have a group of results that share the same group of parameter keys which is the case here.
    • Start Checking dummy data: add all values in this section with full fixed(no dynamic work is here) dummy(but correct) data then click ‘check request’ once you see results check only the data that you want it to show up in your app or widget.
      mashreqdis
  4. Templating Results:
    Now every thing is ready except how the results will look like!! so here comes the last section, ‘Template Settings’. You will find in it 2 parts:

    • Header or non-repeatable Template: this is the template that show results one time. you can add text inline style and html to template you results. The links below the editor is a shorthand to help you copy and paste to the editor:
      • the results keys
      • Functional helpers like: if/ifExists/section
    • Repeatable template: if you have a repeatable data you will setup this template exactly as the above to be in for each repeated item at a time.mashreqtemp
      Save the changes once you finish.
      mashtest

To see the output, we’ll copy and paste the shortcode from the display section in any text widget in your theme. It’s very simple but you can style it as you like.

Advertisements

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