From 92847110d8ce669690eb7087c2cd39494a2287bf Mon Sep 17 00:00:00 2001 From: Francesco Donadon Date: Sat, 13 May 2023 02:31:00 +0100 Subject: [PATCH] Fix compilation in project lacking Finch (#205) Closes #204 --- lib/exvcr/adapter/finch/converter.ex | 157 ++++++++++++++------------- 1 file changed, 81 insertions(+), 76 deletions(-) diff --git a/lib/exvcr/adapter/finch/converter.ex b/lib/exvcr/adapter/finch/converter.ex index d633f3c..f8ee73c 100644 --- a/lib/exvcr/adapter/finch/converter.ex +++ b/lib/exvcr/adapter/finch/converter.ex @@ -1,86 +1,91 @@ -defmodule ExVCR.Adapter.Finch.Converter do - @moduledoc """ - Provides helpers to mock Finch methods. - """ - - use ExVCR.Converter - - alias ExVCR.Util - - defp string_to_response(string) do - response = Enum.map(string, fn({x, y}) -> {String.to_atom(x), y} end) - response = struct(ExVCR.Response, response) - - response = - if response.type == "error" do - body = string_to_error_reason(response.body) - %{response | body: body} - else - response - end - - response = - if is_map(response.headers) do - headers = response.headers - |> Map.to_list - |> Enum.map(fn({k,v}) -> {k, v} end) - %{response | headers: headers} - else - response - end - - response - end +if Code.ensure_loaded?(Finch) do + defmodule ExVCR.Adapter.Finch.Converter do + @moduledoc """ + Provides helpers to mock Finch methods. + """ - defp string_to_error_reason(reason) do - {reason_struct, _} = Code.eval_string(reason) - reason_struct - end + use ExVCR.Converter - defp request_to_string([request, finch_module]) do - request_to_string([request, finch_module, []]) - end + alias ExVCR.Util - defp request_to_string([request, _finch_module, opts]) do - url = Util.build_url(request.scheme, request.host, request.path, request.port, request.query) + defp string_to_response(string) do + response = Enum.map(string, fn {x, y} -> {String.to_atom(x), y} end) + response = struct(ExVCR.Response, response) - %ExVCR.Request{ - url: parse_url(url), - headers: parse_headers(request.headers), - method: String.downcase(request.method), - body: parse_request_body(request.body), - options: parse_options(sanitize_options(opts)) - } - end + response = + if response.type == "error" do + body = string_to_error_reason(response.body) + %{response | body: body} + else + response + end - # If option value is tuple, make it as list, for encoding as json. - defp sanitize_options(options) do - Enum.map(options, fn({key, value}) -> - if is_tuple(value) do - {key, Tuple.to_list(value)} - else - {key, value} - end - end) - end + response = + if is_map(response.headers) do + headers = + response.headers + |> Map.to_list() + |> Enum.map(fn {k, v} -> {k, v} end) - defp response_to_string({:ok, %Finch.Response{} = response}), do: response_to_string(response) + %{response | headers: headers} + else + response + end - defp response_to_string(%Finch.Response{} = response) do - %ExVCR.Response{ - type: "ok", - status_code: response.status, - headers: parse_headers(response.headers), - body: to_string(response.body) - } - end + response + end - defp response_to_string({:error, reason}) do - %ExVCR.Response{ - type: "error", - body: error_reason_to_string(reason) - } - end + defp string_to_error_reason(reason) do + {reason_struct, _} = Code.eval_string(reason) + reason_struct + end + + defp request_to_string([request, finch_module]) do + request_to_string([request, finch_module, []]) + end - defp error_reason_to_string(reason), do: Macro.to_string(reason) + defp request_to_string([request, _finch_module, opts]) do + url = + Util.build_url(request.scheme, request.host, request.path, request.port, request.query) + + %ExVCR.Request{ + url: parse_url(url), + headers: parse_headers(request.headers), + method: String.downcase(request.method), + body: parse_request_body(request.body), + options: parse_options(sanitize_options(opts)) + } + end + + # If option value is tuple, make it as list, for encoding as json. + defp sanitize_options(options) do + Enum.map(options, fn {key, value} -> + if is_tuple(value) do + {key, Tuple.to_list(value)} + else + {key, value} + end + end) + end + + defp response_to_string({:ok, %Finch.Response{} = response}), do: response_to_string(response) + + defp response_to_string(%Finch.Response{} = response) do + %ExVCR.Response{ + type: "ok", + status_code: response.status, + headers: parse_headers(response.headers), + body: to_string(response.body) + } + end + + defp response_to_string({:error, reason}) do + %ExVCR.Response{ + type: "error", + body: error_reason_to_string(reason) + } + end + + defp error_reason_to_string(reason), do: Macro.to_string(reason) + end end