RESTful API versioning using Tornado - python

I have a RESTful service which was written using the Python asynchronous web framework Tornado
I am trying to support API versioning inside my RESTful service, so I can support simultaneous versions of the same API.
One clear way is to simply add the version to the URL path and route each path to a different API class, as follows:
http://mydomain/api/v2/myapi/myarg
http://mydomain/api/v1.1/myapi/myarg
http://mydomain/api/v1/myapi/myarg
But this will result in a tangled and duplicated code.
Another way would be to get the version by header, and check the version inside the API class, and operate accordingly.
This way is also not very native, and seems like a patch to solve the use of API versioning.
It will force me to call the "IF-SWITCH" code that checks the version explicitly in each API class.
Is there a way to do API versioning in Tornado in a more native way?
(where the versioning is treated using some hidden Tornado code, such that is done with other Tornado decorators).

Related

How can I Integrate Python Scripts into a MERN stack

I am thinking of adding python scripts for some data analysis but I am using a MERN stack for my application.
An example of what I am thinking-
My python script will do some data analysis when an endpoint is being called.
../api/dopythonhere and return a JSON object
What would be the best/efficient way to implement python with a MERN stack?
So as far as I understood you have currently an API or full-build functional application made in or partially coded in Python (unknown stack). Therefore you want to migrate this to MERN Stack or call the Python API via your MERN application. There are several things you can do to work with this, but those two might be the cleanest:
Completely migrate and integrate your Python app into your Node.js & Express app and forget about the Python source code.
If it's not already done, make the Python app a functional web API. Therefore you can call it either from the Node API using libraries like Axios or similar (so in your API you can modify the data structure and only send to the front what you need), or you can also call the Python API directly via React front-end, using web development vanilla APIs or any libraries you want.

Best protocol of Web Services for Python based applications

Can anyone suggest best protocol to be used for Web Services (SOAP or REST) for Python for real time data and historical data fetching.
Please suggest the libraries available if any.
REST protocol should be used.
For Django based application use Django rest framework package
For Flask based application use Flask restful package

Are there any known negatives to using Requests in Flask to interface to Cloudant on Bluemix?

I am writing an app in Python Flask that makes use of the Python HTTP library Request to interface with Cloudant on Bluemix. It is an easy interface that allows me to directly access the Bluemix VCAP information for Cloudant and of course the Cloudant API. However it does not make use of the CouchDB package, which seems to be the most popular way to inteface to Cloudant.
Are there negatives in staying with Request as I scale up, and if so what would they be?i
The main advantage of using a Cloudant/CouchDB library is that you write less code. This can be significant in languages like Java where Rest and JSON handling is very cumbersome. However working with Rest and JSON in python using standard libraries is very easy.
However, the main disadvantages of using a Cloudant/CouchDB library are:
you have less control over the interaction with Cloudant which may make things like session management and http connection pooling much harder.
You don't get to learn the Cloudant API as this is abstracted away from you by the library.
Some libraries allow you do to things which can be problematic for scalability such as py-couchdb's functionality for creating temporary views.
Libraries may not implement the full Cloudant API so you may end up having to make Rest/JSON calls to access these features not implemented by the library.

Access Google cloud endpoints on a non-existent version label

I have two apps, my_app and my_endpoint_app. I can access my_endpoint_app with any version label in the URL I want and it will automatically route to the default version if it does not match an existing version.
Example:
https://josh-dot-my_endpoint_app.appspot.com/ will respond with the default version since there is no josh version deployed.
If I try to do the same with a Google Cloud Endpoint service call, I get a Not Found error.
Example:
The unsuccessful https://josh-dot-my_endpoint_app.appspot.com/_ah/api/myendpoint vs the working https://my_endpoint_app.appspot.com/_ah/api/myendpoint
I have a couple of Google AppEngine applications that communicate with each other via Cloud Endpoints.
Under normal usage this is OK because I know the version beforehand and avoid these errors. In our development environment, this falls apart. In order to support feature branches and testing in isolation, we push our code up to appspot using the -V switch of appcfg.py.
Example:
appcfg.py -A my_app -V josh update .
Now I can access my feature branch at https://josh-dot-my_app.appspot.com. In order to support some version label hackery, I dynamically calculate the right endpoint app to call with something like s/my_app/my_endpont_app/g and then make my service calls there. This fails because of the dynamic version label not existing. If I push a version label with that name it completes as expected.
Is there any way to get Cloud Endpoints to answer on non-existent version label hostnames?
Scenarios that I want to support
https://my_endpoint_app.appspot.com/_ah/api/myendpoint
Main application URL, routes to default version
https://josh-dot-my_endpoint_app.appspot.com/_ah/api/myendpoint
Version does not exist, should route to default version
https://new-feature-dot-my_endpoint_app.appspot.com/_ah/api/myendpoint
Version new-feature exists, should route to new-feature version so that we can test new code in isolation before merging into the main code branch. This would be internal apis that the current endpoints might make use of without changing what the endpoint accomplishes. (performance improvements, etc)
You can reroute any Url to any module/version via the dispatch file.

Best way to get xml-rpc and django working together

I have worked with Django for a while but I am new to xml-rpc. I have two Django servers running and the first needs to call functions from some modules of second server. I find xml-rpc easiest way to do so but don't want to run a separate server for this only.
What options do I have? Can I run Django's web-server and xml-rpc server with a single manage runserver command ?
Easily - we use http://code.djangoproject.com/wiki/XML-RPC to add an xml-rpc server into our django server.
You may also consider David Fisher's rpc4django which supports both XMLRPC and JSONRPC within a single package. Features include:
Detects request type (JSONRPC or XMLRPC) based on content
Easy identification of RPC methods via a decorator
Pure python and requires no external modules except Django
Customizable RPC method documentation including reST
Supports XMLRPC and JSONRPC introspection
Supports method signatures (unlike SimpleXMLRPCServer)
Easy installation and integration with existing Django projects
Ties in with Django’s authentication and authorization
Try: http://pypi.python.org/pypi/django-xmlrpc

Categories