Class: ClientApi::Request
- Inherits:
-
Object
- Object
- ClientApi::Request
show all
- Includes:
- ClientApi
- Defined in:
- lib/client-api/request.rb
Direct Known Subclasses
Api
Constant Summary
Constants included
from ClientApi
VERSION
Instance Method Summary
collapse
Methods included from ClientApi
#base_url, #basic_auth, configuration, configure, #datatype, #deep_traverse, #headers, #is_num?, #json_output, #payload, #time_out, #url_generator, #validate, #validate_empty, #validate_headers, #validate_json, #validate_key, #validate_list, #validate_schema, #validate_size
Constructor Details
#initialize(scenario) ⇒ Request
Returns a new instance of Request.
10
11
12
13
|
# File 'lib/client-api/request.rb', line 10
def initialize(scenario)
@scenario = scenario
$logger.debug("Requested scenario == '#{@scenario.description}'") if $logger
end
|
Instance Method Details
#base_url_definition(args) ⇒ Object
92
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/client-api/request.rb', line 92
def base_url_definition(args)
raise "Invalid (or) incomplete URL: #{base_url + args}" unless (['https://', 'http://'].any? { |e| (base_url + args).include? e })
if (base_url[-1, 1] == '/') && (args[0] == '/')
base_url.gsub(/\/$/, '')
elsif (base_url[-1, 1] != '/') && (args[0] != '/')
base_url.concat('', '/')
else
base_url
end
end
|
#basic_encode(options = {}) ⇒ Object
119
120
121
|
# File 'lib/client-api/request.rb', line 119
def basic_encode(options = {})
'Basic ' + ["#{options[:username]}:#{options[:password]}"].pack('m0')
end
|
#connect(args) ⇒ Object
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
# File 'lib/client-api/request.rb', line 104
def connect(args)
http = Net::HTTP.new(uri(args).host, uri(args).port)
if uri(args).scheme == "https"
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
http.read_timeout = time_out.to_i
@http = http
elsif uri(args).scheme == "http"
http.use_ssl = false
http.read_timeout = time_out.to_i
@http = http
end
end
|
#delete_request(url, options = {}) ⇒ Object
52
53
54
55
56
|
# File 'lib/client-api/request.rb', line 52
def delete_request(url, options = {})
connect(url)
pre_logger(:log_url => uri(url), :log_header => (options), :log_method => 'DELETE') if $logger
@http.delete(uri(url).path, = (options))
end
|
#delete_with_body_request(url, options = {}) ⇒ Object
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/client-api/request.rb', line 58
def delete_with_body_request(url, options = {})
body = options[:body] || {}
connect(url)
pre_logger(:log_url => uri(url), :log_header => (options), :log_body => body, :log_method => 'GET') if $logger
request = Net::HTTP::Delete.new(uri(url))
request.body = body.to_json
(options).each { |key,value| request.add_field(key,value)}
@http.request(request)
end
|
#get_request(url, options = {}) ⇒ Object
15
16
17
18
19
|
# File 'lib/client-api/request.rb', line 15
def get_request(url, options = {})
connect(url)
pre_logger(:log_url => uri(url), :log_header => (options), :log_method => 'GET') if $logger
@http.get(uri(url).request_uri, = (options))
end
|
#get_with_body_request(url, options = {}) ⇒ Object
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/client-api/request.rb', line 21
def get_with_body_request(url, options = {})
body = options[:body] || {}
connect(url)
pre_logger(:log_url => uri(url), :log_header => (options), :log_body => body, :log_method => 'GET') if $logger
request = Net::HTTP::Get.new(uri(url))
request.body = body.to_json
(options).each { |key,value| request.add_field(key,value)}
@http.request(request)
end
|
123
124
125
126
127
128
129
130
131
132
133
|
# File 'lib/client-api/request.rb', line 123
def (options = {})
= options[:headers] || {}
authorization = basic_encode(:username => basic_auth['Username'], :password => basic_auth['Password'])
if == nil || == ""
= {}
else
=
end
['Authorization'] = authorization if authorization != "Basic Og=="
.merge()
end
|
#patch_request(url, options = {}) ⇒ Object
76
77
78
79
80
81
|
# File 'lib/client-api/request.rb', line 76
def patch_request(url, options = {})
body = options[:body] || {}
connect(url)
pre_logger(:log_url => uri(url), :log_header => (options), :log_body => body, :log_method => 'PATCH') if $logger
@http.patch(uri(url).path, body.to_json, = (options))
end
|
#post_request(url, options = {}) ⇒ Object
32
33
34
35
36
37
|
# File 'lib/client-api/request.rb', line 32
def post_request(url, options = {})
body = options[:body] || {}
connect(url)
pre_logger(:log_url => uri(url), :log_header => (options), :log_body => body, :log_method => 'POST') if $logger
@http.post(uri(url).path, body.to_json, = (options))
end
|
#post_request_x(url, options = {}) ⇒ Object
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/client-api/request.rb', line 39
def post_request_x(url, options = {})
body = options[:body]
connect(url)
request = Net::HTTP::Post.new(uri(url))
body['data'].each { |key,value| request.set_form([[key.to_s,File.open(value)]], body['type'])}
= (options).delete_if{ |k,| ['Content-Type', 'content-type', 'Content-type', 'content-Type'].include? k }
.each { |key,value| request.add_field(key,value)}
pre_logger(:log_url => uri(url), :log_header => (options), :log_body => body, :log_method => 'POST') if $logger
@http.request(request)
end
|
#pre_logger(options = {}) ⇒ Object
135
136
137
138
139
140
141
|
# File 'lib/client-api/request.rb', line 135
def pre_logger(options = {})
options[:log_body] = 'not available' if options[:log_body].nil?
$logger.debug("Requested method == #{options[:log_method]}")
$logger.debug("Requested url == #{options[:log_url]}")
$logger.debug("Requested headers == #{options[:log_header]}")
$logger.debug("Requested body == #{options[:log_body]}")
end
|
#put_request(url, options = {}) ⇒ Object
69
70
71
72
73
74
|
# File 'lib/client-api/request.rb', line 69
def put_request(url, options = {})
body = options[:body] || {}
connect(url)
pre_logger(:log_url => uri(url), :log_header => (options), :log_body => body, :log_method => 'PUT') if $logger
@http.put(uri(url).path, body.to_json, = (options))
end
|
#uri(args) ⇒ Object
83
84
85
86
87
88
89
90
|
# File 'lib/client-api/request.rb', line 83
def uri(args)
if (args.include? "http://") || (args.include? "https://")
URI.parse(args)
else
base_url = base_url_definition(args)
URI.parse(base_url + args)
end
end
|