-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor GitHub api layer for easier and more flexible testing
- removed bypass from all tests except lib/code_corps/github/api_test.exs - introduced `with_mock_api` macro to mock out an api layer module for usage in misc github tests - implementd proper bypass tests in `api_test` - removed github_case since it's bypass facilities are now only existing in `api_test` - fixed several minor bugs and issues revealed through this process
- Loading branch information
Showing
50 changed files
with
749 additions
and
528 deletions.
There are no files selected for viewing
This file contains 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 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 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,41 @@ | ||
defmodule CodeCorps.GitHub.API do | ||
alias CodeCorps.{ | ||
GitHub, | ||
GitHub.APIError, | ||
GitHub.HTTPClientError | ||
} | ||
|
||
@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) | ||
|> marshall_response() | ||
end | ||
|
||
@typep http_success :: {:ok, integer, [{String.t, String.t}], String.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 | ||
case body |> Poison.decode do | ||
{:ok, json} -> | ||
{:ok, json} | ||
{:error, {_decoding_error, _decoding_value}} -> | ||
marshall_response({:error, :body_decoding_error}) | ||
end | ||
end | ||
defp marshall_response({:ok, 404, _headers, body}) do | ||
{:error, APIError.new({404, %{"message" => body}})} | ||
end | ||
defp marshall_response({:ok, status, _headers, body}) when status in 400..599 do | ||
case body |> Poison.decode do | ||
{:ok, json} -> | ||
{:error, APIError.new({status, json})} | ||
{:error, {_decoding_error, _decoding_value}} -> | ||
marshall_response({:error, :body_decoding_error}) | ||
end | ||
end | ||
defp marshall_response({:error, reason}) do | ||
{:error, HTTPClientError.new(reason: reason)} | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.