@@ -2,19 +2,37 @@ defmodule CodeCorps.GitHub.API do
22 alias CodeCorps . {
33 GithubAppInstallation ,
44 GitHub ,
5+ GitHub.API.Pagination ,
56 GitHub.APIError ,
67 GitHub.HTTPClientError ,
78 User
89 }
910
11+ alias HTTPoison . { Error , Response }
12+
13+ def gateway ( ) , do: Application . get_env ( :code_corps , :github )
14+
1015 @ spec request ( GitHub . method , String . t , GitHub . headers , GitHub . body , list ) :: GitHub . response
11- def request ( method , url , headers , body , options ) do
12- method
13- |> :hackney . request ( url , headers , body , options )
16+ def request ( method , url , body , headers , options ) do
17+ gateway ( ) . request ( method , url , body , headers , options )
1418 |> marshall_response ( )
1519 end
1620
17- defdelegate get_all ( url , headers , opts ) , to: CodeCorps.GitHub.EagerAPI
21+ @ spec get_all ( String . t , GitHub . headers , list ) :: GitHub . response
22+ def get_all ( url , headers , options ) do
23+ { :ok , % Response { request_url: request_url , headers: response_headers } } =
24+ gateway ( ) . request ( :head , url , "" , headers , options )
25+
26+ response_headers
27+ |> Pagination . retrieve_total_pages ( )
28+ |> Pagination . to_page_numbers ( )
29+ |> Enum . map ( & Pagination . add_page_param ( options , & 1 ) )
30+ |> Enum . map ( & gateway ( ) . request ( :get , url , "" , headers , & 1 ) )
31+ |> Enum . map ( & marshall_response / 1 )
32+ |> Enum . map ( & Tuple . to_list / 1 )
33+ |> Enum . map ( & List . last / 1 )
34+ |> List . flatten
35+ end
1836
1937 @ doc """
2038 Get access token headers for a given `CodeCorps.User` and
@@ -47,29 +65,32 @@ defmodule CodeCorps.GitHub.API do
4765 end
4866 end
4967
50- @ typep http_success :: { :ok , integer , [ { String . t , String . t } ] , String . t }
68+ @ typep http_success :: { :ok , Response . t }
5169 @ typep http_failure :: { :error , term }
5270
5371 @ spec marshall_response ( http_success | http_failure ) :: GitHub . response
54- defp marshall_response ( { :ok , status , _headers , body } ) when status in 200 .. 299 do
72+ defp marshall_response ( { :ok , % Response { body: body , status_code: status } } ) when status in 200 .. 299 do
5573 case body |> Poison . decode do
5674 { :ok , json } ->
5775 { :ok , json }
5876 { :error , _value } ->
5977 { :error , HTTPClientError . new ( reason: :body_decoding_error ) }
6078 end
6179 end
62- defp marshall_response ( { :ok , 404 , _headers , body } ) do
80+ defp marshall_response ( { :ok , % Response { body: body , status_code: 404 } } ) do
6381 { :error , APIError . new ( { 404 , % { "message" => body } } ) }
6482 end
65- defp marshall_response ( { :ok , status , _headers , body } ) when status in 400 .. 599 do
83+ defp marshall_response ( { :ok , % Response { body: body , status_code: status } } ) when status in 400 .. 599 do
6684 case body |> Poison . decode do
6785 { :ok , json } ->
6886 { :error , APIError . new ( { status , json } ) }
6987 { :error , _value } ->
7088 { :error , HTTPClientError . new ( reason: :body_decoding_error ) }
7189 end
7290 end
91+ defp marshall_response ( { :error , % Error { reason: reason } } ) do
92+ { :error , HTTPClientError . new ( reason: reason ) }
93+ end
7394 defp marshall_response ( { :error , reason } ) do
7495 { :error , HTTPClientError . new ( reason: reason ) }
7596 end
0 commit comments