|
202 | 202 | " path = self.gh_host + path\n", |
203 | 203 | " if route:\n", |
204 | 204 | " for k,v in route.items(): route[k] = quote(str(route[k]))\n", |
| 205 | + " return_json = ('json' in headers['Accept'])\n", |
205 | 206 | " 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", |
207 | 208 | " if 'X-RateLimit-Remaining' in self.recv_hdrs:\n", |
208 | 209 | " newlim = self.recv_hdrs['X-RateLimit-Remaining']\n", |
209 | 210 | " if self.limit_cb is not None and newlim != self.limit_rem:\n", |
210 | 211 | " self.limit_cb(int(newlim),int(self.recv_hdrs['X-RateLimit-Limit']))\n", |
211 | 212 | " self.limit_rem = newlim\n", |
212 | 213 | "\n", |
213 | | - " return dict2obj(res)\n", |
| 214 | + " return dict2obj(res) if return_json else res\n", |
214 | 215 | "\n", |
215 | 216 | " def __dir__(self): return super().__dir__() + list(self.groups)\n", |
216 | 217 | " def _repr_markdown_(self): return \"\\n\".join(f\"- [{o}]({_docroot + o.replace('_', '-')})\" for o in sorted(self.groups))\n", |
|
292 | 293 | { |
293 | 294 | "data": { |
294 | 295 | "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" |
303 | 303 | ], |
304 | 304 | "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" |
311 | 312 | ] |
312 | 313 | }, |
313 | 314 | "execution_count": null, |
|
361 | 362 | { |
362 | 363 | "data": { |
363 | 364 | "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" |
372 | 372 | ], |
373 | 373 | "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" |
380 | 381 | ] |
381 | 382 | }, |
382 | 383 | "execution_count": null, |
|
388 | 389 | "api['/repos/{owner}/{repo}/git/ref/{ref}'](owner='fastai', repo='ghapi-test', ref='heads/master')" |
389 | 390 | ] |
390 | 391 | }, |
| 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 | + }, |
391 | 428 | { |
392 | 429 | "cell_type": "markdown", |
393 | 430 | "metadata": {}, |
|
0 commit comments