GithubHelp home page GithubHelp logo

tomjackman / fh-api-mapper Goto Github PK

View Code? Open in Web Editor NEW

This project forked from feedhenry-templates/fh-api-mapper

0.0 1.0 0.0 6.35 MB

API Mapping Tool

License: Apache License 2.0

JavaScript 91.64% CSS 6.73% HTML 1.47% CoffeeScript 0.16% Shell 0.01%

fh-api-mapper's Introduction

#RedHat Mobile API Mapper Build Status The API Mapper is a visual tool for transforming the response of JSON APIs. It allows users to:

  • Rename Fields
  • Exclude fields which are not needed
  • Transform Fields using built-in transformations, or custom transforms they have defined themselves

##Setup There are currently some workarounds needed to get the API Mapper up and running within the platform.

  1. Make the newly created service public Public Service
  2. Visit the data browser, depending on your application configuration a "Upgrade Database" action will be available, this means the application is using an old/legacy shared database and it needs to be upgraded to use a dedicated one. Note the application needs to be first finished its initial deploy and be running to perform this task. Public Service
  3. Re-deploy the service
  4. You can now use the API mapper under the "Preview" section of the studio. The mapper can be popped out of the studio fullscreen by visiting the deploy host of this service in a web browser.

##Creating your first request Today, we're going to connect to a GitHub JSON API.

  1. To get started, from the home page of the api mapper, select "New Request".
  2. In the URL field, paste the URL of our API: https://api.github.com/repos/feedhenry-templates/fh-api-mapper
  3. Our API requires us to add a User-Agent header field. In the "Header Key" value, write User-Agent. In the "Header Value" field, write FHApiMapper.
  4. Let's set up where this API gets mounted. Select the "Mount Path" tab, and enter a mount path - we're using /thisrepo.
  5. Click 'Create Request'. We have now saved our first request.
  6. Let's try this request. Click the blue "Send Request" button.
  7. In the "Response" section, verify the "Response Headers" and "Response Body" sections appear as expected. You can now see the response body.

##Add a Mapping

  1. Once we've created and saved a request, we can then add a mapping. Click the blue "Add a Mapping" button.
  2. Once the mapping is created, we see a list of fields the API returns on the left. This API has a lot of fields. We're now going to modify the response which our API returns.
  3. Click the "owner" field, and the "Field Mapping" panel should update. We can now define transformations on this field.
  4. We're going to discard the "owner" field, and all of it's children. Untick the "Use this field" box. The change is automatically saved.
  5. Let's rename the id field to be called _id. Select the "id" field on the left, and in the "rename field" box, type _id.
  6. Lastly, we're going to repurpose the boolean private field to be a public field. As part of this, we're also going to invert the value of this field. As before, rename the field from private to public. Now, select a transformation called "invert".
  7. Now that we've transformed our response, let's see it in action. In the "Response" section, visit the "Response Body" tab. On the left is the previous API response. On the right is the Mapped Response.
  8. Verify the mapped response has a field named "public" set to true, and the id field now reads _id. Verify there is no owner field.

##Using the Mapped API

  1. Now that we've mapped our API, let's make use of this. First, we're going to test the mapped API using the command line.
  2. Navigate to the "Sample Code" section. Copy the "cURL Request" to the clipboard, and try the command in a *nix terminal.
  3. Verify the mapped response is returned.
  4. You can also use the Node.js code snippets. Copy the Node.js Request Module snippet into a new file called test.js. You can then run this file from the terminal by running node test.js. You should see the mapped response output.

##Writing your own Mappings As well as using built-in mappings, you can also write your own transformation functions. Here's how.

  1. In the studio's code editor, open the application.js file in the root directory.

  2. You'll notice the API mapper route is instantiated by providing one optional transformation, called mixedArrayTransform. By looking at this, you can probably figure out how to add your own!
    We're going to add a transformation called 'even', which changes even numbers to 0, and odd numbers to 1 - really simple! Here's the implementation:

    // First, tell the mapper it operates on numbers exports.type = 'number'; // then, implement the function. exports.transform = function(n){ return n%2; };

  3. Now that we've created our transformation file, we need to include it in application.js. We'll replace the instantiation of the API mapper route with something like this:

    app.use('/', require('./lib/api')({
    transformations : { even : require('./transformations/even.js') } }));

  4. You can now use your new transformation on numeric types!

Developing

grunt serve

Open http://localhost:8001/

Running Tests

grunt test

fh-api-mapper's People

Contributors

andresgalante avatar cianclarke avatar david-martin avatar grahamhillis avatar grdryn avatar jasonmadigan avatar lfryc avatar maleck13 avatar matzew avatar nialldonnellyfh avatar odra avatar pb82 avatar wtrocki avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.