Module: AI21::HTTP
- Includes:
- Helper
- Included in:
- Client
- Defined in:
- lib/ai21/http.rb
Instance Method Summary
collapse
Methods included from Helper
#camel_to_snake, #deep_transform_keys!, #snake_to_camel
Instance Method Details
#authorization ⇒ Object
53
54
55
|
# File 'lib/ai21/http.rb', line 53
def authorization
"Bearer #{AI21.configuration.access_token}"
end
|
#content_type ⇒ Object
57
58
59
|
# File 'lib/ai21/http.rb', line 57
def content_type
"application/json"
end
|
#delete(path) ⇒ Object
20
21
22
|
# File 'lib/ai21/http.rb', line 20
def delete(path)
fetch(path, :delete)
end
|
#fetch(path, method, body = nil) ⇒ Object
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/ai21/http.rb', line 24
def fetch(path, method, body = nil)
url = url(path)
http = http(url)
request = request(url, method)
request.body = body.to_json if body
body = http.request(request).read_body
camel_to_snake ::JSON.parse(body)
end
|
#get(path) ⇒ Object
12
13
14
|
# File 'lib/ai21/http.rb', line 12
def get(path)
fetch(path, :get)
end
|
#http(url) ⇒ Object
47
48
49
50
51
|
# File 'lib/ai21/http.rb', line 47
def http(url)
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
http
end
|
#post(path, body) ⇒ Object
16
17
18
|
# File 'lib/ai21/http.rb', line 16
def post(path, body)
fetch(path, :post, body)
end
|
#request(url, method) ⇒ Object
35
36
37
38
39
40
41
|
# File 'lib/ai21/http.rb', line 35
def request(url, method)
request = Object.const_get("Net::HTTP::#{method.capitalize}").new(url)
request["accept"] = content_type
request["content-type"] = content_type
request["Authorization"] = authorization
request
end
|