zammad_api 1.3.1 → 1.4.0
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/lib/zammad_api/client.rb +5 -5
- data/lib/zammad_api/errors.rb +62 -0
- data/lib/zammad_api/list_base.rb +4 -3
- data/lib/zammad_api/resources/base.rb +12 -18
- data/lib/zammad_api/resources/ticket.rb +3 -2
- data/lib/zammad_api/resources/ticket_article_attachment.rb +1 -2
- data/lib/zammad_api/transport.rb +1 -1
- data/lib/zammad_api/version.rb +1 -1
- data/lib/zammad_api.rb +1 -0
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 60178628e519723ca90e772769e9b61e615d152bdb825210b35e8233b9e15a16
|
|
4
|
+
data.tar.gz: 92fe4e55cf8230790ce87735120f39934ac9313064f67983e052d7b2fde6126f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: cb06e3507b9598274739a30aea5e109e4965444f521b925c2c0b949929b786a2cd80b20dbce2ac29323614ac48a1d8bb008b993ae75cc31f7733e63adf4e7b24
|
|
7
|
+
data.tar.gz: 7bfe654612c6be190f5270b7fb311986e566458d4b6529683347b89d4a39718751702a61ee71904c1deec0d169278d274febe3d8ec62bcbdd8c31cc34122ebbe
|
data/lib/zammad_api/client.rb
CHANGED
|
@@ -31,7 +31,7 @@ module ZammadAPI
|
|
|
31
31
|
begin
|
|
32
32
|
class_object = Kernel.const_get(class_name)
|
|
33
33
|
rescue
|
|
34
|
-
raise "Resource for #{method} does not exist"
|
|
34
|
+
raise ResourceNotFoundError, "Resource for #{method} does not exist"
|
|
35
35
|
end
|
|
36
36
|
ZammadAPI::Dispatcher.new(@transport, class_object)
|
|
37
37
|
end
|
|
@@ -39,20 +39,20 @@ module ZammadAPI
|
|
|
39
39
|
private
|
|
40
40
|
|
|
41
41
|
def check_config
|
|
42
|
-
raise 'missing url in config' if !@config[:url]
|
|
43
|
-
raise 'config url needs to start with http:// or https://' if !%r{^(http|https)://}.match?(@config[:url])
|
|
42
|
+
raise ConfigurationError, 'missing url in config' if !@config[:url]
|
|
43
|
+
raise ConfigurationError, 'config url needs to start with http:// or https://' if !%r{^(http|https)://}.match?(@config[:url])
|
|
44
44
|
|
|
45
45
|
# check for token auth
|
|
46
46
|
return if @config[:http_token] && !@config[:http_token].empty?
|
|
47
47
|
return if @config[:oauth2_token] && !@config[:oauth2_token].empty?
|
|
48
48
|
|
|
49
49
|
if !@config[:user] || @config[:user].empty?
|
|
50
|
-
raise 'missing user in config'
|
|
50
|
+
raise ConfigurationError, 'missing user in config'
|
|
51
51
|
end
|
|
52
52
|
|
|
53
53
|
return if @config[:password] && !@config[:password].empty?
|
|
54
54
|
|
|
55
|
-
raise 'missing password in config'
|
|
55
|
+
raise ConfigurationError, 'missing password in config'
|
|
56
56
|
end
|
|
57
57
|
|
|
58
58
|
def modulize(string)
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
require 'json'
|
|
2
|
+
|
|
3
|
+
module ZammadAPI
|
|
4
|
+
class Error < RuntimeError; end
|
|
5
|
+
|
|
6
|
+
class ConfigurationError < Error; end
|
|
7
|
+
|
|
8
|
+
class ResourceNotFoundError < Error; end
|
|
9
|
+
|
|
10
|
+
class ResponseError < Error
|
|
11
|
+
attr_reader :response, :operation, :resource_class
|
|
12
|
+
|
|
13
|
+
def initialize(operation:, response:, resource_class: nil)
|
|
14
|
+
@operation = operation
|
|
15
|
+
@response = response
|
|
16
|
+
@resource_class = resource_class
|
|
17
|
+
super(default_message)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def status
|
|
21
|
+
response&.status
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def body
|
|
25
|
+
response&.body
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def self.from(response, **kwargs)
|
|
29
|
+
klass = if response.nil?
|
|
30
|
+
self
|
|
31
|
+
elsif response.status >= 500
|
|
32
|
+
ServerError
|
|
33
|
+
else
|
|
34
|
+
ClientError
|
|
35
|
+
end
|
|
36
|
+
klass.new(response: response, **kwargs)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
private
|
|
40
|
+
|
|
41
|
+
def default_message
|
|
42
|
+
subject = resource_class ? "#{operation} (#{resource_class.name})" : operation
|
|
43
|
+
"Can't #{subject}: #{detail}"
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def detail
|
|
47
|
+
body_error || "HTTP #{status}"
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def body_error
|
|
51
|
+
return nil if body.to_s.strip.empty?
|
|
52
|
+
|
|
53
|
+
parsed = JSON.parse(body)
|
|
54
|
+
parsed.is_a?(Hash) ? parsed['error'] : nil
|
|
55
|
+
rescue JSON::ParserError
|
|
56
|
+
nil
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
class ClientError < ResponseError; end
|
|
61
|
+
class ServerError < ResponseError; end
|
|
62
|
+
end
|
data/lib/zammad_api/list_base.rb
CHANGED
|
@@ -62,11 +62,12 @@ module ZammadAPI
|
|
|
62
62
|
}.join('&')
|
|
63
63
|
|
|
64
64
|
response = @transport.get(url: url)
|
|
65
|
-
data = safe_json_parse(response.body)
|
|
66
65
|
if response.status != 200
|
|
67
|
-
raise "
|
|
66
|
+
raise ResponseError.from(response, operation: "get .#{request} of object", resource_class: @resource.class)
|
|
68
67
|
end
|
|
69
68
|
|
|
69
|
+
data = safe_json_parse(response.body)
|
|
70
|
+
|
|
70
71
|
list = []
|
|
71
72
|
data.each do |local_data|
|
|
72
73
|
item = @resource.new(@transport, local_data)
|
|
@@ -77,7 +78,7 @@ module ZammadAPI
|
|
|
77
78
|
end
|
|
78
79
|
|
|
79
80
|
def perform_request(_parameter)
|
|
80
|
-
raise "no perform_request implementation for #{self.class.name} found"
|
|
81
|
+
raise Error, "no perform_request implementation for #{self.class.name} found"
|
|
81
82
|
end
|
|
82
83
|
end
|
|
83
84
|
end
|
|
@@ -38,17 +38,14 @@ module ZammadAPI
|
|
|
38
38
|
end
|
|
39
39
|
|
|
40
40
|
def changed?
|
|
41
|
-
|
|
41
|
+
!@changes.to_h.empty?
|
|
42
42
|
end
|
|
43
43
|
|
|
44
44
|
def destroy
|
|
45
45
|
response = @transport.delete(url: "#{@url}/#{@attributes[:id]}")
|
|
46
|
-
if response.body.to_s != '' && response.body.to_s != ' '
|
|
47
|
-
data = safe_json_parse(response.body)
|
|
48
|
-
end
|
|
49
46
|
return true if response.status == 200
|
|
50
47
|
|
|
51
|
-
raise
|
|
48
|
+
raise ResponseError.from(response, operation: 'destroy object', resource_class: self.class)
|
|
52
49
|
end
|
|
53
50
|
|
|
54
51
|
def save
|
|
@@ -79,11 +76,11 @@ module ZammadAPI
|
|
|
79
76
|
|
|
80
77
|
def self.find(transport, id)
|
|
81
78
|
response = transport.get(url: "#{@url}/#{id}?expand=true")
|
|
82
|
-
data = safe_json_parse(response.body)
|
|
83
79
|
if response.status != 200
|
|
84
|
-
raise
|
|
80
|
+
raise ResponseError.from(response, operation: 'find object', resource_class: self)
|
|
85
81
|
end
|
|
86
82
|
|
|
83
|
+
data = safe_json_parse(response.body)
|
|
87
84
|
item = new(transport, data)
|
|
88
85
|
item.new_instance = false
|
|
89
86
|
item
|
|
@@ -110,11 +107,10 @@ module ZammadAPI
|
|
|
110
107
|
end
|
|
111
108
|
|
|
112
109
|
def save_new
|
|
113
|
-
response
|
|
114
|
-
|
|
115
|
-
return attributes if response.status == 201
|
|
110
|
+
response = @transport.post(url: "#{@url}?expand=true", params: @attributes)
|
|
111
|
+
return safe_json_parse(response.body) if response.status == 201
|
|
116
112
|
|
|
117
|
-
save_error(
|
|
113
|
+
save_error(response)
|
|
118
114
|
end
|
|
119
115
|
|
|
120
116
|
def save_existing
|
|
@@ -122,16 +118,14 @@ module ZammadAPI
|
|
|
122
118
|
@changes.each do |name, values|
|
|
123
119
|
attributes_to_post[name] = values[1]
|
|
124
120
|
end
|
|
125
|
-
response
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
return attributes if response.status == 200
|
|
121
|
+
response = @transport.put(url: "#{@url}/#{@attributes[:id]}?expand=true", params: attributes_to_post)
|
|
122
|
+
return safe_json_parse(response.body) if response.status == 200
|
|
129
123
|
|
|
130
|
-
save_error(
|
|
124
|
+
save_error(response)
|
|
131
125
|
end
|
|
132
126
|
|
|
133
|
-
def save_error(
|
|
134
|
-
raise
|
|
127
|
+
def save_error(response)
|
|
128
|
+
raise ResponseError.from(response, operation: 'save object', resource_class: self.class)
|
|
135
129
|
end
|
|
136
130
|
|
|
137
131
|
def symbolize_keys_deep!(hash)
|
|
@@ -3,11 +3,12 @@ class ZammadAPI::Resources::Ticket < ZammadAPI::Resources::Base
|
|
|
3
3
|
|
|
4
4
|
def articles
|
|
5
5
|
response = @transport.get(url: "/api/v1/ticket_articles/by_ticket/#{id}?expand=true")
|
|
6
|
-
data = safe_json_parse(response.body)
|
|
7
6
|
if response.status != 200
|
|
8
|
-
raise
|
|
7
|
+
raise ZammadAPI::ResponseError.from(response, operation: 'get articles', resource_class: self.class)
|
|
9
8
|
end
|
|
10
9
|
|
|
10
|
+
data = safe_json_parse(response.body)
|
|
11
|
+
|
|
11
12
|
data.collect do |raw|
|
|
12
13
|
item = ZammadAPI::Resources::TicketArticle.new(@transport, raw)
|
|
13
14
|
item.new_instance = false
|
|
@@ -13,7 +13,6 @@ class ZammadAPI::Resources::TicketArticleAttachment < ZammadAPI::Resources::Base
|
|
|
13
13
|
response = @transport.get(url: "/api/v1/ticket_attachment/#{ticket_id}/#{article_id}/#{id}")
|
|
14
14
|
return response.body if response.status == 200
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
raise "Can't get articles (#{self.class.name}): #{data['error']}"
|
|
16
|
+
raise ZammadAPI::ResponseError.from(response, operation: 'get articles', resource_class: self.class)
|
|
18
17
|
end
|
|
19
18
|
end
|
data/lib/zammad_api/transport.rb
CHANGED
data/lib/zammad_api/version.rb
CHANGED
data/lib/zammad_api.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: zammad_api
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.4.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Martin Edenhofer
|
|
@@ -34,6 +34,7 @@ files:
|
|
|
34
34
|
- lib/zammad_api.rb
|
|
35
35
|
- lib/zammad_api/client.rb
|
|
36
36
|
- lib/zammad_api/dispatcher.rb
|
|
37
|
+
- lib/zammad_api/errors.rb
|
|
37
38
|
- lib/zammad_api/json_helper.rb
|
|
38
39
|
- lib/zammad_api/list_all.rb
|
|
39
40
|
- lib/zammad_api/list_base.rb
|