zammad_api 1.4.0 → 2.0.0.alpha1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.yardopts +11 -0
- data/CHANGELOG.md +235 -0
- data/LICENSE.AGPL.txt +661 -0
- data/LICENSE.MIT.txt +7 -0
- data/LICENSE.md +6 -0
- data/README.md +794 -0
- data/UPGRADING.md +158 -0
- data/lib/zammad_api/attribute_access.rb +121 -0
- data/lib/zammad_api/client.rb +243 -43
- data/lib/zammad_api/collection.rb +233 -0
- data/lib/zammad_api/config.rb +239 -0
- data/lib/zammad_api/deep_copy.rb +43 -0
- data/lib/zammad_api/errors.rb +97 -35
- data/lib/zammad_api/resource_proxy.rb +195 -0
- data/lib/zammad_api/resources/base.rb +196 -93
- data/lib/zammad_api/resources/group.rb +12 -2
- data/lib/zammad_api/resources/organization.rb +12 -2
- data/lib/zammad_api/resources/ticket.rb +42 -19
- data/lib/zammad_api/resources/ticket_article.rb +14 -7
- data/lib/zammad_api/resources/ticket_article_attachment.rb +29 -13
- data/lib/zammad_api/resources/ticket_priority.rb +10 -2
- data/lib/zammad_api/resources/ticket_state.rb +9 -2
- data/lib/zammad_api/resources/user.rb +16 -2
- data/lib/zammad_api/resources.rb +13 -13
- data/lib/zammad_api/response.rb +52 -0
- data/lib/zammad_api/transport.rb +212 -40
- data/lib/zammad_api/version.rb +4 -1
- data/lib/zammad_api.rb +16 -3
- data/sig/zammad_api/attribute_access.rbs +31 -0
- data/sig/zammad_api/client.rbs +42 -0
- data/sig/zammad_api/collection.rbs +46 -0
- data/sig/zammad_api/config.rbs +69 -0
- data/sig/zammad_api/deep_copy.rbs +7 -0
- data/sig/zammad_api/errors.rbs +61 -0
- data/sig/zammad_api/resource_proxy.rbs +41 -0
- data/sig/zammad_api/resources/base.rbs +66 -0
- data/sig/zammad_api/resources/resources.rbs +201 -0
- data/sig/zammad_api/response.rbs +16 -0
- data/sig/zammad_api/transport.rbs +47 -0
- data/sig/zammad_api.rbs +3 -0
- metadata +69 -15
- data/lib/zammad_api/dispatcher.rb +0 -12
- data/lib/zammad_api/json_helper.rb +0 -11
- data/lib/zammad_api/list_all.rb +0 -11
- data/lib/zammad_api/list_base.rb +0 -84
- data/lib/zammad_api/list_search.rb +0 -11
- data/lib/zammad_api/log.rb +0 -18
|
@@ -1,18 +1,34 @@
|
|
|
1
|
-
|
|
2
|
-
def initialize(transport, attributes = {}) # rubocop:disable Lint/MissingSuper
|
|
3
|
-
@transport = transport
|
|
4
|
-
@attributes = attributes
|
|
5
|
-
symbolize_keys_deep!(@attributes)
|
|
6
|
-
end
|
|
1
|
+
# frozen_string_literal: true
|
|
7
2
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
3
|
+
module ZammadAPI
|
|
4
|
+
module Resources
|
|
5
|
+
# An attachment of a ticket article.
|
|
6
|
+
#
|
|
7
|
+
# Attachments are read-only metadata until {#download} is called, which
|
|
8
|
+
# returns the file contents.
|
|
9
|
+
class TicketArticleAttachment
|
|
10
|
+
include AttributeAccess
|
|
11
|
+
|
|
12
|
+
# @api private
|
|
13
|
+
# @param transport [Transport]
|
|
14
|
+
# @param attributes [Hash]
|
|
15
|
+
def initialize(transport, attributes = {})
|
|
16
|
+
@transport = transport
|
|
17
|
+
@attributes = frozen_attributes(Hash(attributes)) # steep:ignore UnresolvedOverloading
|
|
18
|
+
end
|
|
11
19
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
20
|
+
# Downloads the attachment.
|
|
21
|
+
#
|
|
22
|
+
# @return [String] the file contents, in +ASCII-8BIT+ encoding
|
|
23
|
+
# @raise [ResponseError] when Zammad rejected the request
|
|
24
|
+
def download
|
|
25
|
+
segments = %i[ticket_id article_id id].map { Transport.escape_path_segment(fetch(it)) }
|
|
26
|
+
@transport.get("api/v1/ticket_attachment/#{segments.join('/')}").raw_body.b
|
|
27
|
+
end
|
|
15
28
|
|
|
16
|
-
|
|
29
|
+
def inspect
|
|
30
|
+
"#<#{self.class.name} id=#{id.inspect} filename=#{self[:filename].inspect} size=#{self[:size].inspect}>"
|
|
31
|
+
end
|
|
32
|
+
end
|
|
17
33
|
end
|
|
18
34
|
end
|
|
@@ -1,3 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ZammadAPI
|
|
4
|
+
module Resources
|
|
5
|
+
# A ticket priority, such as "2 normal". Reached through
|
|
6
|
+
# {Client#ticket_priorities}.
|
|
7
|
+
class TicketPriority < Base
|
|
8
|
+
path 'api/v1/ticket_priorities'
|
|
9
|
+
end
|
|
10
|
+
end
|
|
3
11
|
end
|
|
@@ -1,3 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ZammadAPI
|
|
4
|
+
module Resources
|
|
5
|
+
# A ticket state, such as "open". Reached through {Client#ticket_states}.
|
|
6
|
+
class TicketState < Base
|
|
7
|
+
path 'api/v1/ticket_states'
|
|
8
|
+
end
|
|
9
|
+
end
|
|
3
10
|
end
|
|
@@ -1,3 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ZammadAPI
|
|
4
|
+
module Resources
|
|
5
|
+
# A user: a customer, an agent or an administrator. Reached through
|
|
6
|
+
# {Client#users}, and the one the client authenticates as through {Client#me}.
|
|
7
|
+
class User < Base
|
|
8
|
+
path 'api/v1/users'
|
|
9
|
+
|
|
10
|
+
# UsersController#index hardcodes `reorder(id: :asc)`, so it reads nothing
|
|
11
|
+
# but the paging - not even sort_by.
|
|
12
|
+
sortable false
|
|
13
|
+
|
|
14
|
+
searchable true
|
|
15
|
+
end
|
|
16
|
+
end
|
|
3
17
|
end
|
data/lib/zammad_api/resources.rb
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
require 'zammad_api/resources/ticket_priority'
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'resources/base'
|
|
4
|
+
require_relative 'resources/group'
|
|
5
|
+
require_relative 'resources/organization'
|
|
6
|
+
require_relative 'resources/ticket'
|
|
7
|
+
require_relative 'resources/ticket_article'
|
|
8
|
+
require_relative 'resources/ticket_article_attachment'
|
|
9
|
+
require_relative 'resources/ticket_priority'
|
|
10
|
+
require_relative 'resources/ticket_state'
|
|
11
|
+
require_relative 'resources/user'
|
|
13
12
|
|
|
14
13
|
module ZammadAPI
|
|
15
|
-
|
|
14
|
+
# Namespace for the Zammad record classes.
|
|
15
|
+
module Resources
|
|
16
16
|
end
|
|
17
17
|
end
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'json'
|
|
4
|
+
|
|
5
|
+
module ZammadAPI
|
|
6
|
+
# A decoded HTTP response.
|
|
7
|
+
#
|
|
8
|
+
# This deliberately does not expose Faraday objects, so that the HTTP client
|
|
9
|
+
# stays an implementation detail of {Transport}.
|
|
10
|
+
#
|
|
11
|
+
# @!attribute [r] status
|
|
12
|
+
# @return [Integer] HTTP status code
|
|
13
|
+
# @!attribute [r] headers
|
|
14
|
+
# @return [Hash{String => String}] response headers, keys downcased
|
|
15
|
+
# @!attribute [r] body
|
|
16
|
+
# @return [Hash, Array, String] JSON responses are decoded with symbol
|
|
17
|
+
# keys; every other content type is left as the raw body
|
|
18
|
+
# @!attribute [r] raw_body
|
|
19
|
+
# @return [String] the undecoded response body
|
|
20
|
+
Response = Data.define(:status, :headers, :body, :raw_body)
|
|
21
|
+
|
|
22
|
+
class Response
|
|
23
|
+
# @return [Boolean] whether the status code is in the 2xx range
|
|
24
|
+
def success? = status.between?(200, 299)
|
|
25
|
+
|
|
26
|
+
# Decodes a JSON body with symbol keys. Anything else - a proxy error page,
|
|
27
|
+
# a file download, a body that does not parse - is handed back as it is.
|
|
28
|
+
#
|
|
29
|
+
# @api private
|
|
30
|
+
# @param content_type [String, nil]
|
|
31
|
+
# @param raw_body [String]
|
|
32
|
+
# @return [Hash, Array, String]
|
|
33
|
+
def self.decode_body(content_type, raw_body)
|
|
34
|
+
return raw_body if !content_type.to_s.include?('json') || raw_body.empty?
|
|
35
|
+
|
|
36
|
+
JSON.parse(raw_body, symbolize_names: true)
|
|
37
|
+
rescue JSON::ParserError
|
|
38
|
+
raw_body
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# The body, checked to be the JSON object or array the caller expects.
|
|
42
|
+
#
|
|
43
|
+
# @param shape [:object, :array]
|
|
44
|
+
# @return [Hash, Array]
|
|
45
|
+
# @raise [ParseError] when the body has another shape
|
|
46
|
+
def decoded(shape)
|
|
47
|
+
return body if body.is_a?(shape == :object ? Hash : Array)
|
|
48
|
+
|
|
49
|
+
raise ParseError, "expected a JSON #{shape}, got #{body.class}"
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
data/lib/zammad_api/transport.rb
CHANGED
|
@@ -1,63 +1,235 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'faraday'
|
|
2
|
-
require '
|
|
4
|
+
require 'faraday/retry'
|
|
5
|
+
require 'uri'
|
|
3
6
|
|
|
4
7
|
module ZammadAPI
|
|
8
|
+
# Performs the HTTP requests against a Zammad instance.
|
|
9
|
+
#
|
|
10
|
+
# Instances are immutable once built: {#with_on_behalf_of} returns a copy
|
|
11
|
+
# rather than mutating shared state, which makes a single transport safe to
|
|
12
|
+
# use from several threads.
|
|
13
|
+
#
|
|
14
|
+
# @api private
|
|
5
15
|
class Transport
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
16
|
+
# The method that is retried: the one that only reads. A write that timed
|
|
17
|
+
# out may still have been applied, so it is not sent twice.
|
|
18
|
+
RETRIABLE_METHODS = %i[get].freeze
|
|
19
|
+
|
|
20
|
+
# Statuses a read is retried on.
|
|
21
|
+
RETRIABLE_STATUSES = [429, 500, 502, 503, 504].freeze
|
|
22
|
+
|
|
23
|
+
# Errors a read is retried on. The adapter wraps socket and TLS errors
|
|
24
|
+
# into Faraday's own classes.
|
|
25
|
+
RETRIABLE_EXCEPTIONS = [Faraday::RetriableResponse, Faraday::ConnectionFailed, Faraday::TimeoutError].freeze
|
|
26
|
+
|
|
27
|
+
# Keys whose values are never written to the log. Matched anywhere in the
|
|
28
|
+
# name, as Zammad's own log filter does, so +monkey_id+ is hidden too.
|
|
29
|
+
SENSITIVE_KEY_PATTERN = /passw|pwd|bind_pw|passphrase|secret|token|credential|cookie|authorization|key|otp|salt/i
|
|
30
|
+
|
|
31
|
+
# A path that opens with a scheme, which Faraday would send to that host.
|
|
32
|
+
ABSOLUTE_URL = /\A[a-z][a-z0-9+.-]*:/i
|
|
33
|
+
private_constant :ABSOLUTE_URL
|
|
34
|
+
|
|
35
|
+
# @return [Config]
|
|
36
|
+
attr_reader :config
|
|
37
|
+
|
|
38
|
+
# @return [String, nil] the user requests are performed for, sent as the
|
|
39
|
+
# +From+ header
|
|
40
|
+
attr_reader :on_behalf_of
|
|
41
|
+
|
|
42
|
+
# A record id as a path segment, percent-encoded.
|
|
43
|
+
#
|
|
44
|
+
# @param value [Integer, String]
|
|
45
|
+
# @return [String]
|
|
46
|
+
# @raise [ArgumentError] for an id that is nil or empty
|
|
47
|
+
def self.escape_path_segment(value)
|
|
48
|
+
segment = value.to_s
|
|
49
|
+
raise ArgumentError, 'a record id is required' if segment.empty?
|
|
50
|
+
|
|
51
|
+
URI.encode_uri_component(segment)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# A request path relative to the instance URL. A leading slash is
|
|
55
|
+
# dropped, so paths can be pasted from the Zammad documentation.
|
|
56
|
+
#
|
|
57
|
+
# @param path [String]
|
|
58
|
+
# @return [String]
|
|
59
|
+
# @raise [ArgumentError] for an absolute URL, which would carry this
|
|
60
|
+
# client's credentials to another host
|
|
61
|
+
def self.relative_path(path)
|
|
62
|
+
relative = path.to_s.sub(%r{\A/+}, '')
|
|
63
|
+
if ABSOLUTE_URL.match?(relative)
|
|
64
|
+
raise ArgumentError, "a request path is relative to the instance URL, and this one is an absolute URL: pass the path alone, such as 'api/v1/tickets'"
|
|
15
65
|
end
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
66
|
+
|
|
67
|
+
relative
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# A path as it may be logged or named in an error: without a query string
|
|
71
|
+
# or fragment, which may carry a token.
|
|
72
|
+
#
|
|
73
|
+
# @param path [String]
|
|
74
|
+
# @return [String]
|
|
75
|
+
def self.redact_path(path) = path.to_s.sub(/[?#].*/m, '')
|
|
76
|
+
|
|
77
|
+
# Replaces the values of sensitive keys, at any depth, for the log.
|
|
78
|
+
#
|
|
79
|
+
# @param value [Object]
|
|
80
|
+
# @return [Object]
|
|
81
|
+
def self.redact(value)
|
|
82
|
+
case value
|
|
83
|
+
when Hash then value.to_h { |key, nested| [key, SENSITIVE_KEY_PATTERN.match?(key.to_s) ? Config::REDACTION : redact(nested)] }
|
|
84
|
+
when Array then value.map { redact(it) }
|
|
85
|
+
else value
|
|
23
86
|
end
|
|
24
87
|
end
|
|
25
88
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
89
|
+
# @param config [Config]
|
|
90
|
+
# @param on_behalf_of [String, nil] see {Client#on_behalf_of}
|
|
91
|
+
# @raise [ConfigurationError] when the connection cannot be built
|
|
92
|
+
def initialize(config, on_behalf_of: nil)
|
|
93
|
+
@config = config
|
|
94
|
+
@on_behalf_of = on_behalf_of
|
|
95
|
+
@connection = build_connection
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# @param identifier [String, nil] see {Client#on_behalf_of}
|
|
99
|
+
# @return [Transport] a copy that performs requests for that user
|
|
100
|
+
def with_on_behalf_of(identifier)
|
|
101
|
+
copy = dup
|
|
102
|
+
copy.instance_variable_set(:@on_behalf_of, identifier)
|
|
103
|
+
copy
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# @param config [Config]
|
|
107
|
+
# @return [Transport] a new transport for +config+, for the same user
|
|
108
|
+
def with_config(config) = self.class.new(config, on_behalf_of: on_behalf_of)
|
|
109
|
+
|
|
110
|
+
def inspect
|
|
111
|
+
"#<#{self.class.name} url=#{config.url.inspect} auth=#{config.authentication_scheme}#{" on_behalf_of=#{on_behalf_of.inspect}" if on_behalf_of}>"
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# Performs a request and raises on anything but a 2xx response.
|
|
115
|
+
#
|
|
116
|
+
# @param method [Symbol] +:get+, +:post+, +:put+ or +:delete+
|
|
117
|
+
# @param path [String] path relative to {Config#url}
|
|
118
|
+
# @param query [Hash, nil] query string parameters
|
|
119
|
+
# @param body [Hash, Array, String, nil] request payload; a Hash or Array
|
|
120
|
+
# is encoded as JSON, a String is sent as it is
|
|
121
|
+
# @param headers [Hash, nil] request headers
|
|
122
|
+
# @return [Response]
|
|
123
|
+
# @raise [ResponseError] for non-2xx responses
|
|
124
|
+
# @raise [TimeoutError] when the response took longer than {Config#timeout}
|
|
125
|
+
# @raise [ConnectionError] when the instance was unreachable, including
|
|
126
|
+
# a connection not opened within {Config#open_timeout}
|
|
127
|
+
def request(method, path, query: nil, body: nil, headers: nil)
|
|
128
|
+
path = Transport.relative_path(path)
|
|
129
|
+
request_line = "#{method.to_s.upcase} #{Transport.redact_path(path)}"
|
|
130
|
+
log_request(request_line, query, body, headers)
|
|
131
|
+
|
|
132
|
+
started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
133
|
+
response = decode(perform(method, path, query, body, headers))
|
|
134
|
+
log_response(request_line, response, started)
|
|
135
|
+
return response if response.success?
|
|
136
|
+
|
|
137
|
+
raise ResponseError.build(response, request_line)
|
|
138
|
+
rescue Faraday::TimeoutError => e
|
|
139
|
+
raise TimeoutError, "#{request_line} timed out (#{e.message})"
|
|
140
|
+
rescue Faraday::SSLError => e
|
|
141
|
+
raise ConnectionError, "#{request_line} failed: TLS handshake with #{config.url} failed (#{e.message})"
|
|
142
|
+
rescue Faraday::ConnectionFailed => e
|
|
143
|
+
raise ConnectionError, "#{request_line} failed: #{config.url} is unreachable (#{e.message})"
|
|
32
144
|
end
|
|
33
145
|
|
|
146
|
+
# The verb shorthands.
|
|
147
|
+
def get(path, **options) = request(:get, path, **options)
|
|
148
|
+
def post(path, **options) = request(:post, path, **options)
|
|
149
|
+
def put(path, **options) = request(:put, path, **options)
|
|
150
|
+
def delete(path, **options) = request(:delete, path, **options)
|
|
151
|
+
|
|
34
152
|
private
|
|
35
153
|
|
|
36
|
-
|
|
37
|
-
|
|
154
|
+
# The connection's :json middleware encodes a Hash or Array body.
|
|
155
|
+
def perform(method, path, query, body, headers)
|
|
156
|
+
@connection.public_send(method, path) do |request|
|
|
157
|
+
request.params.update(query) if query
|
|
158
|
+
headers&.each { |name, value| request.headers[name.to_s] = value.to_s }
|
|
159
|
+
request.headers['From'] = on_behalf_of if on_behalf_of
|
|
160
|
+
request.body = body if body
|
|
161
|
+
end
|
|
162
|
+
end
|
|
38
163
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
164
|
+
def build_connection
|
|
165
|
+
Faraday.new(
|
|
166
|
+
url: config.url,
|
|
167
|
+
ssl: { verify: config.ssl_verify },
|
|
168
|
+
proxy: config.proxy,
|
|
169
|
+
request: { timeout: config.timeout, open_timeout: config.open_timeout },
|
|
170
|
+
headers: { 'User-Agent' => config.user_agent, 'Accept' => 'application/json' }
|
|
171
|
+
) do |faraday|
|
|
172
|
+
apply_authentication(faraday)
|
|
173
|
+
faraday.request :json
|
|
174
|
+
faraday.request :retry, retry_options
|
|
175
|
+
faraday.adapter(config.adapter) if config.adapter
|
|
176
|
+
config.middleware&.call(faraday)
|
|
42
177
|
end
|
|
178
|
+
rescue Faraday::Error => e
|
|
179
|
+
raise ConfigurationError, "config could not be used to build a connection: #{e.message}"
|
|
180
|
+
end
|
|
43
181
|
|
|
44
|
-
|
|
45
|
-
|
|
182
|
+
def retry_options
|
|
183
|
+
{
|
|
184
|
+
max: config.retries,
|
|
185
|
+
interval: config.retry_interval,
|
|
186
|
+
max_interval: config.timeout,
|
|
187
|
+
interval_randomness: 0.5,
|
|
188
|
+
backoff_factor: 2,
|
|
189
|
+
retry_statuses: RETRIABLE_STATUSES,
|
|
190
|
+
methods: RETRIABLE_METHODS,
|
|
191
|
+
exceptions: RETRIABLE_EXCEPTIONS
|
|
192
|
+
}
|
|
193
|
+
end
|
|
46
194
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
195
|
+
def apply_authentication(faraday)
|
|
196
|
+
case config.authentication_scheme
|
|
197
|
+
when :http_token then faraday.request :authorization, 'Token', config.http_token
|
|
198
|
+
when :oauth2_token then faraday.request :authorization, 'Bearer', config.oauth2_token
|
|
199
|
+
else faraday.request :authorization, :basic, config.user, config.password
|
|
200
|
+
end
|
|
201
|
+
end
|
|
51
202
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
203
|
+
def decode(faraday_response)
|
|
204
|
+
headers = faraday_response.headers.to_h.to_h { |name, value| [name.to_s.downcase, value.to_s] }.freeze
|
|
205
|
+
raw_body = faraday_response.body.to_s
|
|
55
206
|
|
|
56
|
-
|
|
207
|
+
Response.new(
|
|
208
|
+
status: faraday_response.status,
|
|
209
|
+
headers: headers,
|
|
210
|
+
body: Response.decode_body(headers['content-type'], raw_body),
|
|
211
|
+
raw_body: raw_body
|
|
212
|
+
)
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
def log_request(request_line, query, body, headers)
|
|
216
|
+
logger.debug do
|
|
217
|
+
"Zammad API request: #{request_line}#{" query=#{Transport.redact(query).inspect}" if query}#{" headers=#{Transport.redact(headers).inspect}" if headers}"
|
|
57
218
|
end
|
|
219
|
+
logger.debug { "Zammad API payload: #{loggable_body(body)}" } if body
|
|
220
|
+
end
|
|
58
221
|
|
|
59
|
-
|
|
60
|
-
|
|
222
|
+
def log_response(request_line, response, started)
|
|
223
|
+
duration = ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - started) * 1000).round
|
|
224
|
+
logger.debug { "Zammad API response: #{request_line} -> #{response.status} in #{duration}ms" }
|
|
61
225
|
end
|
|
226
|
+
|
|
227
|
+
def loggable_body(body)
|
|
228
|
+
return Transport.redact(body).inspect if body.is_a?(Hash) || body.is_a?(Array)
|
|
229
|
+
|
|
230
|
+
"[#{body.to_s.bytesize} bytes, not logged]"
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
def logger = config.logger
|
|
62
234
|
end
|
|
63
235
|
end
|
data/lib/zammad_api/version.rb
CHANGED
data/lib/zammad_api.rb
CHANGED
|
@@ -1,6 +1,19 @@
|
|
|
1
|
-
|
|
2
|
-
require 'zammad_api/errors'
|
|
3
|
-
require 'zammad_api/client'
|
|
1
|
+
# frozen_string_literal: true
|
|
4
2
|
|
|
3
|
+
require_relative 'zammad_api/version'
|
|
4
|
+
require_relative 'zammad_api/errors'
|
|
5
|
+
require_relative 'zammad_api/deep_copy'
|
|
6
|
+
require_relative 'zammad_api/config'
|
|
7
|
+
require_relative 'zammad_api/response'
|
|
8
|
+
require_relative 'zammad_api/transport'
|
|
9
|
+
require_relative 'zammad_api/attribute_access'
|
|
10
|
+
require_relative 'zammad_api/collection'
|
|
11
|
+
require_relative 'zammad_api/resources'
|
|
12
|
+
require_relative 'zammad_api/resource_proxy'
|
|
13
|
+
require_relative 'zammad_api/client'
|
|
14
|
+
|
|
15
|
+
# Ruby client for the Zammad API v1.0.
|
|
16
|
+
#
|
|
17
|
+
# @see Client
|
|
5
18
|
module ZammadAPI
|
|
6
19
|
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
module ZammadAPI
|
|
2
|
+
module AttributeAccess
|
|
3
|
+
# private_constant in the source; RBS has no way to say so.
|
|
4
|
+
ATTRIBUTE_WRITER: Regexp
|
|
5
|
+
|
|
6
|
+
@attributes: Hash[Symbol, untyped]
|
|
7
|
+
|
|
8
|
+
attr_reader attributes: Hash[Symbol, untyped]
|
|
9
|
+
|
|
10
|
+
def []: (Symbol | String key) -> untyped
|
|
11
|
+
def []=: (Symbol | String key, untyped value) -> untyped
|
|
12
|
+
def fetch: (Symbol | String key, *untyped default) ?{ (Symbol) -> untyped } -> untyped
|
|
13
|
+
def key?: (Symbol | String key) -> bool
|
|
14
|
+
def to_h: () -> Hash[Symbol, untyped]
|
|
15
|
+
def id: () -> Integer?
|
|
16
|
+
def deconstruct_keys: (Array[Symbol]?) -> Hash[Symbol, untyped]
|
|
17
|
+
def ==: (untyped other) -> bool
|
|
18
|
+
def eql?: (untyped other) -> bool
|
|
19
|
+
def hash: () -> Integer
|
|
20
|
+
def as_json: (*untyped) -> Hash[Symbol, untyped]
|
|
21
|
+
def to_json: (*untyped) -> String
|
|
22
|
+
def method_missing: (Symbol, *untyped) -> untyped
|
|
23
|
+
def respond_to_missing?: (Symbol, ?bool) -> bool
|
|
24
|
+
|
|
25
|
+
private
|
|
26
|
+
|
|
27
|
+
def writable_attributes?: () -> bool
|
|
28
|
+
def write_attribute: (Symbol, untyped) -> untyped
|
|
29
|
+
def frozen_attributes: (untyped) -> untyped
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
module ZammadAPI
|
|
2
|
+
class Client
|
|
3
|
+
RESOURCES: Hash[Symbol, untyped]
|
|
4
|
+
ENV_OPTIONS: Hash[String, Symbol]
|
|
5
|
+
|
|
6
|
+
@transport: Transport
|
|
7
|
+
@resources: Hash[untyped, ResourceProxy[untyped]]
|
|
8
|
+
|
|
9
|
+
def self.from_env: (**untyped overrides) -> Client
|
|
10
|
+
|
|
11
|
+
def initialize: (?transport: Transport?, **untyped options) -> void
|
|
12
|
+
def config: () -> Config
|
|
13
|
+
|
|
14
|
+
def groups: () -> ResourceProxy[Resources::Group]
|
|
15
|
+
def organizations: () -> ResourceProxy[Resources::Organization]
|
|
16
|
+
def tickets: () -> ResourceProxy[Resources::Ticket]
|
|
17
|
+
def ticket_articles: () -> ResourceProxy[Resources::TicketArticle]
|
|
18
|
+
def ticket_priorities: () -> ResourceProxy[Resources::TicketPriority]
|
|
19
|
+
def ticket_states: () -> ResourceProxy[Resources::TicketState]
|
|
20
|
+
def users: () -> ResourceProxy[Resources::User]
|
|
21
|
+
|
|
22
|
+
# A class cannot carry its own type into the answer - RBS has no way to
|
|
23
|
+
# say "a proxy for whichever class this is" - so a proxy for a resource of
|
|
24
|
+
# your own is untyped until you say what it is:
|
|
25
|
+
#
|
|
26
|
+
# roles = client.resource(Role) #: ZammadAPI::ResourceProxy[Role]
|
|
27
|
+
def resource: (Symbol | String | Class name) -> ResourceProxy[untyped]
|
|
28
|
+
def resource_names: () -> Array[Symbol]
|
|
29
|
+
def me: () -> Resources::User
|
|
30
|
+
def version: () -> String?
|
|
31
|
+
|
|
32
|
+
def get: (String path, ?query: Hash[untyped, untyped]?, ?headers: Hash[untyped, untyped]?) -> Response
|
|
33
|
+
def post: (String path, ?query: Hash[untyped, untyped]?, ?body: untyped, ?headers: Hash[untyped, untyped]?) -> Response
|
|
34
|
+
def put: (String path, ?query: Hash[untyped, untyped]?, ?body: untyped, ?headers: Hash[untyped, untyped]?) -> Response
|
|
35
|
+
def delete: (String path, ?query: Hash[untyped, untyped]?, ?headers: Hash[untyped, untyped]?) -> Response
|
|
36
|
+
|
|
37
|
+
def with: (**untyped options) -> Client
|
|
38
|
+
def on_behalf_of: (String | Integer identifier) -> Client
|
|
39
|
+
| [T] (String | Integer identifier) { (Client) -> T } -> T
|
|
40
|
+
def inspect: () -> String
|
|
41
|
+
end
|
|
42
|
+
end
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
module ZammadAPI
|
|
2
|
+
# Generic over the resource it holds, so that what a walk yields is a
|
|
3
|
+
# Resources::Ticket rather than something untyped: `client.tickets.all` is a
|
|
4
|
+
# Collection[Resources::Ticket].
|
|
5
|
+
class Collection[R < Resources::Base]
|
|
6
|
+
include Enumerable[R]
|
|
7
|
+
|
|
8
|
+
SEARCH_MAX_PER_PAGE: Integer
|
|
9
|
+
|
|
10
|
+
@transport: Transport
|
|
11
|
+
@resource_class: untyped
|
|
12
|
+
@path: String
|
|
13
|
+
@query: Hash[Symbol, untyped]
|
|
14
|
+
@per_page: Integer
|
|
15
|
+
@page: Integer?
|
|
16
|
+
@search: bool
|
|
17
|
+
|
|
18
|
+
def initialize: (Transport transport, untyped resource_class, ?search: bool, ?query: Hash[Symbol, untyped], ?per_page: Integer?, ?page: Integer?) -> void
|
|
19
|
+
|
|
20
|
+
def each: () { (R) -> void } -> Collection[R]
|
|
21
|
+
| () -> Enumerator[R, Collection[R]]
|
|
22
|
+
def find_each: (?batch_size: Integer?) { (R) -> void } -> Collection[R]
|
|
23
|
+
| (?batch_size: Integer?) -> Enumerator[R, Collection[R]]
|
|
24
|
+
def in_batches: (?of: Integer?) { (Array[R]) -> void } -> Collection[R]
|
|
25
|
+
| (?of: Integer?) -> Enumerator[Array[R], Collection[R]]
|
|
26
|
+
def page: (Integer number, ?per_page: Integer?) -> Collection[R]
|
|
27
|
+
def first: () -> R?
|
|
28
|
+
| (Integer count) -> Array[R]
|
|
29
|
+
def find: (?untyped ifnone) { (R) -> boolish } -> R?
|
|
30
|
+
| (*untyped) -> untyped
|
|
31
|
+
def order: (*(Symbol | String) attributes, **(Symbol | String) directions) -> Collection[R]
|
|
32
|
+
def pluck: (*(Symbol | String) keys) -> Array[untyped]
|
|
33
|
+
def count: (*untyped) ?{ (R) -> boolish } -> Integer
|
|
34
|
+
def empty?: () -> bool
|
|
35
|
+
def inspect: () -> String
|
|
36
|
+
|
|
37
|
+
private
|
|
38
|
+
|
|
39
|
+
def walk: () { (Array[R]) -> void } -> void
|
|
40
|
+
def fetch: (Integer number) -> Array[R]
|
|
41
|
+
def repeated_page_message: (Integer number) -> String
|
|
42
|
+
def total_count: () -> Integer?
|
|
43
|
+
def sorted_by: () -> Hash[String, String]
|
|
44
|
+
def with: (?page: Integer?, ?per_page: Integer?, ?query: Hash[Symbol, untyped]) -> Collection[R]
|
|
45
|
+
end
|
|
46
|
+
end
|