@@ -114,119 +114,91 @@ defmodule OpenApiTypesense.Client do
114114
115115 defp req_request ( req_client , opts ) do
116116 { _req , resp } = Req.Request . run_request ( req_client )
117- parse_resp ( resp , opts [ :response ] )
117+ parse_resp ( resp , opts )
118118 end
119119
120- defp encode_body ( opts ) do
121- if opts [ :request ] do
122- [ content_type ] = opts [ :request ]
123- parse_content_type ( content_type , opts [ :args ] [ :body ] )
124- else
125- Jason . encode_to_iodata! ( opts [ :args ] [ :body ] )
126- end
127- end
128-
129- defp parse_content_type ( { "application/octet-stream" , { :string , :generic } } , body ) do
130- Enum . map_join ( body , "\n " , & Jason . encode_to_iodata! / 1 )
131- end
132-
133- defp parse_content_type ( { "application/json" , { mod , :t } } , body ) when is_atom ( mod ) do
134- atom_keys = Map . keys ( mod . __struct__ ( ) ) |> Enum . reject ( & ( & 1 == :__struct__ ) )
135- string_keys = Enum . map ( atom_keys , & to_string / 1 )
136- keys = atom_keys ++ string_keys
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 )
137130
138- body
139- |> Map . take ( keys )
140- |> OpenApiTypesense.Converter . to_atom_keys ( safe: false )
141- |> Jason . encode_to_iodata! ( )
131+ { :ok , response }
142132 end
143133
144- defp parse_content_type ( { "application/json" , _ } , body ) do
145- Jason . encode_to_iodata! ( body )
146- end
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
147141
148- # Some resources are missing 4xx descriptions, hence we will set a default
149- # instead so we can see the actual error message instead of stacktrace.
150- # See https://github.com/typesense/typesense-api-spec/pull/84
151- defp parse_resp ( % Req.Response { } = resp , status_type ) do
152- { status , type } =
153- status_type
154- |> Enum . find ( fn { status , _type } -> status == resp . status end ) ||
155- { resp . status , :map }
142+ { { status , { mod , t } } , _ } when status === code and t !== :generic ->
143+ struct ( mod , body )
156144
157- parse_values ( status , type , resp . body )
158- end
145+ { { status , [ { _mod , t } ] } , nil } when status === code and t !== :generic ->
146+ [ ]
159147
160- defp parse_resp ( error , _opts_resp ) do
161- { :error , Exception . message ( error ) }
162- end
148+ { { status , :map } , _ } when status === code ->
149+ body
163150
164- @ spec parse_values (
165- non_neg_integer ( ) ,
166- atom ( ) | list ( ) | tuple ( ) ,
167- map ( ) | String . t ( ) | list ( )
168- ) ::
169- { :ok , any ( ) } | { :error , any ( ) }
170- defp parse_values ( code , :map , body ) do
171- status = if code in 200 .. 299 , do: :ok , else: :error
151+ { _ , _ } ->
152+ body
153+ |> String . splitter ( "\n " )
154+ |> Enum . map ( & Jason . decode! / 1 )
155+ end
156+ end )
172157
173- { status , body }
158+ { :ok , response }
174159 end
175160
176- defp parse_values ( code , values , body ) when is_list ( values ) do
177- status = if code in 200 .. 299 , do: :ok , else: :error
178-
179- resp =
180- values
181- |> Enum . map ( fn { module , _func_name } ->
182- cond do
183- is_list ( body ) ->
184- Enum . map ( body , fn single_body ->
185- struct ( module , single_body )
186- end )
187-
188- is_nil ( body ) ->
189- [ ]
190-
191- true ->
192- struct ( module , body )
161+ 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 )
193167 end
194168 end )
195- |> List . flatten ( )
196169
197- { status , resp }
170+ { :error , message }
198171 end
199172
200- defp parse_values ( code , { module , _func_name } = _values , body ) when module == :string do
201- status = if code in 200 .. 299 , do: :ok , else: :error
202-
203- case status do
204- :ok ->
205- resp =
206- body
207- |> String . splitter ( "\n " )
208- |> Enum . map ( & Jason . decode! / 1 )
209-
210- { status , resp }
173+ defp parse_resp ( error , _opts_resp ) do
174+ { :error , Exception . message ( error ) }
175+ end
211176
212- :error ->
213- { status , struct ( ApiResponse , message: Jason . decode! ( body ) [ "error" ] ) }
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 ] )
214183 end
215184 end
216185
217- defp parse_values (
218- code ,
219- { module , _func_name } = _values ,
220- % { hits: results , union_request_params: _ } = _body
221- ) do
222- status = if code in 200 .. 299 , do: :ok , else: :error
223-
224- { status , struct ( module , % { results: results } ) }
186+ defp parse_content_type ( { "application/octet-stream" , { :string , :generic } } , body ) do
187+ Enum . map_join ( body , "\n " , & Jason . encode_to_iodata! / 1 )
225188 end
226189
227- defp parse_values ( code , { module , _func_name } = _values , body ) do
228- status = if code in 200 .. 299 , do: :ok , else: :error
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
194+
195+ # body
196+ # |> Map.take(keys)
197+ # |> OpenApiTypesense.Converter.to_atom_keys(safe: false)
198+ # |> Jason.encode_to_iodata!()
199+ # end
229200
230- { status , struct ( module , body ) }
201+ defp parse_content_type ( { "application/json" , _ } , body ) do
202+ Jason . encode_to_iodata! ( body )
231203 end
232204end
0 commit comments