Class: HaveAPI::ClientExamples::Http
Direct Known Subclasses
Curl
Instance Attribute Summary
#action, #action_name, #base_url, #host, #resource, #resource_path, #version
Instance Method Summary
collapse
auth, clients, code, example, init, #initialize, label, order, register, #version_url
Instance Method Details
#auth(method, desc) ⇒ Object
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
|
# File 'lib/haveapi/client_examples/http.rb', line 19
def auth(method, desc)
case method
when :basic
" GET / HTTP/1.1\n Host: \#{host}\n Authorization: Basic dXNlcjpzZWNyZXQ=\n\n END\n\n when :token\n login = auth_token_credentials(desc).merge(lifetime: 'fixed')\n\n <<~END\n POST /_auth/token/tokens HTTP/1.1\n Host: \#{host}\n Content-Type: application/json\n\n \#{JSON.pretty_generate({ token: login })}\n END\n\n when :oauth2\n <<~END\n # 1) Request authorization code\n GET \#{desc[:authorize_path]}?response_type=code&client_id=$client_id&state=$state&redirect_uri=$client_redirect_uri HTTP/1.1\n Host: \#{host}\n\n # 2) The user logs in using this API\n\n # 3) The API then redirects the user back to the client application\n GET $client_redirect_uri?code=$authorization_code&state=$state\n Host: client-application\n\n # 4) The client application requests access token\n POST \#{desc[:token_path]}\n Content-Type: application/x-www-form-urlencoded\n\n grant_type=authorization_code&code=$authorization_code&redirect_uri=$client_redirect_uri&client_id=$client_id&client_secret=$client_secret\n END\n end\nend\n"
|
#init ⇒ Object
11
12
13
14
15
16
17
|
# File 'lib/haveapi/client_examples/http.rb', line 11
def init
" OPTIONS /v\#{version}/ HTTP/1.1\n Host: \#{host}\n\n END\nend\n"
|
#request(sample) ⇒ Object
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/haveapi/client_examples/http.rb', line 61
def request(sample)
path = resolve_path(
action[:method],
action[:path],
sample[:path_params] || [],
sample[:request]
)
req = "#{action[:method]} #{path} HTTP/1.1\n"
req << "Host: #{host}\n"
req << "Content-Type: application/json\n\n"
if action[:method] != 'GET' && sample[:request] && !sample[:request].empty?
req << JSON.pretty_generate({ action[:input][:namespace] => sample[:request] })
end
req
end
|
#resolve_path(method, path, path_params, input_params) ⇒ Object
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/haveapi/client_examples/http.rb', line 97
def resolve_path(method, path, path_params, input_params)
ret = path.clone
path_params.each do |v|
ret.sub!(/\{[a-zA-Z\-_]+\}/, v.to_s)
end
return ret if method != 'GET' || !input_params || input_params.empty?
ret << '?'
ret << input_params.map { |k, v| "#{k}=#{CGI.escape(v.to_s)}" }.join('&')
ret
end
|
#response(sample) ⇒ Object
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
# File 'lib/haveapi/client_examples/http.rb', line 80
def response(sample)
content = JSON.pretty_generate({
status: sample[:status],
message: sample[:message],
response: { action[:output][:namespace] => sample[:response] },
errors: sample[:errors]
})
status_msg = Rack::Utils::HTTP_STATUS_CODES[sample[:http_status]]
res = "HTTP/1.1 #{sample[:http_status]} #{status_msg}\n"
res << "Content-Type: application/json;charset=utf-8\n"
res << "Content-Length: #{content.size}\n\n"
res << content
res
end
|