SatelliteQE / nailgun

Why use a hammer when you can use a nailgun.
GNU General Public License v3.0
46 stars 83 forks source link

Use new "json" argument in Requests #213

Open Ichimonji10 opened 9 years ago

Ichimonji10 commented 9 years ago

Requests now offers a json argument. Consider this sample code:

import json
import requests

r = requests.post(
    'https://httpbin.org/post',
    data=json.dumps({'my': 'json'}),
    headers={'Content-Type': 'application/json'}
)

The same code can now be simplified to this:

import requests

r = requests.post('https://httpbin.org/post', json={'my': 'json'})

References:

This is great. Let's take advantage of this capability by doing the following:

  1. Change data={...} to json={...} and drop the header assignment wherever possible. (But do not touch nailgun.client!)
  2. Drop all references to nailgun.client from the other modules.
  3. Deprecate module nailgun.client, and eventually drop it.

Points 2 and 3 are worth some extra discussion. The purpose of module nailgun.client is to do the following:

Most of the automatic JSON encoding and header twiddling can now be done by Requests itself. We'll still need to explicitly set headers in cases where we're uploading files, but that's rare enough that I think it'll be easy to find and fix those few locations.

Logging requests and responses is a tougher nut to crack. Adding logging statements to every single *_raw mixin method and all of the helper methods is a repetitive and fragile solution. It may be worth spending some time reading up on what Python's standard framework provides. It may be that it's easy to just log out information using logging tools built in to Requests.

elyezer commented 9 years ago

:+1:

sthirugn commented 9 years ago

:+1: