@@ -32,12 +32,28 @@ defmodule Algora.Github.Client do
3232
3333 with { :ok , encoded_body } <- Jason . encode ( body ) ,
3434 request = Finch . build ( method , url , headers , encoded_body ) ,
35- { :ok , % Finch.Response { body: body } } <- Finch . request ( request , Algora.Finch ) ,
35+ { :ok , % Finch.Response { body: body } } <- request_with_follow_redirects ( request ) ,
3636 { :ok , decoded_body } <- Jason . decode ( body ) do
3737 maybe_handle_error ( decoded_body )
3838 end
3939 end
4040
41+ defp request_with_follow_redirects ( request ) do
42+ case Finch . request ( request , Algora.Finch ) do
43+ { :ok , % Finch.Response { status: status , headers: headers } } when status in [ 301 , 302 , 307 ] ->
44+ case List . keyfind ( headers , "location" , 0 ) do
45+ { "location" , location } ->
46+ request_with_follow_redirects ( Finch . build ( request . method , location , request . headers , request . body ) )
47+
48+ nil ->
49+ { :error , "Redirect response missing location header" }
50+ end
51+
52+ res ->
53+ res
54+ end
55+ end
56+
4157 defp maybe_handle_error ( % { "message" => message , "status" => status } = body ) do
4258 case Integer . parse ( status ) do
4359 { code , _ } when code >= 400 -> { :error , "#{ code } #{ message } " }
0 commit comments