gh icon indicating copy to clipboard operation
gh copied to clipboard

Handle API substitution format?

Open hadley opened this issue 7 years ago • 9 comments

i.e. gh::gh("/repos/hadley/dplyr") gives:

...
   "statuses_url": "https://api.github.com/repos/hadley/dplyr/statuses/{sha}",
 ...
  "git_commits_url": "https://api.github.com/repos/hadley/dplyr/git/commits{/sha}",
  "comments_url": "https://api.github.com/repos/hadley/dplyr/comments{/number}",
  "issue_comment_url": "https://api.github.com/repos/hadley/dplyr/issues/comments{/number}",
  "contents_url": "https://api.github.com/repos/hadley/dplyr/contents/{+path}",
  "compare_url": "https://api.github.com/repos/hadley/dplyr/compare/{base}...{head}",
  "merges_url": "https://api.github.com/repos/hadley/dplyr/merges",
...

Although it's not clear whether that's quite consistent enough to be parseable.

hadley avatar Nov 08 '16 22:11 hadley

Oh, https://developer.github.com/v3/#hypermedia says these are URI templates, as described in https://tools.ietf.org/html/rfc6570.

hadley avatar Nov 08 '16 22:11 hadley

So we probably need a uritemplate micropackage

hadley avatar Nov 08 '16 22:11 hadley

Yes, these can be potentially useful, to walk the network of GH objects, although I am not entirely sure how we would do that, especially without proper classes for repos, users, PRs, etc.

gaborcsardi avatar Nov 08 '16 23:11 gaborcsardi

THere is even a test suite: https://github.com/uri-templates/uritemplate-test

gaborcsardi avatar Nov 08 '16 23:11 gaborcsardi

In the short term, I was mostly thinking that this might be another source of copy-and-paste urls.

hadley avatar Nov 08 '16 23:11 hadley

OK, maybe I'll write an uritemplate package. I guess Ropensci could use that, too, assuming it is used by other APIs as well. Is it @sckott?

gaborcsardi avatar Nov 10 '16 22:11 gaborcsardi

haven't seen templates very often in servers, but I'm going to implement them in my request pkg and in webmockr, so started https://github.com/ropenscilabs/urltemplate a while back but not useable yet

sckott avatar Nov 10 '16 23:11 sckott

@sckott Cool!

gaborcsardi avatar Nov 10 '16 23:11 gaborcsardi

I've accidentally made considerable progress towards this in:

  • 05f0b49152602274ad3f416abd880c8df03a0268
  • 6a821910ad8e5ec7381d747b329edb8f498b7386

because the main API documentation uses URI templates now.

However this form does not currently work:

gh::gh(
  "https://api.github.com/repos/tidyverse/dplyr/branches{/branch}",
  branch = "master"
)
#> Error in gh_process_response(raw): 
#> GitHub API error (404): 404 Not Found
#> Message: Not Found
#> Read more at https://docs.github.com/rest
#> 
#> URL not found: https://api.github.com/repos/tidyverse/dplyr/branches%7B/branch%7D?branch=master

i.e. with the / inside the final {} (indicating that the entire suffix is optional). This form never appears in the docs, so my simple approach to URI templates doesn't anticipate that.

jennybc avatar Aug 12 '20 16:08 jennybc

Also now in httr2::req_template(), so I suspect we can close this issue.

hadley avatar Feb 06 '23 23:02 hadley