Skip to content

Commit a100707

Browse files
authored
Merge pull request #102 from lfdebrux/lfdebrux-add-media-types-support
Add support for media types
2 parents bf5f4b5 + 1bf6e02 commit a100707

File tree

1 file changed

+67
-30
lines changed

1 file changed

+67
-30
lines changed

00_core.ipynb

Lines changed: 67 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -202,15 +202,16 @@
202202
" path = self.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",
@@ -292,22 +293,22 @@
292293
{
293294
"data": {
294295
"text/markdown": [
295-
"```json\n",
296-
"{ 'node_id': 'MDM6UmVmMzE1NzEyNTg4OnJlZnMvaGVhZHMvbWFzdGVy',\n",
297-
" 'object': { 'sha': '0a69ee7d9aebb4d8d86ba620dd1a7c8f3954155c',\n",
298-
" 'type': 'commit',\n",
299-
" 'url': 'https://api.github.com/repos/fastai/ghapi-test/git/commits/0a69ee7d9aebb4d8d86ba620dd1a7c8f3954155c'},\n",
300-
" 'ref': 'refs/heads/master',\n",
301-
" 'url': 'https://api.github.com/repos/fastai/ghapi-test/git/refs/heads/master'}\n",
302-
"```"
296+
"- ref: refs/heads/master\n",
297+
"- node_id: MDM6UmVmMzE1NzEyNTg4OnJlZnMvaGVhZHMvbWFzdGVy\n",
298+
"- url: https://api.github.com/repos/fastai/ghapi-test/git/refs/heads/master\n",
299+
"- object: \n",
300+
" - sha: d0605f3abc070f4790501db038c24223379007a5\n",
301+
" - type: commit\n",
302+
" - url: https://api.github.com/repos/fastai/ghapi-test/git/commits/d0605f3abc070f4790501db038c24223379007a5"
303303
],
304304
"text/plain": [
305-
"{'ref': 'refs/heads/master',\n",
306-
" 'node_id': 'MDM6UmVmMzE1NzEyNTg4OnJlZnMvaGVhZHMvbWFzdGVy',\n",
307-
" 'url': 'https://api.github.com/repos/fastai/ghapi-test/git/refs/heads/master',\n",
308-
" 'object': {'sha': '0a69ee7d9aebb4d8d86ba620dd1a7c8f3954155c',\n",
309-
" 'type': 'commit',\n",
310-
" 'url': 'https://api.github.com/repos/fastai/ghapi-test/git/commits/0a69ee7d9aebb4d8d86ba620dd1a7c8f3954155c'}}"
305+
"- ref: refs/heads/master\n",
306+
"- node_id: MDM6UmVmMzE1NzEyNTg4OnJlZnMvaGVhZHMvbWFzdGVy\n",
307+
"- url: https://api.github.com/repos/fastai/ghapi-test/git/refs/heads/master\n",
308+
"- object: \n",
309+
" - sha: d0605f3abc070f4790501db038c24223379007a5\n",
310+
" - type: commit\n",
311+
" - url: https://api.github.com/repos/fastai/ghapi-test/git/commits/d0605f3abc070f4790501db038c24223379007a5"
311312
]
312313
},
313314
"execution_count": null,
@@ -361,22 +362,22 @@
361362
{
362363
"data": {
363364
"text/markdown": [
364-
"```json\n",
365-
"{ 'node_id': 'MDM6UmVmMzE1NzEyNTg4OnJlZnMvaGVhZHMvbWFzdGVy',\n",
366-
" 'object': { 'sha': '0a69ee7d9aebb4d8d86ba620dd1a7c8f3954155c',\n",
367-
" 'type': 'commit',\n",
368-
" 'url': 'https://api.github.com/repos/fastai/ghapi-test/git/commits/0a69ee7d9aebb4d8d86ba620dd1a7c8f3954155c'},\n",
369-
" 'ref': 'refs/heads/master',\n",
370-
" 'url': 'https://api.github.com/repos/fastai/ghapi-test/git/refs/heads/master'}\n",
371-
"```"
365+
"- ref: refs/heads/master\n",
366+
"- node_id: MDM6UmVmMzE1NzEyNTg4OnJlZnMvaGVhZHMvbWFzdGVy\n",
367+
"- url: https://api.github.com/repos/fastai/ghapi-test/git/refs/heads/master\n",
368+
"- object: \n",
369+
" - sha: d0605f3abc070f4790501db038c24223379007a5\n",
370+
" - type: commit\n",
371+
" - url: https://api.github.com/repos/fastai/ghapi-test/git/commits/d0605f3abc070f4790501db038c24223379007a5"
372372
],
373373
"text/plain": [
374-
"{'ref': 'refs/heads/master',\n",
375-
" 'node_id': 'MDM6UmVmMzE1NzEyNTg4OnJlZnMvaGVhZHMvbWFzdGVy',\n",
376-
" 'url': 'https://api.github.com/repos/fastai/ghapi-test/git/refs/heads/master',\n",
377-
" 'object': {'sha': '0a69ee7d9aebb4d8d86ba620dd1a7c8f3954155c',\n",
378-
" 'type': 'commit',\n",
379-
" 'url': 'https://api.github.com/repos/fastai/ghapi-test/git/commits/0a69ee7d9aebb4d8d86ba620dd1a7c8f3954155c'}}"
374+
"- ref: refs/heads/master\n",
375+
"- node_id: MDM6UmVmMzE1NzEyNTg4OnJlZnMvaGVhZHMvbWFzdGVy\n",
376+
"- url: https://api.github.com/repos/fastai/ghapi-test/git/refs/heads/master\n",
377+
"- object: \n",
378+
" - sha: d0605f3abc070f4790501db038c24223379007a5\n",
379+
" - type: commit\n",
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)