Repository navigation
Switched the GitHub API to use a simple Gateway module #1138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,19 +2,37 @@ defmodule CodeCorps.GitHub.API do | |
| alias CodeCorps.{ | ||
| GithubAppInstallation, | ||
| GitHub, | ||
| GitHub.API.Pagination, | ||
| GitHub.APIError, | ||
| GitHub.HTTPClientError, | ||
| User | ||
| } | ||
|
|
||
| alias HTTPoison.{Error, Response} | ||
|
|
||
| def gateway(), do: Application.get_env(:code_corps, :github) | ||
|
|
||
| @spec request(GitHub.method, String.t, GitHub.headers, GitHub.body, list) :: GitHub.response | ||
| def request(method, url, headers, body, options) do | ||
| method | ||
| |> :hackney.request(url, headers, body, options) | ||
| def request(method, url, body, headers, options) do | ||
| gateway().request(method, url, body, headers, options) | ||
| |> marshall_response() | ||
| end | ||
|
|
||
| defdelegate get_all(url, headers, opts), to: CodeCorps.GitHub.EagerAPI | ||
| @spec get_all(String.t, GitHub.headers, list) :: GitHub.response | ||
| def get_all(url, headers, options) do | ||
| {:ok, %Response{request_url: request_url, headers: response_headers}} = | ||
| gateway().request(:head, url, "", headers, options) | ||
|
|
||
| response_headers | ||
| |> Pagination.retrieve_total_pages() | ||
| |> Pagination.to_page_numbers() | ||
| |> Enum.map(&Pagination.add_page_param(options, &1)) | ||
| |> Enum.map(&gateway().request(:get, url, "", headers, &1)) | ||
| |> Enum.map(&marshall_response/1) | ||
| |> Enum.map(&Tuple.to_list/1) | ||
| |> Enum.map(&List.last/1) | ||
| |> List.flatten | ||
| end | ||
|
|
||
| @doc """ | ||
| Get access token headers for a given `CodeCorps.User` and | ||
|
|
@@ -47,29 +65,32 @@ defmodule CodeCorps.GitHub.API do | |
| end | ||
| end | ||
|
|
||
| @typep http_success :: {:ok, integer, [{String.t, String.t}], String.t} | ||
| @typep http_success :: {:ok, Response.t} | ||
| @typep http_failure :: {:error, term} | ||
|
|
||
| @spec marshall_response(http_success | http_failure) :: GitHub.response | ||
| defp marshall_response({:ok, status, _headers, body}) when status in 200..299 do | ||
| defp marshall_response({:ok, %Response{body: body, status_code: status}}) when status in 200..299 do | ||
| case body |> Poison.decode do | ||
| {:ok, json} -> | ||
| {:ok, json} | ||
| {:error, _value} -> | ||
| {:error, HTTPClientError.new(reason: :body_decoding_error)} | ||
| end | ||
| end | ||
| defp marshall_response({:ok, 404, _headers, body}) do | ||
| defp marshall_response({:ok, %Response{body: body, status_code: 404}}) do | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good fix here. |
||
| {:error, APIError.new({404, %{"message" => body}})} | ||
| end | ||
| defp marshall_response({:ok, status, _headers, body}) when status in 400..599 do | ||
| defp marshall_response({:ok, %Response{body: body, status_code: status}}) when status in 400..599 do | ||
| case body |> Poison.decode do | ||
| {:ok, json} -> | ||
| {:error, APIError.new({status, json})} | ||
| {:error, _value} -> | ||
| {:error, HTTPClientError.new(reason: :body_decoding_error)} | ||
| end | ||
| end | ||
| defp marshall_response({:error, %Error{reason: reason}}) do | ||
| {:error, HTTPClientError.new(reason: reason)} | ||
| end | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Much needed. |
||
| defp marshall_response({:error, reason}) do | ||
| {:error, HTTPClientError.new(reason: reason)} | ||
| end | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| defmodule CodeCorps.GitHub.API.Gateway do | ||
| @moduledoc ~S""" | ||
| The gate through which all communication with the GitHub API must go through. | ||
|
|
||
| The purpose of this module is to centralize the most basic GitHub API request, | ||
| so the module can be injected into tests easily, giving full control to what | ||
| the tested response is. | ||
| """ | ||
|
|
||
| alias CodeCorps.GitHub | ||
|
|
||
| @spec request(GitHub.method, String.t, GitHub.body, GitHub.headers, list) :: GitHub.response | ||
| def request(method, url, body, headers, options) do | ||
| HTTPoison.request(method, url, body, headers, options) | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| defmodule CodeCorps.GitHub.API.Pagination do | ||
| @moduledoc ~S""" | ||
| Used to parse and build pagination data when fetching multiple pages from the GitHub API | ||
| """ | ||
|
|
||
| @doc ~S""" | ||
| Parses a collection of response headers and determines the record page count for a GitHub endpoint. | ||
|
|
||
| The response the headers are retrieved from is usually generated using a | ||
| `:head` request. | ||
|
|
||
| The value of a "Link" header is used to determine the page count. | ||
|
|
||
| If the "Link" header is not present in the collection, the count is assumed 1 | ||
|
|
||
| If the "Link" header is present, we use regex to parse the pagination info | ||
| from its value. | ||
|
|
||
| The format of the header is as follows: | ||
|
|
||
| ``` | ||
| {"Link", '<https://api.github.com/search/code?q=addClass+user%3Amozilla&page=15>; rel="next", | ||
| <https://api.github.com/search/code?q=addClass+user%3Amozilla&page=34>; rel="last", | ||
| <https://api.github.com/search/code?q=addClass+user%3Amozilla&page=1>; rel="first", | ||
| <https://api.github.com/search/code?q=addClass+user%3Amozilla&page=13>; rel="prev"' | ||
| ``` | ||
|
|
||
| The page count is determind by locating the `rel="last"` url and extracting | ||
| the `page` query parameter from it. | ||
| """ | ||
| @spec retrieve_total_pages(list) :: integer | ||
| def retrieve_total_pages(headers) do | ||
| headers | ||
| |> List.keyfind("Link", 0, nil) | ||
| |> extract_total_pages | ||
| end | ||
|
|
||
| @spec extract_total_pages(nil | String.t) :: integer | ||
| defp extract_total_pages(nil), do: 1 | ||
| defp extract_total_pages({"Link", value} = _header) do | ||
| value | ||
| |> String.split(", ") | ||
| |> Enum.map(fn link -> | ||
| rel = get_rel(link) | ||
| page = get_page(link) | ||
| {rel, page} | ||
| end) | ||
| |> Enum.into(%{}) | ||
| |> Map.get("last") | ||
| end | ||
|
|
||
| @spec get_rel(String.t) :: String.t | ||
| defp get_rel(link) when is_binary(link) do | ||
| # Searches for `rel=` | ||
| Regex.run(~r{rel="([a-z]+)"}, link) |> List.last() | ||
| end | ||
|
|
||
| @spec get_page(String.t) :: integer | ||
| defp get_page(link) when is_binary(link) do | ||
| # Searches for the following variations: | ||
| # ``` | ||
| # ?page={match}> | ||
| # ?page={match}&... | ||
| # &page={match}> | ||
| # &page={match}&... | ||
| # ``` | ||
| Regex.run(~r{[&/?]page=([^>&]+)}, link) |> List.last |> String.to_integer | ||
| end | ||
|
|
||
| @doc ~S""" | ||
| From the specified page count, generates a list of integers, `1..count` | ||
| """ | ||
| @spec to_page_numbers(integer) :: list(integer) | ||
| def to_page_numbers(total) when is_integer(total), do: 1..total | ||
|
|
||
| @doc ~S""" | ||
| Adds a `page` query parameter to an `options` `Keyword` list. | ||
|
|
||
| For `HTTPPoison`, query parameters go under the `params` key of the `options` | ||
| argument, so this function also ensures the `params` key is present. | ||
| """ | ||
| @spec add_page_param(Keyword.t, integer) :: Keyword.t | ||
| def add_page_param(options, page) when is_list(options) when is_integer(page) do | ||
| params = | ||
| options | ||
| |> Keyword.get(:params, []) | ||
| |> Keyword.put(:page, page) | ||
|
|
||
| options | ||
| |> Keyword.put(:params, params) | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just read (oddly in
benchee's README) thatEnum.map |> List.flattenis 1.83x slower thanEnum.flat_map. Wonder if we should use that instead here?