golang: swagger REST api documents generator - python

I've searched internet but couldn't find a good solution. I'm looking for something that pretty specific - a golang copy of aiohttp_swagger.
That is a Python package which does magical things. In the endpoint handler method, one write some comment code and it'll be automatically parsed and generates swagger documentation. When the server is up and running, a special URL /api/doc handler will be inserted into the server where people can see it live.
I understand it's tied into the implementation of aiohttp framework and the way Go is used, web framework is not as popular (not using one myself), but I still like that solution very much and would like to find something in the Go land.
So my question is a bit open I guess: is there an equivalent (or rough) of aiohttp_swagger package in Go?

go-swagger does it based on doc comments: https://goswagger.io/generate/spec.html
go-restful has a builder for creating swagger 2.0 documents: https://github.com/emicklei/go-restful-openapi

Related

Swagger and then flask or opposite

Swagger and then flask or opposite
Hello,
I'm starting a new poroject who need an API.
I 'm going with flask and swagger.
A simple question: should i start by defining the API with swagger and then generate the python code or the opposite.
Thank for your help.
There are solutions for both approaches:
If you end up preferring to define the API first, I would recommend connexion. You define OpenAPI (new name for Swagger) specification files, and then you do Python code accordingly. I would say that this is the best approach as you can guarantee that whatever code you write afterwards will conform to the specification, that you can provide to whoever wants it. Another advantage is not mixing up core concepts of your logic with the API specification.
Otherwise, flask-restplus does the trick. This is the most common (but not necessarily better) approach, where you write your Python code and the specification is then generated. This is the approach I usually follow in simple use cases.
For information i finally choose the openapi-generator tool that allow me to generate python server stub on Flask/connexion stack and my client lib for Android on top of okhttp.
https://github.com/OpenAPITools/openapi-generator
I was rather satisfied with this choice during my project.
To merge your server stub last version with older one you can dedicated a git branch for raw server stub generation and another one wich you fill with your own code. That way you won't lose your previous version with the code you add to the server stub.

Flask Custom Login and Template Rendering

I am new to learning Python for web development and have decided to go with Flask as my framework of choice as of now. I have chosen this primarily for its bare-bones approach on web development and I want to make as much of my web app custom and on my own as possible. I have made a very basic MVC framework in PHP and would like to make something like this in Python. I was researching some ways to interact with a database and add user login/register support and my original thoughts were that I wanted to do this all custom with my own methods and objects and what not. Similar to how I have done in PHP, so I can learn as much about developing my own back-end efficiently for long term production projects. In some research I found the basic objects in Flask such as Login Manager, Login-Form and Flask-Admin, etc. I don't like the idea of using these nicely packaged things that I have very little control over and have not dev'd custom.
Using that kind of stuff I also feel that I am not learning how all of that stuff works on the lowest level so I could not reproduce some of the benefits they are giving me later on in life when project circumstances change and maximum flexibility, customization, maintainability, and efficiency is needed. Obviously I am not in that position right now, but I DO know how to write this back end stuff in PHP and am just wondering if that means anything as far as managing sessions and Authentication on my own in Python/Flask, or if my thinking is totally out of ocontext and its not even close to managing the same stuff.
It is a really great idea to learn how authentication works at a low level -- it's particularly important as many people never learn this stuff, and it's quite interesting!
What I'd recommend you do is take a look at the official Flask tutorial (http://flask.pocoo.org/docs/0.10/tutorial/), as it covers a lot of this (working directly with sessions, etc.).
What I would not recommend, however, is using this sort of thing in production.
Using your own authentication code is almost never a good idea -- it's much better to rely on a well supported library that has been audited by other people for security issues.
In the Flask world you've got a couple choices:
Flask-Login: https://flask-login.readthedocs.org/en/latest/
Flask-Security: https://pythonhosted.org/Flask-Security/
Flask-Stormpath: http://flask-stormpath.readthedocs.org/en/latest/
Of those 3, I really like Flask-Stormpath -- but I'm super biased as I wrote it =)
Flask-Stormpath supports the widest array of customization / etc., and allows you to do whatever you want with it.
Hope that helps!
Flask official doc have some app examples. One of them is MiniTwit a micro Twitter clone. As it is a complete app, you could found much of what you're looking for. If you want to make something greater, try Full Stack Python Flask tutorial

Versioning a TastyPie Api with the Accept header

I'm not trying to start a religious war, but I personally really don't like api version information in the URL of a resource. I think the best way to do it is via the Accept header of the resource or adding a ?version=2 to the query string. If you are curious about this topic. There are a number of good (an passionate) posts on StackOverflow on the topic. Here is a good thread here. Also, IMHO, a good blog post here by Steve Klabnik. Again, these are just my perferences, and I'm not trying to create a thread on this topic (again).
I'm currently looking for a Django package to help with creating a RESTful API. After some reading, it seems like TastyPie has most of what I want/need and is well supported (and has really good docs). And I'm just wondering if there is a way to implement a different versioning scheme? Has anyone else out there done this? Is there another package that might work more the way I want?
Yes, you can use Accept headers or any other method to version your API, and do this in a way that is not specific to whatever Django API package you are using. One easy way to do this is to add some middleware to check for the headers on relevant requests and then load the appropriate URL conf depending on the version specified.
There are several simple apps on github that use URL routing middleware that you can customize to meet your needs.
Also, Tastypie is amazing and I highly recommend it over Piston after using both.

Python REST frameworks for App Engine?

Any pointers, advice on implementing a REST API on App Engine with Python? Using webapp for the application itself.
What I currently know is that I can:
hack up my own webapp handlers for handling REST-like URIs, but this seems to lose its elegance for larger amounts of resources. I mean, it's simple when it comes to temperature/atlanta, but not so much* for even a rather simple /users/alice/address/work (though do keep in mind that I'm not saying this after having implemented that, just after spending some time trying to design an appropriate handler, so my perception may be off).
use the REST functionality provided by one of the bigger Python web frameworks out there. I have some unexplainable sympathy towards web2py, but, since it's not used for the project, bundling it with the application just to provide some REST functionality seems.. overkill?
(Huh, looks like I don't like any of these approaches. Tough.)
So here's me asking: what advice, preferably based on experience, would you have for me here? What are my options, is my view of them correct, did I miss something?
Thanks in advance.
I had a similar issue. Wanting to quickly get my DataStore exposed via REST to WebApps.
Found: AppEngine REST Server.
I have only used it lightly so far, but it certainly appears to be very useful with a small amount of work. And it does use webapp as you suggested.
ProtoRPC is bundled with the SDK, and it is robust and actively developed (however experimental). Although I think the source code itself is a little convoluted, the feature-set is pretty complete and it was made by someone with experience in creating this kind of library. It supports transmiting using JSON, ProtocolBuffer and URL-encoded formats.
Also, you can create APIs that work on the server side and client side -- it defines a 'message' protocol with implementations in Python and JavaScript. I used other "RESTful" Python libraries, but no other provided this consistency out of the box.
Here is the project page and here is the mailing list.
Edit: maybe their documentation is lacking some keywords, but just to be clear: one or the purposes of ProtoRPC is to provide a solid foundation to create REST services.

Python for web scripting

I'm just starting out with Python and have practiced so far in the IDLE interface. Now I'd like to configure Python with MAMP so I can start creating really basic webapps — using Python inside HTML, or well, vice-versa. (I'm assuming HTML is allowed in Python, just like PHP? If not, are there any modules/template engines for that?)
What modules do I need to install to run .py from my localhost? Googling a bit, it seems there're various methods — mod_python, FastCGI etc.. which one should I use and how to install it with MAMP Pro 1.8.2?
Many thanks
I think probably the easiest way for you to get started is to work with something like Django. It's a top-to-bottom web development stack which provides you with everything you need to develop and run a backend server. Things can be very simple in that world, no need to mess around with mod_python or FastCGI unless you really have the need.
It's also nice because it conforms to WSGI, which is a Python standard which allows you to plug together unrelated bits of reusable code to add specific functionality to your web app when needed (say for example on-the-fly gzip compression, or OpenID authentication). Once you have outgrown the default Django stack, or want to change something specific you can go down this road if you want.
Those are a few pointers to get you started. You could also look at other alternative frameworks such as TurboGears or paste if you wanted but Django is a great way to get something up and running quickly. Anyway, I'm sure you'll enjoy the experience: WSGI makes it a real joy knocking up web apps with the wealth of Python code you'll find on the web.
[edit: you may find it helpful to browse some of the may Django related questions here on stack-overflow if you run into problems]
You asked whether HTML is allowed within Python, which indicates that you still think too much in PHP terms about it. Contrary to PHP, Python was not designed to create dynamic web-pages. Instead, it was designed as a stand-alone, general-purpose programming language. Therefore you will not be able to put HTML into Python. There are some templating libraries which allow you to go the other way around, somewhat, but that's a completely different issue.
With things like Django or TurboGears or all the other web-frameworks, you essentially set up a small, stand-alone web-server (which comes bundled with the framework so you don't have to do anything), tell the server which function should handle what URL and then write those functions. In the simplest case, each URL you specify has its own function.
That 'handler function' (or 'view function' in Django terminology) receives a request object in which interesting info about the just-received request is contained. It then does whatever processing is required (a DB query for example). Finally, it produces some output, which is returned to the client. A typical way to get the output is to have some data passed to a template where it is rendered together with some HTML.
So, the HTML is separated in a template (in the typical case) and is not in the Python code.
About Python 3: I think you will find that the vast majority of all Python development going on in the world is still with Python 2.*. As others have pointed out here, Python 3 is just coming out, most of the good stuff is not available for it yet, and you shouldn't be bothered about that.
My advise: Grab yourself Python 2.6 and Django 1.1 and dive in. It's fun.
Django is definitely not the easiest way.
check out pylons. http://pylonshq.com/
also check sqlalchemy for sql related stuff. Very cool library.
On the other hand, you can always start with something very simple like mako for templating. http://www.makotemplates.org/

Categories

Resources