qtc-sdk 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,38 @@
1
+ require_relative 'base'
2
+
3
+ module Qtc
4
+ module Cli
5
+ class Mar::Domains < Mar::Base
6
+
7
+ def list(options)
8
+ instance_id = resolve_instance_id(options)
9
+ instance_data = instance_info(instance_id)
10
+ if instance_data
11
+ token = instance_data['authorizations'][0]['access_token']
12
+ result = client.get("/apps/#{instance_id}/domains", nil, {'Authorization' => "Bearer #{token}"})
13
+ result['results'].each do |r|
14
+ print color("* #{r['name']}", :bold)
15
+ end
16
+ end
17
+ end
18
+
19
+ def create(name, options)
20
+ instance_id = resolve_instance_id(options)
21
+ instance_data = instance_info(instance_id)
22
+ if instance_data
23
+ token = instance_data['authorizations'][0]['access_token']
24
+ client.post("/apps/#{instance_id}/domains", {name: name}, {}, {'Authorization' => "Bearer #{token}"})
25
+ end
26
+ end
27
+
28
+ def destroy(name, options)
29
+ instance_id = resolve_instance_id(options)
30
+ instance_data = instance_info(instance_id)
31
+ if instance_data
32
+ token = instance_data['authorizations'][0]['access_token']
33
+ client.delete("/apps/#{instance_id}/domains/#{name}", nil, nil, {'Authorization' => "Bearer #{token}"})
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,40 @@
1
+ require_relative 'base'
2
+
3
+ module Qtc
4
+ module Cli
5
+ class Mar::Env < Mar::Base
6
+
7
+ def set(vars, options)
8
+ instance_id = resolve_instance_id(options)
9
+ env_vars = {}
10
+ vars.each do |type|
11
+ arr = type.strip.split("=")
12
+ if arr[0]
13
+ if arr[1].nil? || arr[1] == ""
14
+ env_vars[arr[0]] = nil
15
+ else
16
+ env_vars[arr[0]] = arr[1..-1].join("=")
17
+ end
18
+ end
19
+ end
20
+ instance_data = instance_info(instance_id)
21
+ if instance_data
22
+ token = instance_data['authorizations'][0]['access_token']
23
+ client.put("/apps/#{instance_id}/env_vars", env_vars, {}, {'Authorization' => "Bearer #{token}"})
24
+ end
25
+ end
26
+
27
+ def show(options)
28
+ instance_id = resolve_instance_id(options)
29
+ instance_data = instance_info(instance_id)
30
+ if instance_data
31
+ token = instance_data['authorizations'][0]['access_token']
32
+ env_vars = client.get("/apps/#{instance_id}/env_vars", {}, {'Authorization' => "Bearer #{token}"})
33
+ env_vars.each do |key, value|
34
+ puts "#{key}=#{value}"
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,36 @@
1
+ require 'base64'
2
+ require_relative 'base'
3
+
4
+ module Qtc
5
+ module Cli
6
+ class Mar::SslCertificates < Mar::Base
7
+
8
+
9
+ def create(options)
10
+ raise ArgumentError.new("--key=#{opts.key} is not a file") unless File.exists(opts.key)
11
+ raise ArgumentError.new("--cert=#{opts.cert} is not a file") unless File.exists(opts.cert)
12
+
13
+ instance_id = resolve_instance_id(options)
14
+ instance_data = instance_info(instance_id)
15
+ if instance_data
16
+ token = instance_data['authorizations'][0]['access_token']
17
+ data = {
18
+ name: instance_id,
19
+ privateKey: File.read(opts.key),
20
+ certificateBody: File.read(opts.cert)
21
+ }
22
+ client.post("/apps/#{instance_id}/ssl_certificate", data, {}, {'Authorization' => "Bearer #{token}"})
23
+ end
24
+ end
25
+
26
+ def destroy(options)
27
+ instance_id = resolve_instance_id(options)
28
+ instance_data = instance_info(instance_id)
29
+ if instance_data
30
+ token = instance_data['authorizations'][0]['access_token']
31
+ client.delete("/apps/#{instance_id}/ssl_certificate", {}, {}, {'Authorization' => "Bearer #{token}"})
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
data/lib/qtc/client.rb ADDED
@@ -0,0 +1,170 @@
1
+ require 'json'
2
+ require 'httpclient'
3
+ require_relative 'errors'
4
+
5
+ module Qtc
6
+ class Client
7
+
8
+ attr_accessor :default_headers
9
+ attr_reader :http_client
10
+
11
+ # Initialize api client
12
+ #
13
+ # @param [String] api_url
14
+ # @param [Hash] default_headers
15
+ def initialize(api_url, default_headers = {})
16
+ @http_client = HTTPClient.new
17
+ @default_headers = {'Accept' => 'application/json', 'Content-Type' => 'application/json'}.merge(default_headers)
18
+ @api_url = api_url
19
+ end
20
+
21
+ # Get request
22
+ #
23
+ # @param [String] path
24
+ # @param [Hash,NilClass] params
25
+ # @return [Hash]
26
+ def get(path, params = nil, headers = {})
27
+ response = http_client.get(request_uri(path), params, request_headers(headers))
28
+ if response.status == 200
29
+ parse_response(response)
30
+ else
31
+ handle_error_response(response)
32
+ end
33
+ end
34
+
35
+ # Post request
36
+ #
37
+ # @param [String] path
38
+ # @param [Object] obj
39
+ # @param [Hash] params
40
+ # @param [Hash] headers
41
+ # @return [Hash]
42
+ def post(path, obj, params = {}, headers = {})
43
+ request_headers = request_headers(headers)
44
+ request_options = {
45
+ header: request_headers,
46
+ body: encode_body(obj, request_headers['Content-Type']),
47
+ query: params
48
+ }
49
+ response = http_client.post(request_uri(path), request_options)
50
+ if [200, 201].include?(response.status)
51
+ parse_response(response)
52
+ else
53
+ handle_error_response(response)
54
+ end
55
+ end
56
+
57
+ # Put request
58
+ #
59
+ # @param [String] path
60
+ # @param [Object] obj
61
+ # @param [Hash] params
62
+ # @param [Hash] headers
63
+ # @return [Hash]
64
+ def put(path, obj, params = {}, headers = {})
65
+ request_headers = request_headers(headers)
66
+ request_options = {
67
+ header: request_headers,
68
+ body: encode_body(obj, request_headers['Content-Type']),
69
+ query: params
70
+ }
71
+
72
+ response = http_client.put(request_uri(path), request_options)
73
+ if [200, 201].include?(response.status)
74
+ parse_response(response)
75
+ else
76
+ handle_error_response(response)
77
+ end
78
+ end
79
+
80
+ # Delete request
81
+ #
82
+ # @param [String] path
83
+ # @param [Hash,String] body
84
+ # @param [Hash] params
85
+ # @param [Hash] headers
86
+ # @return [Hash]
87
+ def delete(path, body = nil, params = {}, headers = {})
88
+ request_headers = request_headers(headers)
89
+ request_options = {
90
+ header: request_headers,
91
+ body: encode_body(body, request_headers['Content-Type']),
92
+ query: params
93
+ }
94
+ response = http_client.delete(request_uri(path), request_options)
95
+ if response.status == 200
96
+ parse_response(response)
97
+ else
98
+ handle_error_response(response)
99
+ end
100
+ end
101
+
102
+ private
103
+
104
+ ##
105
+ # Get full request uri
106
+ #
107
+ # @param [String] path
108
+ # @return [String]
109
+ def request_uri(path)
110
+ "#{@api_url}#{path}"
111
+ end
112
+
113
+ ##
114
+ # Get request headers
115
+ #
116
+ # @param [Hash] headers
117
+ # @return [Hash]
118
+ def request_headers(headers = {})
119
+ @default_headers.merge(headers)
120
+ end
121
+
122
+ ##
123
+ # Encode body based on content type
124
+ #
125
+ # @param [Object] body
126
+ # @param [String] content_type
127
+ def encode_body(body, content_type)
128
+ if content_type == 'application/json'
129
+ dump_json(body)
130
+ else
131
+ body
132
+ end
133
+ end
134
+
135
+ ##
136
+ # Parse response
137
+ #
138
+ # @param [HTTP::Message]
139
+ # @return [Object]
140
+ def parse_response(response)
141
+ if response.headers['Content-Type'].include?('application/json')
142
+ parse_json(response.body)
143
+ else
144
+ response.body
145
+ end
146
+ end
147
+
148
+ ##
149
+ # Parse json
150
+ #
151
+ # @param [String] json
152
+ # @return [Hash,Object,NilClass]
153
+ def parse_json(json)
154
+ JSON.parse(json) rescue nil
155
+ end
156
+
157
+ ##
158
+ # Dump json
159
+ #
160
+ # @param [Object] obj
161
+ # @return [String]
162
+ def dump_json(obj)
163
+ JSON.dump(obj)
164
+ end
165
+
166
+ def handle_error_response(response)
167
+ raise Qtc::Errors::StandardError.new(response.status, response.body)
168
+ end
169
+ end
170
+ end
@@ -0,0 +1,117 @@
1
+ require_relative 'collection'
2
+ require_relative 'user_collection'
3
+ require_relative 'usergroup_collection'
4
+
5
+ module Qtc
6
+ module Eds
7
+ class Client
8
+
9
+ DEFAULT_OPTIONS = {
10
+ api_url: 'https://api.engin.io/v1'
11
+ }
12
+
13
+ ##
14
+ # Initialize
15
+ #
16
+ # @param [String] backend_id
17
+ # @param [Hash] options
18
+ def initialize(backend_id, options = {})
19
+ @options = DEFAULT_OPTIONS.merge(options)
20
+ @backend_id = backend_id
21
+ headers = {'Enginio-Backend-Id' => @backend_id}
22
+ @client = Qtc::Client.new(@options[:api_url], headers)
23
+ end
24
+
25
+ ##
26
+ # Get Qtc::Client instance
27
+ #
28
+ # @return [Qtc::Client]
29
+ def http_client
30
+ @client
31
+ end
32
+
33
+ ##
34
+ # Get collection
35
+ #
36
+ # @param [String] name
37
+ # @return [Qtc::Eds::Collection]
38
+ def collection(name)
39
+ Qtc::Eds::Collection.new(@client, "/objects/#{name}")
40
+ end
41
+
42
+ ##
43
+ # Get user collection
44
+ #
45
+ # @return [Qtc::Eds::UserCollection]
46
+ def users
47
+ Qtc::Eds::UserCollection.new(@client)
48
+ end
49
+
50
+ ##
51
+ # Get usergroup collection
52
+ #
53
+ # @return [Qtc::Eds::UsergroupCollection]
54
+ def usergroups
55
+ Qtc::Eds::UsergroupCollection.new(@client)
56
+ end
57
+
58
+ def current_user
59
+ if @client.default_headers['Authorization']
60
+ @client.get('/user')
61
+ end
62
+ end
63
+
64
+ ##
65
+ # Set access token
66
+ #
67
+ # @param [String] access_token
68
+ def access_token=(access_token)
69
+ if !access_token.nil?
70
+ @client.default_headers['Authorization'] = "Bearer #{access_token}"
71
+ else
72
+ @client.default_headers.delete('Authorization')
73
+ end
74
+ end
75
+
76
+ ##
77
+ # Call block with given access token
78
+ #
79
+ # @param [String] access_token
80
+ # @param []
81
+ def with_access_token(access_token, &block)
82
+ prev_auth = @client.default_headers['Authorization'].dup
83
+ @client.default_headers['Authorization'] = "Bearer #{access_token}"
84
+ result = call(&block)
85
+ @client.default_headers['Authorization'] = prev_auth
86
+ result
87
+ ensure
88
+ @client.default_headers['Authorization'] = prev_auth if prev_auth
89
+ end
90
+
91
+ ##
92
+ # Create user access token
93
+ #
94
+ # @param [String] username
95
+ # @param [String] password
96
+ def create_user_token(username, password)
97
+ body = {
98
+ grant_type: 'password',
99
+ username: username,
100
+ password: password
101
+ }
102
+ @client.post('/auth/oauth2/token', body, {}, {'Content-Type' => 'application/x-www-form-urlencoded'})
103
+ end
104
+
105
+ ##
106
+ # Revoke user access token
107
+ #
108
+ # @param [String] token
109
+ def revoke_user_token(token)
110
+ body = {
111
+ token: token
112
+ }
113
+ @client.post('/auth/oauth2/revoke', body, {}, {'Content-Type' => 'application/x-www-form-urlencoded'})
114
+ end
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,125 @@
1
+ module Qtc
2
+ module Eds
3
+ class Collection
4
+
5
+ ##
6
+ # Initialize EDS collection
7
+ #
8
+ # @param [Qtc::Client] client
9
+ # @param [String] path
10
+ def initialize(client, path)
11
+ @client = client
12
+ @path = path
13
+ end
14
+
15
+ ##
16
+ # Insert a new object
17
+ #
18
+ # @param [Hash] object
19
+ # @return [Hash]
20
+ def insert(object)
21
+ client.post(path, object)
22
+ end
23
+
24
+ ##
25
+ # Update object
26
+ #
27
+ # @param [String] id
28
+ # @param [Hash] object
29
+ # @return [Hash]
30
+ def update(id, object)
31
+ client.put("#{path}/#{id}", object)
32
+ end
33
+
34
+ ##
35
+ # Atomic operation
36
+ #
37
+ # @param [String] id
38
+ # @param [Hash] operation
39
+ # @return [Hash]
40
+ def atomic_operation(id, operation)
41
+ client.put("#{path}/#{id}/atomic", operation)
42
+ end
43
+
44
+ ##
45
+ # Remove object
46
+ #
47
+ # @param [String]
48
+ # @return [Hash]
49
+ def remove(id)
50
+ client.delete("#{path}/#{id}")
51
+ end
52
+
53
+ ##
54
+ # Find object by id
55
+ #
56
+ # @param [String] id
57
+ # @return [Hash]
58
+ def find_one(id)
59
+ client.get("#{path}/#{id}")
60
+ end
61
+
62
+ ##
63
+ # Find objects
64
+ #
65
+ # @param [Hash] params
66
+ # @return [Array<Hash>]
67
+ def find(params = {})
68
+ if params[:q] && params[:q].is_a?(Hash)
69
+ params[:q] = params[:q].to_json
70
+ end
71
+ if params[:sort] && !params[:sort].is_a?(String)
72
+ params[:sort] = params[:sort].to_json
73
+ end
74
+ response = client.get("#{path}", params)
75
+ response['results']
76
+ end
77
+
78
+ ##
79
+ # Set object permissions
80
+ #
81
+ # @param [String]
82
+ def set_permissions(id, permissions)
83
+ client.post("#{path}/#{id}/access", permissions)
84
+ end
85
+
86
+ ##
87
+ # Add permissions
88
+ #
89
+ # @param [String] id
90
+ # @param [Hash] permissions
91
+ # @return [Hash]
92
+ def add_permissions(id, permissions)
93
+ client.put("#{path}/#{id}/access", permissions)
94
+ end
95
+
96
+ ##
97
+ # Remove permissions
98
+ #
99
+ # @param [String] id
100
+ # @param [Hash] permissions
101
+ # @return [Hash]
102
+ def remove_permissions(id, permissions)
103
+ client.delete("#{path}/#{id}/access", permissions)
104
+ end
105
+
106
+ protected
107
+
108
+ ##
109
+ # Get client
110
+ #
111
+ # @return [Qtc::Client]
112
+ def client
113
+ @client
114
+ end
115
+
116
+ ##
117
+ # Get path
118
+ #
119
+ # @return [String]
120
+ def path
121
+ @path
122
+ end
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,14 @@
1
+ module Qtc
2
+ module Eds
3
+ class UserCollection < Collection
4
+
5
+ ##
6
+ # Initialize EDS user collection
7
+ #
8
+ # @param [Qtc::Client] client
9
+ def initialize(client)
10
+ super(client, '/users')
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,42 @@
1
+ module Qtc
2
+ module Eds
3
+ class UsergroupCollection < Collection
4
+
5
+ ##
6
+ # Initialize EDS usergroup collection
7
+ #
8
+ # @param [Qtc::Client] client
9
+ def initialize(client)
10
+ super(client, '/usergroups')
11
+ end
12
+
13
+ ##
14
+ # Add member to usergroup
15
+ #
16
+ # @param [String] id
17
+ # @param [Hash] user
18
+ def add_member(id, user)
19
+ client.post("#{path}/#{id}/members", user)
20
+ end
21
+
22
+ ##
23
+ # Remove member from usergroup
24
+ #
25
+ # @param [String] id
26
+ # @param [Hash] user
27
+ def remove_member(id, user)
28
+ client.delete("#{path}/#{id}/members", user)
29
+ end
30
+
31
+ ##
32
+ # Get usergroup members
33
+ #
34
+ # @param [String] id
35
+ # @return [Array<Hash>]
36
+ def members(id)
37
+ response = client.get("#{path}/#{id}/members")
38
+ response['results']
39
+ end
40
+ end
41
+ end
42
+ end
data/lib/qtc/errors.rb ADDED
@@ -0,0 +1,11 @@
1
+ module Qtc
2
+ module Errors
3
+ class StandardError < ::StandardError
4
+
5
+ def initialize(status, message)
6
+ @status = status
7
+ super(message)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,56 @@
1
+ require 'faye/websocket'
2
+
3
+ module Qtc
4
+ module Mws
5
+ class Client
6
+
7
+ DEFAULT_OPTIONS = {
8
+ api_url: 'https://mws-eu-1.qtc.io/v1'
9
+ }
10
+
11
+ attr_accessor :gateway_id, :options, :http_client
12
+
13
+ ##
14
+ # Initialize
15
+ #
16
+ # @param [String] gateway_id
17
+ # @param [Hash] options
18
+ def initialize(gateway_id, options = {})
19
+ self.gateway_id = opts[:gateway_id]
20
+ self.options = DEFAULT_OPTIONS.merge(options)
21
+ self.http_client = Qtc::Client.new(http_client_url)
22
+ end
23
+
24
+ ##
25
+ # Send message to websocket clients
26
+ #
27
+ # @param [String] message
28
+ # @param [Hash] receivers
29
+ # @option receivers [Array<String>] :sockets
30
+ # @option receivers [Array<String>] :tags
31
+ def send_message(message, receivers = {sockets: ['*'], tags: nil})
32
+ data = {data: message, receivers: receivers}
33
+ self.http_client.post('/messages', data)
34
+ end
35
+
36
+ ##
37
+ # Get websocket client
38
+ #
39
+ # @return [Faye::WebSocket::Client]
40
+ def websocket_client
41
+ uri = self.http_client.get('/websocket_uri')
42
+ Faye::WebSocket::Client.new(uri["uri"])
43
+ end
44
+
45
+ private
46
+
47
+ ##
48
+ # Get url for HTTPClient
49
+ #
50
+ # @return [String]
51
+ def http_client_url
52
+ "#{self.options[:api_url]}/gateways/#{self.gateway_id}"
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,3 @@
1
+ module Qtc
2
+ VERSION = "0.0.1"
3
+ end
data/lib/qtc-sdk.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'qtc/client'
2
+ require 'qtc/eds/client'
data/lib/qtc_cli.rb ADDED
@@ -0,0 +1,12 @@
1
+ require 'thor'
2
+ require 'qtc/client'
3
+ require 'qtc/cli/instances'
4
+ require 'qtc/cli/mar'
5
+
6
+ class QtcCli < Thor
7
+ desc 'mar', 'Managed application runtime commands'
8
+ subcommand 'mar', Qtc::Cli::Mar
9
+
10
+ desc 'instances', 'Instances commands'
11
+ subcommand 'instances', Qtc::Cli::Instances
12
+ end