@@ -103,38 +103,42 @@ following helper program can be used to create `gists
103103 # !/usr/bin/env python
104104
105105 import sys
106- import urllib2
106+ import requests
107107 import json
108108
109109 def do_gist_json (s ):
110110 """ Use json to post to github. """
111111 gist_public = False
112- gist_url = ' https://api.github.com/gists'
113-
114- data = {' description' : None ,
115- ' public' : None ,
116- ' files' : {
117- ' sample' : { ' content' : None }
118- }}
119- data[' description' ] = ' Gist from BPython'
120- data[' public' ] = gist_public
121- data[' files' ][' sample' ][' content' ] = s
122-
123- req = urllib2.Request(gist_url, json.dumps(data), {' Content-Type' : ' application/json' })
124- try :
125- res = urllib2.urlopen(req)
126- except HTTPError, e:
127- return e
112+ gist_url = " https://api.github.com/gists"
113+
114+ data = {
115+ " description" : " Gist from bpython" ,
116+ " public" : gist_public,
117+ " files" : {
118+ " sample" : {
119+ " content" : s
120+ },
121+ },
122+ }
123+
124+ headers = {
125+ " Content-Type" : " application/json" ,
126+ " X-Github-Username" : " YOUR_USERNAME" ,
127+ " Authorization" : " token YOUR_TOKEN" ,
128+ }
128129
129130 try :
131+ res = requests.post(gist_url, data = json.dumps(payload), headers = headers)
132+ res.raise_for_status()
130133 json_res = json.loads(res.read())
131- return json_res[' html_url' ]
132- except HTTPError, e:
133- return e
134+ return json_res[" html_url" ]
135+ except requests.exceptions.HTTPError as err:
136+ return err
137+
134138
135139 if __name__ == " __main__" :
136140 s = sys.stdin.read()
137- print do_gist_json(s)
141+ print ( do_gist_json(s) )
138142
139143
140144 .. versionadded :: 0.12
0 commit comments