Class: LeanTesting::APIRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/BaseClass/APIRequest.rb

Instance Method Summary collapse

Constructor Details

#initialize(origin, endpoint, method, opts = nil) ⇒ APIRequest

Constructs API request definition.

Arguments: origin Client – Originating client reference endpoint String – API endpoint method String – Method for cURL call - supports GET, POST, PUT or DELETE only opts Hash – (optional) Additional options to pass to request. Request parameters (if any) must bep assed here.

Exceptions: SDKInvalidArgException if method is non-string. SDKInvalidArgException if unsupported method is provided. SDKInvalidArgException if endpoint is non-string. SDKInvalidArgException if opts param is not a hash.



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
# File 'lib/BaseClass/APIRequest.rb', line 26

def initialize(origin, endpoint, method, opts = nil)
	@default_ops = {									# Basic support for extended opts
		'base_uri'	=> 'https://api.leantesting.com',	# assumed default for API base
		'form_data'	=> false,							# sets content type to multipart/form-data if true
		'params'	=> {}								# params to be pased in request
	}

	if !opts
		opts = {}
	end

	if !method.is_a? String
		raise SDKInvalidArgException, '`method` must be a string'
	elsif !['GET', 'POST', 'PUT', 'DELETE'].include? method
		raise SDKInvalidArgException, 'unsupported ' + method + ' `method`'
	elsif !endpoint.is_a? String
		raise SDKInvalidArgException, '`endpoint` must be a string'
	elsif !opts.is_a? Hash
		raise SDKInvalidArgException, '`opts` must be a hash'
	end

	@opts = @default_ops.clone
	self.updateOpts(opts)

	@origin		= origin
	@endpoint	= endpoint
	@method		= method
end

Instance Method Details

#callObject

Executes cURL call as per current API definition state.

Returns: String – Returns resulting data response from server (including errors and inconsistencies)



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
135
136
137
138
139
140
# File 'lib/BaseClass/APIRequest.rb', line 85

def call

	ch = Curl::Easy.new

	callUrl = @opts['base_uri'] + @endpoint

	if @origin.getCurrentToken.is_a? String
		ch.headers['Authorization'] = 'Bearer ' + @origin.getCurrentToken
	end

	ch.url = callUrl

	ch.header_in_body = false

	case @method
	when 'GET'
		callUrl += '?' + Curl::postalize(@opts['params'])
		ch.url = callUrl

		ch.http_get
	when 'POST'
		if @opts['form_data'] == true && (@opts.has_key? 'file_path')
			ch.headers['Content-Type'] = 'multipart/form-data'
			ch.multipart_form_post = true

			ch.http_post(Curl::PostField.file('file', @opts['file_path']))
		else
			jsonData = JSON.generate(@opts['params'])

			ch.headers['Content-Type'] = 'application/json'
			ch.headers['Content-Length'] = jsonData.length

			ch.http_post(jsonData)
		end
	when 'PUT'
		jsonData = JSON.generate(@opts['params'])

		ch.headers['Content-Type'] = 'application/json'
		ch.headers['Content-Length'] = jsonData.length

		ch.http_put(jsonData)
	when 'DELETE'
		ch.http_delete
	end

	curlData	= ch.body_str
	curlStatus	= ch.status.to_i()

	ch.close
	ch = nil

	{
		'data' => curlData,
		'status' => curlStatus
	}
end

#execObject

Does cURL data interpretation

Exceptions: SDKErrorResponseException if the remote response is an error. A server response is interpreted as an error if obtained status code differs from expected status code. Expected status codes are ‘200 OK` for GET/POST/PUT, `204 No Content` for DELETE. SDKBadJSONResponseException if the remote response contains erronated or invalid JSON contents

Returns: Hash – In case of successful request, a JSON decoded object is returned. Boolean – If a DELETE request is issued, returns true if call is successful (exception otherwise).



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/BaseClass/APIRequest.rb', line 155

def exec
	if @origin.debugReturn && @origin.debugReturn.has_key?('data') && @origin.debugReturn.has_key?('status')

		curlData = @origin.debugReturn['data']
		curlStatus = @origin.debugReturn['status']

	else

		callReturn = call
		curlData = callReturn['data']
		curlStatus = callReturn['status']

	end

	if @method == 'DELETE'
		expectedHTTPStatus = 204
	else
		expectedHTTPStatus = 200
	end

	if curlStatus != expectedHTTPStatus
		raise SDKErrorResponseException, curlStatus.to_s() + ' - ' + curlData
	end

	if @method == 'DELETE'				# if DELETE request, expect no output
		return true
	end

	begin
		jsonData = JSON.parse(curlData)	# normally, expect JSON qualified output
	rescue JSON::ParserError
		raise SDKBadJSONResponseException, curlData
	end

	if jsonData.length.zero?
		raise SDKUnexpectedResponseException, 'Empty object received'
	end

	return jsonData

end

#updateOpts(opts = nil) ⇒ Object

Updates options list inside API request definition.

Arguments: opts Hash – (optional) Additional options array to merge with previous option values

Exceptions: SDKInvalidArgException if opts param is not a hash. SDKInvalidArgException if provided parameter list is non-hash parameter.



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/BaseClass/APIRequest.rb', line 65

def updateOpts(opts = nil)
	if !opts
		opts = {}
	end

	if !opts.is_a? Hash
		raise SDKInvalidArgException, '`opts` must be a hash'
	elsif opts.has_key? 'params' && !opts['params'].is_a?(Hash)
		raise SDKInvalidArgException '`opts[\'params\']` must be a hash'
	end

	@opts.merge!(opts)
end