Add extra_data parameter + Add request_url property in UnirestResponse by ocervell · Pull Request #36 · Kong/unirest-python · GitHub
Skip to content

Add extra_data parameter + Add request_url property in UnirestResponse#36

Open
ocervell wants to merge 1 commit into
Kong:masterfrom
ocervell:master
Open

Add extra_data parameter + Add request_url property in UnirestResponse#36
ocervell wants to merge 1 commit into
Kong:masterfrom
ocervell:master

Conversation

@ocervell

@ocervell ocervell commented Dec 12, 2016

Copy link
Copy Markdown

Pretty often when I make asynchronous requests I need two things that are commonly implemented in asynchronous request libraries:

  • Access the request URL in the callback function.
  • Access extra data that I pass with my request and that I have to access in the callback.

Now I love using unirest because it's so simple and beautiful, but those are limitations that are too important to pass on.

A simple example is for logging / reporting purposes, when you need to log the request URL in the response and record the HTTP code .
I add an extra_data and request_url properties in the UnirestResponse to tackle these two problems.

Example use:

import unirest
import logging
log = logging.getLogger(__name__)

def callback(r): 
  task_id = r.extra_data['task_id']
  entry = "Task number: {0} | Request url: {1} | Response code: {2}".format(task_id, r.request_url, r.code)
  log.info(entry)

extra_data = {'task_id': 1}
unirest.get('http://unirest.io', callback=callback, extra_data=extra_data)

@ocervell

Copy link
Copy Markdown
Author

@CLAassistant

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants