Class: ThreeScale::API::HttpClient
- Inherits:
-
Object
- Object
- ThreeScale::API::HttpClient
show all
- Defined in:
- lib/3scale/api/http_client.rb
Defined Under Namespace
Modules: JSONParser
Classes: ForbiddenError
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#delete(path, params: nil) ⇒ Object
-
#forbidden!(response) ⇒ Object
-
#get(path, params: nil) ⇒ Object
-
#initialize(endpoint:, provider_key:, format: :json) ⇒ HttpClient
constructor
A new instance of HttpClient.
-
#parse(response) ⇒ Object
-
#parser ⇒ Object
-
#patch(path, body:, params: nil) ⇒ Object
-
#post(path, body:, params: nil) ⇒ Object
-
#put(path, body: nil, params: nil) ⇒ Object
-
#serialize(body) ⇒ Object
Constructor Details
#initialize(endpoint:, provider_key:, format: :json) ⇒ HttpClient
Returns a new instance of HttpClient.
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/3scale/api/http_client.rb', line 10
def initialize(endpoint:, provider_key:, format: :json)
@endpoint = URI(endpoint).freeze
@admin_domain = @endpoint.host.freeze
@provider_key = provider_key.freeze
@http = Net::HTTP.new(admin_domain, @endpoint.port)
@http.use_ssl = @endpoint.is_a?(URI::HTTPS)
= {
'Accept' => "application/#{format}",
'Content-Type' => "application/#{format}",
'Authorization' => 'Basic ' + [":#{@provider_key}"].pack('m').delete("\r\n")
}
if debug?
@http.set_debug_output($stdout)
['Accept-Encoding'] = 'identity'
end
.freeze
@format = format
end
|
Instance Attribute Details
#admin_domain ⇒ Object
Returns the value of attribute admin_domain.
8
9
10
|
# File 'lib/3scale/api/http_client.rb', line 8
def admin_domain
@admin_domain
end
|
#endpoint ⇒ Object
Returns the value of attribute endpoint.
8
9
10
|
# File 'lib/3scale/api/http_client.rb', line 8
def endpoint
@endpoint
end
|
Returns the value of attribute format.
8
9
10
|
# File 'lib/3scale/api/http_client.rb', line 8
def format
@format
end
|
Returns the value of attribute headers.
8
9
10
|
# File 'lib/3scale/api/http_client.rb', line 8
def
end
|
#provider_key ⇒ Object
Returns the value of attribute provider_key.
8
9
10
|
# File 'lib/3scale/api/http_client.rb', line 8
def provider_key
@provider_key
end
|
Instance Method Details
#delete(path, params: nil) ⇒ Object
49
50
51
|
# File 'lib/3scale/api/http_client.rb', line 49
def delete(path, params: nil)
parse @http.delete(format_path_n_query(path, params), )
end
|
#forbidden!(response) ⇒ Object
64
65
66
|
# File 'lib/3scale/api/http_client.rb', line 64
def forbidden!(response)
raise ForbiddenError, response
end
|
#get(path, params: nil) ⇒ Object
33
34
35
|
# File 'lib/3scale/api/http_client.rb', line 33
def get(path, params: nil)
parse @http.get(format_path_n_query(path, params), )
end
|
#parse(response) ⇒ Object
54
55
56
57
58
59
60
|
# File 'lib/3scale/api/http_client.rb', line 54
def parse(response)
case response
when Net::HTTPUnprocessableEntity, Net::HTTPSuccess then parser.decode(response.body)
when Net::HTTPForbidden then forbidden!(response)
else "Can't handle #{response.inspect}"
end
end
|
#parser ⇒ Object
76
77
78
79
80
81
|
# File 'lib/3scale/api/http_client.rb', line 76
def parser
case format
when :json then JSONParser
else "unknown format #{format}"
end
end
|
#patch(path, body:, params: nil) ⇒ Object
37
38
39
|
# File 'lib/3scale/api/http_client.rb', line 37
def patch(path, body:, params: nil)
parse @http.patch(format_path_n_query(path, params), serialize(body), )
end
|
#post(path, body:, params: nil) ⇒ Object
41
42
43
|
# File 'lib/3scale/api/http_client.rb', line 41
def post(path, body:, params: nil)
parse @http.post(format_path_n_query(path, params), serialize(body), )
end
|
#put(path, body: nil, params: nil) ⇒ Object
45
46
47
|
# File 'lib/3scale/api/http_client.rb', line 45
def put(path, body: nil, params: nil)
parse @http.put(format_path_n_query(path, params), serialize(body), )
end
|
#serialize(body) ⇒ Object
68
69
70
71
72
73
74
|
# File 'lib/3scale/api/http_client.rb', line 68
def serialize(body)
case body
when nil then nil
when String then body
else parser.encode(body)
end
end
|