A Very Simple API 

development-api

There comes a time in the life of a developer where he/she is forced to deal with an API, be a desktop or a web developer, in some time it will be necessary.

hot-head

Also, maybe, the developer will need to create it’s own API and then is where the things get a little bit difficult. There is a lot of concerns, discussions and suggestions around API standards but none of them are still the definitive right way to build an API, so as always each developer builds the code as it wants.

For me, everytime that I need to deal with a webservice, I study a lot, check the new standards look at the client needs and make the best effort to make the most light and precise services that the client wants. But thats the hard way, the easy way should be, to unpack a generic API code adjust some settings and start to delivery services… Should be like this.

crazyHarry

There are some nice tutorials and tools on internet about building an API, like with laravel and with apigility, but almost all have some framework dependencies and then I though in something more simple, just with the basic stuff, why not?

 

Considering all that, and on all the effort spend on every new webservice, I decided to make a very simple API pack with the basics to make a webservice to work, so here is what I tried to covered:

  • Auto detect XML-RPC or REST request;
  • Standard formated XML or JSON automatically (depending of the request);
  • Automatic deal with errors and/or warnings (saving in a log);
  • Resources to return standard field answers on requests;
  • Code injection protection;
  • Method request control and run according to the operation method request (POST, GET, PUT, DELETE, OPTIONS, PATCH);
  • Versioning control;
  • URL friendly following at least level 2 maturity level;
  • Start point to build my own custom webservice methods;

It’s not easy to cover all the resources of an API and still keep the code simple and dependencies free but I tried. In the end the result was very good but still have some work to do.

The code is avalibale on my github account and it’s free to use and to contribute. Note that this is not the “state of art” and any kind of contribution will be welcome.

The very simple API pack worked just fine and  can be used on every new project with just a few lines of set up on an specific settings file. Also, the use is very simple:

  1. Extend the main class AbstractService:
    class MyServiceName extends \classes\AbstractService
  2. Initialize the service with the needed settings:

    public function execute()
    {
    $this->initialize(['allowedMethods'=>[AMT_GET, AMT_POST, AMT_OPTIONS],
    'answerType'=>AST_AUTO]);
    }
  3. Add the code on the respective method and call the answer method:

    public function onPost()
    {
    //... your code here
    $this->answer([
    'message' => 'Here are the fields that the API received',
    'fields' => $this->getFields()
    ]);
    }
  4. Call the API from the browser:
    http://your-server-path/v1/MyServiceName

… and voilà!

vsapi-sample-answer
Sample JSON answer using AJAX on vsapi

Also, an error message:

vsapi-sample-error-answer
JSON error answer using AJAX on vsapi

And, a XML answer:

vsapi-xml-answer
XML answer using PHP on vsapi

Generic or Custom API, which one to use?

api-538x294

On the past month I was working in a project, that seemed to be easy on the beginning, to create a kind of service that should be the core to delivery coordinates and weather information of trucks, airplanes and ships from an specific company, and, the project has extended a little (as always), because the client also wanted to add some extra resources like user control, sell credits to use the service among other details. In fact, it wasn’t so easy as I expected but in the end, we did it.

Our team made some research before develop anything and concluded that an API should be the right service to complete this project.

An API is not just a JSON or XML content, it’s more than that, it can have many aspects and involve different types of access, depending of the needs, the most common types are the RESTFul service which is more like an architecture over HTTP using different methods to make the operations and the RPC (Remote Procedure Call) service which is usually done via POST method sending payload information to define the operations. It was important to us to know exactly what are the client needs and what we should start to use.

Knowing about these and some other dificulties of building a web service API, we did what any sane developer would do… search on internet for some basic and simple API to build over it our own solution. Well, we didn’t find something easy and simple, but we have found the Zend apigility which is a fantastic API-based architecture and is based on Zend Framework, so we just gave a chance for it.

ag-hero

The apigility tool offered a great tutorial, many sample codes and can be setup almost by the admin enviroment, also, a complete set of answer types, allowed methods, filters, validations and is also documentation ready, it’s really amazing.

intro-first-rest-service-status-settings

But, “with great power comes great responsibilities”. The apigility is based on Zend framework, so it’s implicit necessary to have a good knowledge on the framework to make  a good job, also, we don’t felt like this is the right solution for this project, it was more like “A cannon to hunt a little bird”, and more crucial, the Zend framework would push us to do extra documentation and can be a possible problem in future updates, for the client, as we don’t have too much professionals available with this knowledge. So, we give up of apigility for now.

After some long discussions, following the team instincts, feelings, making some sketches, we kept the idea of making by ourselfs a PHP web service API, which should exactly fit our needs without lack or overage of resources.

There is a lot of discussions around the API standards and if it should be adopted or not, among them are the swagger specification and the Open API Initiative which are supported by many stakeholders like Google, Microsoft, IBM, Paypal and other giants.

We didn’t followed all the standard specifications that we found, but we focused in almost all of them to finish our API.

Was it worth? Well, we were a team of 3 person, we planned, documented, developed, tested and done the service in about 5 days and the client is happy and the project is working, so, the team and the client thinks it was worth it, that’s what matters. 🙂

api-sample-use
Sample use of the tracking API

Web Development Trends – Jul-2017

There is no doubt that developers/coders are one of the most requested professionals in the market these days. According to a research made by the Michael Page Recruitment Services, software engineers and developers are the most requested professionals on 82% of the countries that the research covered (Brazil, USA, Japan, UK, France, Chile, Mexico, Canada, Australia, Germany, Netherlands, Spain, Portugal, Italy, Sweden, Denmark, Switzerland, Ireland).

You may be thinthing “Yes, I’m in!”, but wait, you must be updated with the new technologies to be a part of this research.

Recently, looking at many job offers on linkedin that pops up on our profiles I surprisingly saw, besides long experience, a lot of new technologies and languages mentioned on the “you must have” session, some of them are obviously needed like HTML5 and CSS3 but some others are a few emerging ones like the javascript frameworks between others.

So, I listed here some of these must have topics that I found (Not in order of importance):

  • HTML5 + CSS3
  • Python, Ruby, Java, PHP (Some are already requesting the PHP 7)
  • PHPUnit Testing
  • ECMAScript 6 (Javascript)
  • Javascript Frameworks (NodeJS, AngularJS, ReactJS)
  • MongoDB, MySQL, SQL Server
  • AWS (Amazon Web Services)
  • Git
  • CollaborativeWeb Enviroments (Docker, Vagrant Box)
  • DevOPs
  • Frameworks (Zend, Laravel, Magento, Symfony)

These are not the only requests that I’ve been seen, but, almost of them was present in the jobs propositions. Anyway, this list shows that the professional of technology, specially the developers, are quickcly discarded if is not up to date with these new trends.

We must keep an eye on everyday news and never think that we already know everything. Opportunity calls people that are ready.