http2py.py2request

Won’t show the details here, but if you are used to it’s not that hard, you just need to find the definition of the API, read it, figure out what part of the request you need in the URL and what you need to put in the “payload”, figure out how those _inputs_ of the request need to be formated, construct the URL and the payload according to your newly acquired knowledge of this specific API, make a web request (say with urllib.request or requests), extract the information you need from the response object (often in .contents or .json()), and often process the data further to get it in the format you can use it directly (say a list, a dict, a dataframe, a numpy array, etc.).

And if you’re experienced, and have felt the pain of needing to reuse or adapt your code, you’ll clean things up as soon as you figure this puzzle out. You’ll divide your code into separate concerns, encapsulate these concerns in functions and classes, and offer a simple, intuitive, python-like interface that reflects the simplicity of what you’re actually doing: Just getting some data. Something like:

` nice_python_obj_I_can_use_directly = get_that_darn_data(query, using, my, words, values, and, defaults='here') `

The details being hidden away, as they should.

And that’s fine. You’ve done well. Congratulate yourself, you deserve it.

Now do that again and again and again, and sometimes under the pressure of a deadline that depends on this data being acquired.

Are you enjoying yourself?

There must be a better way…

http2py.py2request.DFLT_METHOD_FUNC_FROM_METHOD_SPEC(method_spec, *, function_kind='method', dispatch=<function request>)

Makes function that will make http requests for you, on your own terms.

Specify what API you want to talk to, and how you want to talk to it (and it talk back to you), and get a function that does exactly that.

Essentially, this factory function allows you to take an API specification, specify how you want it to relate to python objects (how to convert input arguments to API elements, and how to convert the response of the request, for instance), and get a method that is ready to be used.

What does a method_spec contain? Well… consider first this. The core of this code is this: ```

request_kwargs = dict(**method_spec[‘request_kwargs’]) # to make a copy … # a bunch more code that updates request_kwargs according to other keys of method_spec … # … and some other debugging hooks r = request(**request_kwargs) # call the request if ‘output_trans’ in method_spec: # see if there’s an output_trans function

r = method_spec[‘output_trans’](r) # … if so, use it to extract what you want from the response

return r

``` So you can do almost everything you need with one single key: ‘request_kwargs’ and ‘output_trans’ alone. But there wouldn’t be as much advantage over just calling requests if that’s all there was to it, so we offer some other special keys to cover some of the common patterns.

  • ‘method’:

  • ‘url_template’: Specify the url, but with named placeholders: Example ‘http://base.com/{user}/{action}’.

  • ‘json_arg_names’: Specify the names of arguments of the function that should be put in the json load

  • ‘debug’: ‘print_request_kwargs’ or ‘return_request_kwargs’

  • ‘input_trans’: Function applied to

  • ‘output_trans’: A function applied to response object to extract what we want to return.

  • ‘wraps’: A function whose signature we should use as the output’s function’s signature

Parameters

method_spec – Specification of how to convert arguments of the function that is being made to an http request.

Returns

A function. Note: I say “function”, but the function is meant to be a method, so the function has a self as first argument. That argument is ignored.

class http2py.py2request.Py2Request(method_specs=None, method_func_from_method_spec=<function mk_request_function>, **kwargs)[source]

Make a class that has methods that offer a python interface to web requests

class http2py.py2request.UrlMethodSpecsMaker(url_root, constant_url_query=None, **constant_items)[source]

Utility to help in making templated method_specs dicts to be used to define a Py2Request object.

http2py.py2request.mk_request_function(method_spec, *, function_kind='method', dispatch=<function request>)[source]

Makes function that will make http requests for you, on your own terms.

Specify what API you want to talk to, and how you want to talk to it (and it talk back to you), and get a function that does exactly that.

Essentially, this factory function allows you to take an API specification, specify how you want it to relate to python objects (how to convert input arguments to API elements, and how to convert the response of the request, for instance), and get a method that is ready to be used.

What does a method_spec contain? Well… consider first this. The core of this code is this: ```

request_kwargs = dict(**method_spec[‘request_kwargs’]) # to make a copy … # a bunch more code that updates request_kwargs according to other keys of method_spec … # … and some other debugging hooks r = request(**request_kwargs) # call the request if ‘output_trans’ in method_spec: # see if there’s an output_trans function

r = method_spec[‘output_trans’](r) # … if so, use it to extract what you want from the response

return r

``` So you can do almost everything you need with one single key: ‘request_kwargs’ and ‘output_trans’ alone. But there wouldn’t be as much advantage over just calling requests if that’s all there was to it, so we offer some other special keys to cover some of the common patterns.

  • ‘method’:

  • ‘url_template’: Specify the url, but with named placeholders: Example ‘http://base.com/{user}/{action}’.

  • ‘json_arg_names’: Specify the names of arguments of the function that should be put in the json load

  • ‘debug’: ‘print_request_kwargs’ or ‘return_request_kwargs’

  • ‘input_trans’: Function applied to

  • ‘output_trans’: A function applied to response object to extract what we want to return.

  • ‘wraps’: A function whose signature we should use as the output’s function’s signature

Parameters

method_spec – Specification of how to convert arguments of the function that is being made to an http request.

Returns

A function. Note: I say “function”, but the function is meant to be a method, so the function has a self as first argument. That argument is ignored.

http2py.py2request.raw_response_on_error(func)[source]

A useful output trans decorator that will return the raw response if the output_trans raises an error. The response object will also contain the error that was raised, in the response.output_trans_error attribute.