forked from paypal/paypalhttp_ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_client.rb
134 lines (111 loc) · 3.17 KB
/
http_client.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
122
123
124
125
126
127
128
129
130
131
132
133
134
require 'ostruct'
require 'net/http'
require 'date'
module PayPalHttp
class HttpClient
attr_accessor :environment, :encoder
def initialize(environment)
@environment = environment
@injectors = []
@encoder = Encoder.new
end
def user_agent
"PayPalHttp-Ruby HTTP/1.1"
end
def add_injector(&block)
@injectors << block
end
def has_body(request)
request.respond_to?(:body) and request.body
end
def format_headers(headers)
formatted_headers = {}
headers.each do |key, value|
formatted_headers[key.downcase] = value
end
formatted_headers
end
def map_headers(raw_headers , formatted_headers)
raw_headers.each do |key, value|
if formatted_headers.key?(key.downcase) == true
raw_headers[key] = formatted_headers[key.downcase]
end
end
raw_headers
end
def execute(req)
headers = req.headers || {}
request = OpenStruct.new({
:verb => req.verb,
:path => req.path,
:headers => headers.clone,
:body => req.body,
})
if !request.headers
request.headers = {}
end
@injectors.each do |injector|
injector.call(request)
end
formatted_headers = format_headers(request.headers)
if !formatted_headers["user-agent"] || formatted_headers["user-agent"] == "Ruby"
request.headers["user-agent"] = user_agent
end
body = nil
if has_body(request)
raw_headers = request.headers
request.headers = formatted_headers
body = @encoder.serialize_request(request)
request.headers = map_headers(raw_headers, request.headers)
end
http_request = Net::HTTPGenericRequest.new(request.verb, body != nil, true, request.path, request.headers)
http_request.body = body
uri = URI(@environment.base_url)
Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
_parse_response(http.request(http_request))
end
end
def _parse_response(response)
status_code = response.code.to_i
result = response.body
headers = response.to_hash
if result && !result.empty?
deserialized = @encoder.deserialize_response(response.body, format_headers(headers))
if deserialized.is_a?(String) || deserialized.is_a?(Array)
result = deserialized
else
result = _parse_values(deserialized)
end
else
result = nil
end
obj = OpenStruct.new({
:status_code => status_code,
:result => result,
:headers => response.to_hash,
})
if status_code >= 200 and status_code < 300
return obj
elsif
raise HttpError.new(obj.status_code, obj.result, obj.headers)
end
end
def _parse_values(values)
obj = nil
if values.is_a?(Array)
obj = []
values.each do |v|
obj << _parse_values(v)
end
elsif values.is_a?(Hash)
obj = OpenStruct.new()
values.each do |k, v|
obj[k] = _parse_values(v)
end
else
obj = values
end
obj
end
end
end