kerio-api 0.1.1 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/kerio-api/client.rb +5 -10
- data/lib/kerio-api/method/generic.rb +48 -0
- data/lib/kerio-api/method/session/login.rb +17 -0
- data/lib/kerio-api/method/session/logout.rb +16 -0
- data/lib/kerio-api/method/upload.rb +18 -0
- data/lib/kerio-api/session.rb +40 -14
- metadata +17 -15
- data/lib/kerio-api/interface/generic.rb +0 -16
- data/lib/kerio-api/interface/session.rb +0 -21
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f69373b20fb3918fec7293e08e4313713d522494
|
4
|
+
data.tar.gz: 810d75d6bb149cce9f7309718493288fdf76c040
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b9d2ad2390a67da3333e1382b121d726d0ef36fa712ddfc56f6b1e1445258fec0aeaa50acb1946d256073fee1ca3d6625ec27803580dbc8f6ccaec3b4aed0c0d
|
7
|
+
data.tar.gz: 7cd701832de49f9aa31823239f392a22648d599f4caeb72e70254582289110d259a1f4b00e5b85680d7f3fff8f00f0c47545b9dcfd4d7eacd73cd209c300363d
|
data/lib/kerio-api/client.rb
CHANGED
@@ -1,23 +1,18 @@
|
|
1
1
|
require 'kerio-api/session'
|
2
|
-
require 'kerio-api/
|
3
|
-
require 'kerio-api/
|
2
|
+
require 'kerio-api/chainable_method'
|
3
|
+
require 'kerio-api/method/generic'
|
4
4
|
|
5
5
|
module Kerio
|
6
6
|
module Api
|
7
7
|
class Client
|
8
|
-
|
8
|
+
include Kerio::Api::ChainableMethod
|
9
9
|
|
10
|
+
def initialize (params)
|
10
11
|
@session = Kerio::Api::Session.new(params[:url])
|
11
12
|
end
|
12
13
|
|
13
14
|
def method_missing(method, *args, &block)
|
14
|
-
|
15
|
-
# prefer special implementation over the generic one
|
16
|
-
return Kerio::Api::Interface.const_get(method).new(method, @session)
|
17
|
-
|
18
|
-
rescue NameError
|
19
|
-
|
20
|
-
return Kerio::Api::Interface::Generic.new(method, @session)
|
15
|
+
return next_method(names: [method], session: @session, args: args)
|
21
16
|
end
|
22
17
|
end
|
23
18
|
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module Kerio
|
2
|
+
module Api
|
3
|
+
module Method
|
4
|
+
class Generic
|
5
|
+
include Kerio::Api::ChainableMethod
|
6
|
+
|
7
|
+
def initialize (params)
|
8
|
+
|
9
|
+
@names = params[:names]
|
10
|
+
|
11
|
+
@session = params[:session]
|
12
|
+
|
13
|
+
@args = params[:args]
|
14
|
+
|
15
|
+
__invoke_method if @args.count > 0
|
16
|
+
end
|
17
|
+
|
18
|
+
def __invoke_method
|
19
|
+
if @resp.nil?
|
20
|
+
name = @names.join('.')
|
21
|
+
@resp = @session.json_method(name, @args[0])
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def __result
|
26
|
+
__invoke_method
|
27
|
+
return @resp['result']
|
28
|
+
end
|
29
|
+
|
30
|
+
def [](index)
|
31
|
+
return __result[index]
|
32
|
+
end
|
33
|
+
|
34
|
+
def pretty_print pp
|
35
|
+
pp __result
|
36
|
+
end
|
37
|
+
|
38
|
+
def method_missing(method, *args, &block)
|
39
|
+
|
40
|
+
@names.push(method)
|
41
|
+
|
42
|
+
return next_method(names: @names, session: @session, args: args)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Kerio
|
2
|
+
module Api
|
3
|
+
module Method
|
4
|
+
module Session
|
5
|
+
class Login < Kerio::Api::Method::Generic
|
6
|
+
|
7
|
+
# we need special implementation of login method to remember the token
|
8
|
+
def __invoke_method
|
9
|
+
|
10
|
+
super
|
11
|
+
@session.token = @resp['result']['token']
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module Kerio
|
4
|
+
module Api
|
5
|
+
module Method
|
6
|
+
class Upload < Kerio::Api::Method::Generic
|
7
|
+
|
8
|
+
def __invoke_method
|
9
|
+
if @resp.nil?
|
10
|
+
raw = @session.upload_file(File.new(@args[0]))
|
11
|
+
|
12
|
+
@resp = JSON.parse(raw)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/kerio-api/session.rb
CHANGED
@@ -1,11 +1,10 @@
|
|
1
1
|
require 'json'
|
2
|
-
require '
|
2
|
+
require 'httmultiparty'
|
3
3
|
require 'kerio-api/error'
|
4
4
|
|
5
5
|
module Kerio
|
6
6
|
module Api
|
7
7
|
class Session
|
8
|
-
|
9
8
|
attr_writer :token
|
10
9
|
|
11
10
|
def initialize(url)
|
@@ -15,33 +14,60 @@ module Kerio
|
|
15
14
|
@cookie = nil
|
16
15
|
end
|
17
16
|
|
18
|
-
def
|
17
|
+
def headers
|
18
|
+
headers = {
|
19
|
+
'Accept' => 'application/json-rpc',
|
20
|
+
}
|
21
|
+
headers['X-Token'] = @token if not @token.nil?
|
22
|
+
headers['Cookie'] = @cookie if not @cookie.nil?
|
23
|
+
|
24
|
+
headers
|
25
|
+
end
|
26
|
+
|
27
|
+
def process_json_response(resp)
|
28
|
+
@cookie = resp.headers['Set-Cookie'] if not resp.headers['Set-Cookie'].nil?
|
29
|
+
|
30
|
+
raise Kerio::Api::Error.new(resp, headers) if ((not resp["error"].nil?) || (resp.code != 200))
|
31
|
+
end
|
32
|
+
|
33
|
+
def json_method(name, params)
|
34
|
+
|
19
35
|
body = {
|
20
36
|
jsonrpc: '2.0',
|
21
37
|
id: Kernel.rand(10**12),
|
22
|
-
method:
|
38
|
+
method: name,
|
23
39
|
params: params,
|
24
40
|
}
|
25
41
|
body['token'] = @token if not @token.nil?
|
26
42
|
|
27
|
-
|
28
|
-
'Accept' => 'application/json-rpc',
|
29
|
-
}
|
30
|
-
headers['X-Token'] = @token if not @token.nil?
|
31
|
-
headers['Cookie'] = @cookie if not @cookie.nil?
|
32
|
-
|
33
|
-
resp = HTTParty.post(
|
43
|
+
resp = HTTMultiParty.post(
|
34
44
|
@url.to_s,
|
35
45
|
body: JSON.generate(body),
|
36
46
|
headers: headers,
|
37
47
|
verify: false,
|
38
48
|
follow_redirects: true,
|
39
49
|
)
|
40
|
-
@cookie = resp.headers['Set-Cookie'] if not resp.headers['Set-Cookie'].nil?
|
41
50
|
|
42
|
-
|
51
|
+
process_json_response(resp)
|
52
|
+
return resp
|
53
|
+
end
|
54
|
+
|
55
|
+
def upload_file(file)
|
56
|
+
h = headers
|
57
|
+
h['Accept'] = '*/*'
|
58
|
+
h['Content-Type'] = 'multipart/form-data';
|
59
|
+
|
60
|
+
resp = HTTMultiParty.post(
|
61
|
+
@url.to_s + '/upload',
|
62
|
+
headers: h,
|
63
|
+
verify: false,
|
64
|
+
query: {
|
65
|
+
'newFile.bin': file,
|
66
|
+
}
|
67
|
+
)
|
43
68
|
|
44
|
-
|
69
|
+
process_json_response(resp)
|
70
|
+
return resp
|
45
71
|
end
|
46
72
|
end
|
47
73
|
end
|
metadata
CHANGED
@@ -1,85 +1,85 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kerio-api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Petr Baloun
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-12-
|
11
|
+
date: 2016-12-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: httmultiparty
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 0.3.16
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 0.3.16
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: json
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: 2.0.2
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
40
|
+
version: 2.0.2
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '3'
|
47
|
+
version: '3.5'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '3'
|
54
|
+
version: '3.5'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: webmock
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version:
|
61
|
+
version: 2.3.1
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version:
|
68
|
+
version: 2.3.1
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: bundler
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
73
|
- - "~>"
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: '
|
75
|
+
version: '1.12'
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version: '
|
82
|
+
version: '1.12'
|
83
83
|
description: This gem allows simple access to administration API of Kerio products
|
84
84
|
email: balounp@gmail.com
|
85
85
|
executables: []
|
@@ -89,8 +89,10 @@ files:
|
|
89
89
|
- lib/kerio-api.rb
|
90
90
|
- lib/kerio-api/client.rb
|
91
91
|
- lib/kerio-api/error.rb
|
92
|
-
- lib/kerio-api/
|
93
|
-
- lib/kerio-api/
|
92
|
+
- lib/kerio-api/method/generic.rb
|
93
|
+
- lib/kerio-api/method/session/login.rb
|
94
|
+
- lib/kerio-api/method/session/logout.rb
|
95
|
+
- lib/kerio-api/method/upload.rb
|
94
96
|
- lib/kerio-api/session.rb
|
95
97
|
homepage: https://github.com/balous/ruby-kerio-api
|
96
98
|
licenses:
|
@@ -1,16 +0,0 @@
|
|
1
|
-
module Kerio
|
2
|
-
module Api
|
3
|
-
module Interface
|
4
|
-
class Generic
|
5
|
-
def initialize (name, session)
|
6
|
-
@name = name
|
7
|
-
@session = session
|
8
|
-
end
|
9
|
-
|
10
|
-
def method_missing(method, *args, &block)
|
11
|
-
return @session.request("#{@name}.#{method}", args[0])
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
@@ -1,21 +0,0 @@
|
|
1
|
-
require 'kerio-api/interface/generic'
|
2
|
-
|
3
|
-
module Kerio
|
4
|
-
module Api
|
5
|
-
module Interface
|
6
|
-
class Session < Kerio::Api::Interface::Generic
|
7
|
-
|
8
|
-
# we need special implementation of login method to remember the token
|
9
|
-
def login(params)
|
10
|
-
|
11
|
-
result = @session.request(
|
12
|
-
'Session.login',
|
13
|
-
params,
|
14
|
-
)
|
15
|
-
|
16
|
-
@session.token = result['token']
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|