59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
# File 'lib/purple/path.rb', line 59
def execute(params = {}, args = {}, *callback_arguments)
= {
'Accept' => 'application/json',
'Content-Type' => 'application/json'
}
if client.authorization
authorization = client.authorization[:data].call
.deep_merge!(authorization) if client.authorization[:type].in?(%i[bearer custom_headers])
params.deep_merge!(authorization) if client.authorization[:type] == :custom_query
end
connection = Faraday.new(url: client.domain) do |conn|
conn.options.timeout = client.timeout if client.timeout.present?
conn. =
end
if client.domain.blank?
raise ArgumentError, 'Client domain is not set. Please set the domain in the client configuration.'
end
unless client.domain.start_with?('http')
raise ArgumentError, "Invalid URL: #{client.domain}. Ensure you have set protocol (http/https) in the client domain."
end
url = "#{client.domain}/#{full_path}"
response = case method
when :get
connection.get(url, params)
when :post
connection.post(url, params.to_json)
when :put
connection.put(url, params.to_json)
when :delete
connection.delete(url, params)
when :patch
connection.patch(url, params.to_json)
end
resp_structure = responses.find { |resp| resp.status_code == response.status }
if resp_structure.nil?
raise "#{client.domain}/#{full_path} returns #{response.status}, but it is not defined in the client"
else
object = if resp_structure.body.is_a?(Purple::Responses::Body)
resp_structure.body.validate!(response.body, args)
elsif resp_structure.body == :default
response.body
else
{}
end
client.callback&.call(url, params, , JSON.parse(response.body), *callback_arguments)
if block_given?
yield(resp_structure.status, object)
else
object
end
end
end
|