forked from zammad/zammad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_client.rb
89 lines (73 loc) · 2.13 KB
/
http_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
# Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
require 'uri'
class GitLab
class HttpClient
attr_reader :api_token, :endpoint, :verify_ssl
def initialize(endpoint, api_token, verify_ssl: true)
raise __('Invalid GitLab configuration (missing endpoint or api_token).') if api_token.blank? || endpoint.blank? || endpoint.exclude?('/graphql') || endpoint.scan(URI::DEFAULT_PARSER.make_regexp).blank?
@api_token = api_token
@endpoint = endpoint
@verify_ssl = verify_ssl
end
# returns path of the subfolder of the endpoint if exists
def endpoint_path
path = URI.parse(endpoint).path
return if path.blank?
return if path == '/api/graphql'
if path.start_with?('/')
path = path[1..]
end
path.sub('api/graphql', '')
end
def perform(payload)
response = UserAgent.post(
endpoint,
payload,
{
headers: headers,
json: true,
open_timeout: 6,
read_timeout: 16,
log: {
facility: 'GitLab',
},
verify_ssl: verify_ssl,
},
)
if !response.success?
Rails.logger.error response.error
raise __('GitLab request failed. Please have a look at the log file for details.')
end
response.data
end
def perform_rest_get_request(variables)
uri = URI.parse(endpoint)
return if uri.blank? || variables.blank?
response = UserAgent.get(
"#{uri.scheme}://#{uri.host}/api/v4/projects/#{ERB::Util.url_encode(variables[:fullpath])}/issues/#{variables[:issue_id]}",
{},
{
headers: headers,
json: true,
open_timeout: 6,
read_timeout: 16,
log: {
facility: 'GitLab',
},
verify_ssl: verify_ssl,
},
)
if !response.success?
Rails.logger.error response.error
return
end
JSON.parse(response.body)
end
private
def headers
{
'PRIVATE-TOKEN': @api_token
}
end
end
end