@@ -78,8 +78,12 @@ defmodule OpenApiTypesense.Client do
7878 if client do
7979 client . request ( conn , params )
8080 else
81- req_client = build_req_client ( conn , params )
82- req_request ( req_client , params )
81+ { _req , resp } =
82+ conn
83+ |> build_req_client ( params )
84+ |> Req.Request . run_request ( )
85+
86+ parse_resp ( resp , params )
8387 end
8488 end
8589
@@ -105,100 +109,70 @@ defmodule OpenApiTypesense.Client do
105109 method: Access . get ( opts , :method , :get ) ,
106110 body: encode_body ( opts ) ,
107111 url: url ,
108- decode_json: [ keys: :atoms ]
112+ decode_body: false
109113 ]
110114 |> Req . new ( )
111115 |> Req.Request . merge_options ( req_options )
112116 |> Req.Request . put_header ( "x-typesense-api-key" , Map . get ( conn , :api_key ) )
113117 end
114118
115- defp req_request ( req_client , opts ) do
116- { _req , resp } = Req.Request . run_request ( req_client )
117- parse_resp ( resp , opts )
118- end
119-
120- defp parse_resp ( % Req.Response { status: code , body: list } , % { response: resp } )
121- when is_list ( list ) and code in 200 .. 299 do
122- response =
123- Enum . find_value ( resp , fn { status , [ { mod , _t } ] } ->
124- if status === code do
125- Enum . map ( list , fn body ->
126- struct ( mod , body )
127- end )
128- end
129- end )
130-
131- { :ok , response }
132- end
133-
134- defp parse_resp ( % Req.Response { status: code , body: body } , % { response: resp } )
135- when code in 200 .. 299 do
136- response =
137- Enum . find_value ( resp , fn resp ->
138- case { resp , body } do
139- { { status , { :string , :generic } } , "" } when status === code ->
140- body
141-
142- { { status , { mod , t } } , _ } when status === code and t !== :generic ->
143- struct ( mod , body )
119+ defp encode_body ( opts ) do
120+ body = opts [ :args ] [ :body ]
144121
145- { { status , [ { _mod , t } ] } , nil } when status === code and t !== :generic ->
146- [ ]
122+ case { opts [ :request ] , body } do
123+ { nil , _ } ->
124+ Jason . encode_to_iodata! ( body )
147125
148- { { status , :map } , _ } when status === code ->
149- body
126+ { [ { "application/octet-stream" , { :string , :generic } } ] , body } when not is_binary ( body ) ->
127+ Enum . map_join ( body , " \n " , & Jason . encode_to_iodata! / 1 )
150128
151- { _ , _ } ->
152- body
153- |> String . splitter ( "\n " )
154- |> Enum . map ( & Jason . decode! / 1 )
155- end
156- end )
129+ { [ { "application/json" , _ } ] , body } when not is_binary ( body ) ->
130+ Jason . encode_to_iodata! ( body )
157131
158- { :ok , response }
132+ { _ , body } when is_binary ( body ) ->
133+ body
134+ end
159135 end
160136
161137 defp parse_resp ( % Req.Response { status: code , body: body } , % { response: resp } ) do
162- message =
163- Enum . find_value ( resp , fn { status , type } ->
164- if status === code do
165- { mod , _t } = type
166- struct ( mod , body )
167- end
168- end )
169-
170- { :error , message }
138+ { _status , mod } = Enum . find ( resp , fn { status , _ } -> status === code end )
139+ parse_body ( code , mod , body )
171140 end
172141
173142 defp parse_resp ( error , _opts_resp ) do
174143 { :error , Exception . message ( error ) }
175144 end
176145
177- defp encode_body ( opts ) do
178- if opts [ :request ] do
179- [ content_type ] = opts [ :request ]
180- parse_content_type ( content_type , opts [ :args ] [ :body ] )
181- else
182- Jason . encode_to_iodata! ( opts [ :args ] [ :body ] )
183- end
146+ defp parse_body ( code , { mod , :t } , body ) when code in 400 .. 499 do
147+ payload =
148+ body
149+ |> Jason . decode! ( )
150+ |> OpenApiTypesense.Converter . to_atom_keys ( )
151+
152+ { :error , struct ( mod , payload ) }
184153 end
185154
186- defp parse_content_type ( { "application/octet-stream" , { :string , :generic } } , body ) do
187- Enum . map_join ( body , " \n " , & Jason . encode_to_iodata! / 1 )
155+ defp parse_body ( _code , [ { _mod , _t } ] , "null" ) do
156+ { :ok , [ ] }
188157 end
189158
190- # defp parse_content_type({"application/json", {module, :t}}, body) when is_atom(module) do
191- # atom_keys = Map.keys(mod.__struct__()) |> Enum.reject(&(&1 == :__struct__))
192- # string_keys = Enum.map(atom_keys, &to_string/1)
193- # keys = atom_keys ++ string_keys
159+ defp parse_body ( _code , [ { mod , _t } ] , body ) when is_binary ( body ) do
160+ { :ok , Poison . decode! ( body , as: [ mod . __struct__ ( ) ] ) }
161+ end
194162
195- # body
196- # |> Map.take(keys)
197- # |> OpenApiTypesense.Converter.to_atom_keys(safe: false)
198- # |> Jason.encode_to_iodata!()
199- # end
163+ defp parse_body ( _code , { :string , :generic } , "" ) do
164+ { :ok , "" }
165+ end
166+
167+ defp parse_body ( _code , { :string , :generic } , body ) do
168+ { :ok , String . split ( body , "\n " ) |> Enum . map ( & Jason . decode! / 1 ) }
169+ end
170+
171+ defp parse_body ( _code , :map , body ) do
172+ { :ok , Jason . decode! ( body , keys: :atoms ) }
173+ end
200174
201- defp parse_content_type ( { "application/json" , _ } , body ) do
202- Jason . encode_to_iodata !( body )
175+ defp parse_body ( _code , { mod , _t } , body ) do
176+ { :ok , Poison . decode !( body , as: mod . __struct__ ( ) ) }
203177 end
204178end
0 commit comments