Validate Request against OpenAPI YAML in Python - python

I have written API documentation using Open API 3 standards (using stoplight.io). Now I'm implementing the API in Python 3.7, and I don't want to rewrite all of the schemas, not to mention I'd like my API to be coupled with my documentation to prevent discrepancies.
I've found tools like pydantic and openapi-schema-validator that seem relevant, but don't fit my use case. For example, pydantic wants me to create the pythonic objects first and then export to json_schema and openapi-schema-validator wants just the json schema while I have an Open API yaml.
This seems like a standard question, but I haven't found a good answer. Thanks!

You could use openapi-core or connexion which both support request and response validation..

Related

Passing mongodb queries through REST API in Python

I am building a REST API to MongoDB using Flask-RESTful. One feature request I have received is to be able to pass arbitrarily complex Mongo queries, e.g. JSON docs, through the API. I am a little concerned about the implementation here. At first, I would just make a new endpoint (users/query/<json>). I was looking at other implementations, where it was passed through as a parameter (users/query?q={}). Now my questions are:
What is the preferred method?
Is this even a good idea?
Am I missing something?
Thanks!

Difference between API and API library/Wrapper

I'm kinda confused. So I understand that If we want to grab data from an API, we can just call that API url in whatever language we are using ( example in python, we can do urllib.open( url of api) and then load the json). My question is, if we can just open the url in any language, what's the point of the API libraries that developers usually have on the site ( library wrapper for python, java, c#, ruby, etc). Do we need to use a specific library to call an API in that specific language? Can we not just open up the API url in any language? What's the point of having a library in each language if we can just extract the API in each of those languages?
You don't need a library for the client. However, developers tend to like libraries because it helps with things like creating authorization headers, creating parameterized URLs and converting response bodies into native types.
However, there are good/bad ways of building these kinds of libraries. Many libraries hide the HTTP API and introduce a whole new set of issues that HTTP interfaces were originally designed to avoid.

How to use python for a webservice

I am really new to python, just played around with the scrapy framework that is used to crawl websites and extract data.
My question is, how to I pass parameters to a python script that is hosted somewhere online.
E.g. I make following request mysite.net/rest/index.py
Now I want to pass some parameters similar to php like *.php?id=...
Yes that would work. Although you would need to write handlers for extracting the url parameters in index.py. Try import cgi module for this in python.
Please note that there are several robust python based web frameworks available (aka Django, Pylons etc.) which automatically parses your url & forms a dictionary of all it's parameters, plus they do much more like session management, user authentication etc. I would highly recommend you use them for faster code turn-around and less maintenance hassles.

Noob Question: Python + Twitter + App Engine - Oauth

I'm sorry but I'm having some trouble implementing Oauth within my app engine python project.
I've been working from http://github.com/tav/tweetapp, but I don't think I have a strong enough grasp on this platform to understand how to implement this class within my main.py I'm building the rest of my app in.
This maybe a feeble attempt, but here is what I have so far:
twa = twitter_auth
client = twa.OAuthClient('twitter')
I've created a source folder within my project called "twitter_auth" and that contains a file within it called "twitter_auth.py" which contains the above linked library, and a file called __ init__.py (no space) which is completely empty.
I really have no idea what to do from here :/
Let me recommend taking a look at the tweepy library and some example tweepy apps. Specifically here: http://github.com/wasauce/tweepy-examples
This shows how to use oauth to authenticate a user: http://github.com/wasauce/tweepy-examples/tree/master/appengine/oauth_example/
As Hagge said, it sounds like your issue is more with the tweetapp library than with App Engine. However, if you would like to know more about OAuth on App Engine and if I may be allowed to link to myself, my two articles on the topic seem to be reasonably popular.
The tweetapp library was a an early prototype for Twitter OAuth on twitter. Tav did the heavy lifting and I deployed the site http://twitteroauth.appspot.com , using some of the tweetapp library. The actual source of that site is here (I need to update the site to point here): http://github.com/ryanwi/twitteroauth
I am still using it in production, but, it has aged and does not work for all API calls. I'd recommend trying a different, more up to date and maintained library as others have mentioned.
But, take a look at the twitteroauth source if you want to try to get a first attempt working.
These two are on Twitter's list
http://github.com/brosner/python-oauth2
http://code.google.com/p/oauth-python-twitter2/
I'm not familiar with that library, but after a quick look and seeing the warning that it is not maintained I'd search for something better. I implemented a simple Twitter connection based on Tornado's auth: see an example of how to make Twitter API calls here (and an authentication example here). In case you don't want to use tipfy, I recommend implementing the python-twitter library in your framework of choice.

How to perform a signed PUT request with OAuth in Python

How is this meant to work? Where are all the oauth_* values meant to go if not in an encoded body like a POST request? In what form do you sign it?
All the Python OAuth libraries I can find only support GET and POST. Does anyone know any that support all methods?
python-oauth2 supports all HTTP verbs -- as the comment I've linked to says, and I quote,
We use PUT extensively at SimpleGeo
(our python-simplegeo package uses
python-oauth2 and PUT requests).
The python-oauth2 package's client
(oauth2.Client) simply wraps httplib2,
which supports all of the verbs AFAIK.
So were did you get the weird notion that python-oauth2 doesn't support PUT?

Categories