-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_client.rb
106 lines (94 loc) · 2.53 KB
/
api_client.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#
# TestRail API binding for Ruby (API v2, available since TestRail 3.0)
#
# Learn more:
#
# http://docs.gurock.com/testrail-api2/start
# http://docs.gurock.com/testrail-api2/accessing
#
# Copyright Gurock Software GmbH. See license.md for details.
#
require 'net/http'
require 'net/https'
require 'uri'
require 'json'
module TestRail
class APIClient
@url = ''
@user = ''
@password = ''
attr_accessor :user
attr_accessor :password
def initialize(base_url)
if !base_url.match(/\/$/)
base_url += '/'
end
@url = base_url + 'index.php?/api/v2/'
end
#
# Send Get
#
# Issues a GET request (read) against the API and returns the result
# (as Ruby hash).
#
# Arguments:
#
# uri The API method to call including parameters
# (e.g. get_case/1)
#
def send_get(uri)
_send_request('GET', uri, nil)
end
#
# Send POST
#
# Issues a POST request (write) against the API and returns the result
# (as Ruby hash).
#
# Arguments:
#
# uri The API method to call including parameters
# (e.g. add_case/1)
# data The data to submit as part of the request (as
# Ruby hash, strings must be UTF-8 encoded)
#
def send_post(uri, data)
_send_request('POST', uri, data)
end
private
def _send_request(method, uri, data)
url = URI.parse(@url + uri)
if method == 'POST'
request = Net::HTTP::Post.new(url.path + '?' + url.query)
request.body = JSON.dump(data)
else
request = Net::HTTP::Get.new(url.path + '?' + url.query)
end
request.basic_auth(@user, @password)
request.add_field('Content-Type', 'application/json')
conn = Net::HTTP.new(url.host, url.port)
if url.scheme == 'https'
conn.use_ssl = true
conn.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
response = conn.request(request)
if response.body && !response.body.empty?
result = JSON.parse(response.body)
else
result = {}
end
if response.code != '200'
if result && result.key?('error')
error = '"' + result['error'] + '"'
else
error = 'No additional error message received'
end
raise APIError.new('TestRail API returned HTTP %s (%s)' %
[response.code, error])
end
result
end
end
class APIError < StandardError
end
end