sexta-feira, 16 de outubro de 2015

CFapi - Creating REST / JSON / SOAP / WSDL / CSV / KML / GeoJSON APIs dynamically

Although this blog is about Android development, I kindly ask my reader´s permission to talk here about an off-topic subject.

Today I´m going to show you a little bit about the backstage part of mobile application development. By "backstage", I mean the back-end part of the software, i.e.: The API.

Not only I am an Android application developer, I am also a Coldfusion developer. And I usually do my back-end software in Coldfusion language.

Having done dozen of Coldfusion APIs that feeds Open Data CKAN sites, and various Android APPs, I some day had this idea of creating an API Dynamic Generator.

It would work like this: I would take a basic API project and turn it to a template with replaceable terms in the code. Then I would ask the developer some details about his database and which database view/table he/she was going to expose as open data or to feed a mobile APP. Then the dynamic API generator would take these information and create by its own a brand new API, making the data available in REST/JSON, SOAP/WSDL,  CSV, KML and GeoJSON formats to be integrated with any software.

Well... That idea became the project CFapi (Coldfusion Dynamic API Generator). It is available now at http://CFapi.riaforge.org and it is open source. You can download, use, share, modify, at your will. Enjoy!

Let me show you a step by step guide to use CFapi to create a new API:

1) Download CFapi in http://CFapi.riaforge.org
2) Extract CFapi.rar file contents to your Coldfusion developer server.
3) Open your web browser and point it to http://localhost/cfapi/index.cfm
    You will see this screen:


 At first run, CFapi comes prepared to use Coldfusion´s default cfbookclub datasource as the main source of data to create an API. Also, I choose to expose, as an example, two of its tables: authors and books. I decided that the generated API (bookclub) user would be allowed to search for authors using a "lastname" filter and for books using "title". And that´s all that is needed!

4) Press the "Generate API" button. Wait some seconds. If everything went ok,
    the following screen will show up:




This means that CFapi created successfully the Bookclub API with all features, i.e.: Rest, Soap and CSV entry-points.

5) Now you have to download the created API and deploy it to your development server. This is done by clicking in the first item "Click here to download your API Project zip file".  Then you extract that file´s content to your Coldfusion web root folder.

6) Now you have to call the main page of your new created API: http://localhost/bookclub/index.cfm. This will fill the memory caches for the first time. If you skip this step, when you consume the entry- points, you will get "no results found" message. The following screen will show up:




This is the welcome screen of your recently created API, showing the available entry-points to the consumer and the needed arguments. An access token is always needed, as access control. Of course you have to use https on your production server to hide it. there is also a "pretty" parameter for REST/JSON entry-points, which means that if set to true, the browser will show the JSON in a human readable format. Otherwise it will put everything in a single row, for optimisation purposes. The default value for "pretty" is false.

Another important parameter is "filter". This is where you should specify a search value so that the API will only return that value, if found. Notice that filter will use the search fields you have specified in the main form of CFapi application. In our case, "lastname" for authors and "tittle" for books.

In the SOAP entry-point you get a default WSDL file to be processed by your SOAP SOA system. I tested it with SOAPUI software and it worked flawlessly. As you can see in the pictures below (REST and SOAP output):

Rest/JSON output


WSDL output file









SOAPUI consuming WSDL


And also, a picture of standard JAVA program using httpGet() and a JSON extraction library to consume the REST/JSON entry-point. This program is available in the "examples" folder of CFapi project:




7) Last, but not least: The memory caches empty from time to time. It is mandatory thay you create a Scheduled Task in Coldfusion Administrator to call, from time to time (you decide how frequent) to call the "updateCache.cfm" file, located in "CFapi/ScheduledTasks" folder. This file is tailor made for your API. You just have to create the Scheduled Task in CF Admin to call it.


Observations:

* CFapi uses Google GSON library to generate JSON. Also, it uses Mark Mandel´s Javaloader project.

* You have to use https on your production server to hide the access token.

* For standard RESTfull URL compliance, you will have to use Web Server URL rewriting, to take the "*.cfm" file extension out of the URL.


Conclusion

I think that CFapi can give Coldfusion an important role on the API market. It represents a viable and fast (instant) solution in creating mobile back-end APIs and also for Open Data projects, like CKAN or Socrata based web sites.

Hope to hear feedback from developers and users as soon as possible.  Of course there is much room to evolve CFapi, and developers opinion is crucial on that subject.

So, please, leave a comment, send an email, contact me on Twitter or Facebook (luizMilfont).

Hope to have helped people with my CFapi project contribution.

Enjoy!




3 comentários:

  1. Respostas
    1. Thanks a lot for your comment. Please, if you publish some api generated with cfapi, let me know, to see how it goes. Best regards!

      Excluir
  2. Would love to try it out. But the download on RIAforge is broken. Get a 404.

    ResponderExcluir