Skip to content

Instantly share code, notes, and snippets.

@technoweenie
Forked from anonymous/no_url_help.rb
Created December 20, 2012 20:17
Show Gist options
  • Save technoweenie/4348195 to your computer and use it in GitHub Desktop.
Save technoweenie/4348195 to your computer and use it in GitHub Desktop.
# lets say we change /repos/* to /repositories
# hardcoded urls
pull_response = RestCall.get("/repos/bob/rails8/pulls") # 301
pull_response = RestCall.get("/repositories/bob/rails8/pulls")
issue_response = RestCall.get("/repos/bob/rails8/issues/#{response.first['issue']}") # 301
issue_response = RestCall.get("/repositories/bob/rails8/issues/#{response.first['issue']}")
# hypermedia
pull_response = RestCall.get("/repos/bob/rails8/pulls") # 301
pull_response = RestCall.get("/repositories/bob/rails8/pulls")
issue_response = RestCall.get(pull_response.first["issue_url"])
# Even if saving one redirect isn't earth shattering, your client doesn't give a shit how my service mashes strings together to form URLs.
#
# * If pull_response is a 404, then you're going to have problems getting the Issue anyway.
# * if 'issue_url' isn't there, then the API is broken.
# * APIs should gradually evolve properties through media types or versioning
#
# Side note: The GitHub API returns 404s instead of 401/403 to prevent security leaks.
# /repos/github/secret-project/pulls/1 should be a 404 for you, even though it may exist for other users that have access.
#
# I hope you're fortunate not to have that same concern in your API :)
pull_response = RestCall.get("/repos/bob/rails8/pulls")
issue_response = RestCall.get("/repos/bob/rails8/issues/#{response.first['issue']}")
# either of these might 404
pull_response = RestCall.get("/repos/bob/rails8/pulls")
issue_response = RestCall.get(pull_response.first["issue_url"]) # => oops, what if response is empty?
if pull_response.present?
issue_response = RestCall.get(pull_response.first["issue_url"]) # => oops, what if response issue_url isn't there?
end
if pull_response.present? && pull_response.first["issue_url"].present?
issue_response = RestCall.get(pull_response.first["issue_url"]) # OK!
end
@technoweenie
Copy link
Author

Truth is, no matter what hints we provide, people are always going to be hardcoding API calls and mashing strings together. If you can at least make some people's lives easier, that's good. Ultimately it's not a big deal though. Things like documentation, and a commitment to not jerking your api consumers around is much more important.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment