Skip to content

Commit 263fc04

Browse files
committed
Add support for media types
For some endpoints GitHub lets you request different response format using the `Accept` header [[1]]. However, by default if the response is not in JSON format `GhApi.__call__` will raise an error. This commit makes it possible by adding a bit of logic to look at the `Accept` header in the request and tell `fastcore.core.urlsend` not to return JSON if it doesn't look like the user is requesting a JSON media type. [1]: https://docs.github.com/en/rest/overview/media-types
1 parent 33f6d22 commit 263fc04

File tree

1 file changed

+47
-10
lines changed

1 file changed

+47
-10
lines changed

00_core.ipynb

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -202,15 +202,16 @@
202202
" if path[:7] not in ('http://','https:/'): path = GH_HOST+path\n",
203203
" if route:\n",
204204
" for k,v in route.items(): route[k] = quote(str(route[k]))\n",
205+
" return_json = ('json' in headers['Accept'])\n",
205206
" res,self.recv_hdrs = urlsend(path, verb, headers=headers or None, debug=self.debug, return_headers=True,\n",
206-
" route=route or None, query=query or None, data=data or None)\n",
207+
" route=route or None, query=query or None, data=data or None, return_json=return_json)\n",
207208
" if 'X-RateLimit-Remaining' in self.recv_hdrs:\n",
208209
" newlim = self.recv_hdrs['X-RateLimit-Remaining']\n",
209210
" if self.limit_cb is not None and newlim != self.limit_rem:\n",
210211
" self.limit_cb(int(newlim),int(self.recv_hdrs['X-RateLimit-Limit']))\n",
211212
" self.limit_rem = newlim\n",
212213
" \n",
213-
" return dict2obj(res)\n",
214+
" return dict2obj(res) if return_json else res\n",
214215
"\n",
215216
" def __dir__(self): return super().__dir__() + list(self.groups)\n",
216217
" def _repr_markdown_(self): return \"\\n\".join(f\"- [{o}]({_docroot + o.replace('_', '-')})\" for o in sorted(self.groups))\n",
@@ -296,18 +297,18 @@
296297
"- node_id: MDM6UmVmMzE1NzEyNTg4OnJlZnMvaGVhZHMvbWFzdGVy\n",
297298
"- url: https://api.github.com/repos/fastai/ghapi-test/git/refs/heads/master\n",
298299
"- object: \n",
299-
" - sha: 164355601ff88f0482d90f2e1bc7b7546a9bacef\n",
300+
" - sha: d0605f3abc070f4790501db038c24223379007a5\n",
300301
" - type: commit\n",
301-
" - url: https://api.github.com/repos/fastai/ghapi-test/git/commits/164355601ff88f0482d90f2e1bc7b7546a9bacef"
302+
" - url: https://api.github.com/repos/fastai/ghapi-test/git/commits/d0605f3abc070f4790501db038c24223379007a5"
302303
],
303304
"text/plain": [
304305
"- ref: refs/heads/master\n",
305306
"- node_id: MDM6UmVmMzE1NzEyNTg4OnJlZnMvaGVhZHMvbWFzdGVy\n",
306307
"- url: https://api.github.com/repos/fastai/ghapi-test/git/refs/heads/master\n",
307308
"- object: \n",
308-
" - sha: 164355601ff88f0482d90f2e1bc7b7546a9bacef\n",
309+
" - sha: d0605f3abc070f4790501db038c24223379007a5\n",
309310
" - type: commit\n",
310-
" - url: https://api.github.com/repos/fastai/ghapi-test/git/commits/164355601ff88f0482d90f2e1bc7b7546a9bacef"
311+
" - url: https://api.github.com/repos/fastai/ghapi-test/git/commits/d0605f3abc070f4790501db038c24223379007a5"
311312
]
312313
},
313314
"execution_count": null,
@@ -365,18 +366,18 @@
365366
"- node_id: MDM6UmVmMzE1NzEyNTg4OnJlZnMvaGVhZHMvbWFzdGVy\n",
366367
"- url: https://api.github.com/repos/fastai/ghapi-test/git/refs/heads/master\n",
367368
"- object: \n",
368-
" - sha: 164355601ff88f0482d90f2e1bc7b7546a9bacef\n",
369+
" - sha: d0605f3abc070f4790501db038c24223379007a5\n",
369370
" - type: commit\n",
370-
" - url: https://api.github.com/repos/fastai/ghapi-test/git/commits/164355601ff88f0482d90f2e1bc7b7546a9bacef"
371+
" - url: https://api.github.com/repos/fastai/ghapi-test/git/commits/d0605f3abc070f4790501db038c24223379007a5"
371372
],
372373
"text/plain": [
373374
"- ref: refs/heads/master\n",
374375
"- node_id: MDM6UmVmMzE1NzEyNTg4OnJlZnMvaGVhZHMvbWFzdGVy\n",
375376
"- url: https://api.github.com/repos/fastai/ghapi-test/git/refs/heads/master\n",
376377
"- object: \n",
377-
" - sha: 164355601ff88f0482d90f2e1bc7b7546a9bacef\n",
378+
" - sha: d0605f3abc070f4790501db038c24223379007a5\n",
378379
" - type: commit\n",
379-
" - url: https://api.github.com/repos/fastai/ghapi-test/git/commits/164355601ff88f0482d90f2e1bc7b7546a9bacef"
380+
" - url: https://api.github.com/repos/fastai/ghapi-test/git/commits/d0605f3abc070f4790501db038c24223379007a5"
380381
]
381382
},
382383
"execution_count": null,
@@ -388,6 +389,42 @@
388389
"api['/repos/{owner}/{repo}/git/ref/{ref}'](owner='fastai', repo='ghapi-test', ref='heads/master')"
389390
]
390391
},
392+
{
393+
"cell_type": "markdown",
394+
"metadata": {},
395+
"source": [
396+
"### Media types"
397+
]
398+
},
399+
{
400+
"cell_type": "markdown",
401+
"metadata": {},
402+
"source": [
403+
"For some endpoints GitHub lets you specify a [media type](https://docs.github.com/en/rest/overview/media-types) the for response data, using the `Accept` header. If you choose a media type that is not JSON formatted (for instance `application/vnd.github.v3.sha`) then the call to the `GhApi` object will return a string instead of an object."
404+
]
405+
},
406+
{
407+
"cell_type": "code",
408+
"execution_count": null,
409+
"metadata": {},
410+
"outputs": [
411+
{
412+
"data": {
413+
"text/plain": [
414+
"'d0605f3abc070f4790501db038c24223379007a5'"
415+
]
416+
},
417+
"execution_count": null,
418+
"metadata": {},
419+
"output_type": "execute_result"
420+
}
421+
],
422+
"source": [
423+
"api('/repos/{owner}/{repo}/commits/{ref}', 'GET', route=dict(\n",
424+
" owner='fastai', repo='ghapi-test', ref='refs/heads/master'),\n",
425+
" headers={'Accept': 'application/vnd.github.VERSION.sha'})"
426+
]
427+
},
391428
{
392429
"cell_type": "markdown",
393430
"metadata": {},

0 commit comments

Comments
 (0)