initial commit with xml and an oauth2 token request helper #84
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hi Filipe (and Vinta Software),
I enjoyed your talk on Wednesday in NYC. I decided to use Tapioca to write a wrapper for the Google Sheets API. In the process, I implemented approaches to issues #78 and #12 (XML and Get Auth Token for Oauth2).
Please review and let me know if this is close to what you had in mind!
Here are some notes on the implementations:
Addition 1, XML:
1. Added an
XMLAdapterMixin
. I had a little bit of trouble with theresponse_to_native()
method in combination withetree.ElementTree
when the API endpoint was returning errors, my solution was to search for 'xml' in the content-type header and return a different response if it wasn't present.2. I created a custom dictionary format that can be translated into XML so that it's easy for a developer to type when they're exploring an API. For me, typing out an XML string is pretty bad, and typing out a highly nested dictionary representation of XML can be horrible. The format uses pipes to separate node tags from attributes in the dictionary's keys, and the dictionary's values become the text in-between the XML tags.
**How it works:**Translation to XML is handled in two stages. We use the
horriblenested dictionary in-between that maps directly onto theetree.ElementTree.Element
object.The "flat dictionary" format:
Is translated into the "etree element dictionary" format:
And is finally translated to the following XML bytes string to be handled by Tapioca:
As of now, the response format is returned only in the nested-dictionary format. I think the biggest challenge with handling XML via dictionaries in Tapioca is that developers may want to work directly with XML as either input or output (in addition to the other formats), which is not directly supported. Need to think about how to add those capabilities / amend the existing config requirements without making configuration or branching on input overly confusing or error-prone.
Edit: The latest commit f782f1f attempts to address multiple input and output formats through a single interface. The input is branched based on 4 types of input (etree.Element, string representing XML, nested dictionary, and flat dictionary).
Branching code:
Addition 2, Token Request:
Here, I followed the Requests-OAuthlib tutorial to make a helper for developers to request their tokens from Oauth services. They just plug in the required fields, follow the instructions, and it returns their token at the end. I haven't looked at this with respect to Oauth token request processes other than Google's.
I think this could go nicely into the cookiecutter since it'll be convenient to type parameters into a file / system variable instead of calling it through the console -- see how it's used in the tapioca-gsheets project.
Thanks for reviewing, and let me know if you'd like to discuss these in more detail or change the approach before considering a merge!