synapse_fi 0.0.1

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.
@@ -0,0 +1,228 @@
1
+ require 'rest-client'
2
+ require 'open-uri'
3
+ require 'json'
4
+ require_relative './error'
5
+
6
+ module Synapse
7
+ # Wrapper for HTTP requests using RestClient.
8
+ class HTTPClient
9
+
10
+ # @!attribute [rw] base_url
11
+ # @return [String] the base url of the API (production or sandbox)
12
+ # @!attribute [rw] config
13
+ # @return [Hash] various settings related to request headers
14
+ # @!attribute [rw] raise_for_202
15
+ # @return [Boolean] relating to how to handle 202 exception
16
+ attr_accessor :base_url, :config, :raise_for_202
17
+
18
+ # @param base_url [String] the base url of the API (production or sandbox)
19
+ # @param client_id [String]
20
+ # @param client_secret [String]
21
+ # @param fingerprint [String]
22
+ # @param ip_address [String]
23
+ # @param raise_for_202 [String]
24
+ # @param logging [Boolean] (optional) logs to stdout when true
25
+ # @param log_to [String] (optional) file path to log to file (logging must be true)
26
+ def initialize(base_url:, client_id:, client_secret:, fingerprint:, ip_address:, raise_for_202:false, **options)
27
+ @raise_for_202 = raise_for_202
28
+ log_to = options[:log_to] || 'stdout'
29
+ RestClient.log = log_to if options[:logging]
30
+ @logging = options[:logging]
31
+
32
+ @config = {
33
+ client_id: client_id,
34
+ client_secret: client_secret,
35
+ fingerprint: fingerprint,
36
+ ip_address: ip_address,
37
+ oauth_key: '',
38
+ }
39
+ @base_url = base_url
40
+ end
41
+
42
+ # Returns headers for HTTP requests.
43
+ # @return [Hash]
44
+ def headers
45
+ user = "#{config[:oauth_key]}|#{config[:fingerprint]}"
46
+ gateway = "#{config[:client_id]}|#{config[:client_secret]}"
47
+ headers = {
48
+ content_type: :json,
49
+ accept: :json,
50
+ 'X-SP-GATEWAY' => gateway,
51
+ 'X-SP-USER' => user,
52
+ 'X-SP-USER-IP' => config[:ip_address],
53
+ }
54
+ if config[:idemopotency_key]
55
+ headers['X-SP-IDEMPOTENCY-KEY'] = config[:idemopotency_key]
56
+ end
57
+ headers
58
+ end
59
+
60
+ # Alias for headers (copy of current headers)
61
+ alias_method :get_headers, :headers
62
+
63
+ # Updates current HTPP headers
64
+ # @param fingerprint [String]
65
+ # @param oauth_key [String]
66
+ # @param fingerprint [String]
67
+ # @param client_id [String]
68
+ # @param client_secret [String]
69
+ # @param ip_address [String]
70
+ # @param idemopotency_key [String]
71
+ def update_headers(oauth_key: nil, fingerprint: nil, client_id: nil, client_secret: nil, ip_address: nil, idemopotency_key: nil)
72
+ config[:fingerprint] = fingerprint if fingerprint
73
+ config[:oauth_key] = oauth_key if oauth_key
74
+ config[:client_id] = client_id if client_id
75
+ config[:client_secret] = client_secret if client_secret
76
+ config[:ip_address] = ip_address if ip_address
77
+ config[:idemopotency_key] = idemopotency_key if idemopotency_key
78
+ nil
79
+ end
80
+
81
+ # Send a POST request to the given path with the given payload
82
+ # @param path [String]
83
+ # @param payload [HASH]
84
+ # @param **options payload = idempotency_key [String] (optional) avoid accidentally performing the same operation twice
85
+ # @return [Hash] API response
86
+ # @raise [Synapse::Error] subclass depends on HTTP response
87
+ def post(path, payload, **options)
88
+ #copy of current headers
89
+ headers = get_headers
90
+
91
+ # update the headers with idempotency_key
92
+ if options[:idempotency_key]
93
+ headers = headers.merge({'X-SP-IDEMPOTENCY-KEY' => options[:idempotency_key]})
94
+ end
95
+
96
+ response = with_error_handling { RestClient::Request.execute(:method => :post, :url => full_url(path), :payload => payload.to_json, :headers => headers, :timeout => 300) }
97
+ puts 'RESPONSE:', JSON.parse(response) if @logging
98
+ response = JSON.parse(response)
99
+
100
+ if raise_for_202 && response["http_code"] == "202"
101
+ raise Error.from_response(response)
102
+ elsif response["error"]
103
+ raise Error.from_response(response)
104
+ else
105
+ response
106
+ end
107
+ end
108
+
109
+ # Sends a GET request to the given path with the given payload.
110
+ # @param path [String]
111
+ # @return [Hash] API response
112
+ # @raise [Synapse::Error] subclass depends on HTTP response
113
+ def get(path)
114
+ response = with_error_handling {RestClient.get(full_url(path), headers)}
115
+ puts 'RESPONSE:', JSON.parse(response) if @logging
116
+ response = JSON.parse(response)
117
+
118
+ if raise_for_202 && response["http_code"] == "202"
119
+ raise Error.from_response(response)
120
+ elsif response["error"]
121
+ raise Error.from_response(response)
122
+ else
123
+ response
124
+ end
125
+ end
126
+
127
+ # Sends a DELETE request to the given path
128
+ # @param path [String]
129
+ # @return [Hash] API response
130
+ # @raise [Synapse::Error] subclass depends on HTTP response
131
+ def delete(path)
132
+ response = with_error_handling {RestClient.delete(full_url(path), headers)}
133
+ puts 'RESPONSE:', JSON.parse(response) if @logging
134
+ response = JSON.parse(response)
135
+
136
+ if raise_for_202 && response["http_code"] == "202"
137
+ raise Error.from_response(response)
138
+ elsif response["error"]
139
+ raise Error.from_response(response)
140
+ else
141
+ response
142
+ end
143
+ end
144
+
145
+ # Sends a PATCH request to the given path with the given payload.
146
+ # @param path [String]
147
+ # @param payload [Hash]
148
+ # @return [Hash] API response
149
+ # @raise [Synapse::Error] subclass depends on HTTP response
150
+ def patch(path, payload)
151
+ response = with_error_handling {RestClient::Request.execute(:method => :patch, :url => full_url(path), :payload => payload.to_json, :headers => headers, :timeout => 300)}
152
+ p 'RESPONSE:', JSON.parse(response) if @logging
153
+ response = JSON.parse(response)
154
+
155
+ if raise_for_202 && response["http_code"] == "202"
156
+ raise Error.from_response(response)
157
+ elsif response["error"]
158
+ raise Error.from_response(response)
159
+ else
160
+ response
161
+ end
162
+ end
163
+
164
+ def oauthenticate(user_id:)
165
+ refresh_token = refresh_token(user_id: user_id)
166
+ end
167
+
168
+ private
169
+
170
+ # get user
171
+ # get refresh_token
172
+ # send refresh_token to oauth path
173
+ # grabs the refresh token and formats a refresh token payload
174
+ def refresh_token(user_id:)
175
+ path = "/users/#{user_id}"
176
+ response = get(path)
177
+ refresh_token = response["refresh_token"]
178
+
179
+ refresh_token = {"refresh_token" => refresh_token}
180
+ oauth_path = oauth_path(user_id)
181
+ authenticate(refresh_token, oauth_path)
182
+ end
183
+
184
+ # options payload to change scope of oauth
185
+ def authenticate(refresh_token, oauth_path)
186
+ oauth_key = post(oauth_path, refresh_token)
187
+ oauth_key = oauth_key['oauth_key']
188
+ update_headers(oauth_key: oauth_key)
189
+ nil
190
+ end
191
+
192
+ def oauth_path(user_id)
193
+ path = "/oauth/#{user_id}"
194
+ end
195
+
196
+ def full_url(path)
197
+ "#{base_url}#{path}"
198
+ end
199
+
200
+ # raising an exception based on http_request
201
+ # yeilds if http_request raises an exception
202
+ def with_error_handling
203
+ yield
204
+ rescue RestClient::Exceptions::Timeout
205
+ body = {
206
+ error: {
207
+ en: "Request Timeout"
208
+ },
209
+ http_code: 504
210
+ }
211
+ raise Error.from_response(body)
212
+ rescue RestClient::Exception => e
213
+ if e.response.headers[:content_type] == 'application/json'
214
+ body = JSON.parse(e.response.body)
215
+ else
216
+ body = {
217
+ error: {
218
+ en: e.response.body
219
+ },
220
+ http_code: e.response.code
221
+ }
222
+ end
223
+ raise Error.from_response(body)
224
+ end
225
+ end
226
+ end
227
+
228
+
@@ -0,0 +1,19 @@
1
+ module Synapse
2
+
3
+ class Node
4
+
5
+ attr_reader :node_id, :user_id, :payload, :full_dehydrate, :type
6
+
7
+ attr_accessor
8
+
9
+ def initialize(node_id:, user_id:,payload:, full_dehydrate:, type:nil)
10
+ @node_id = node_id
11
+ @full_dehydrate = full_dehydrate
12
+ @user_id = user_id
13
+ @payload = payload
14
+ @type = type
15
+ end
16
+ end
17
+ end
18
+
19
+
@@ -0,0 +1,19 @@
1
+ module Synapse
2
+
3
+ class Nodes
4
+
5
+ attr_reader :page, :page_count, :limit, :payload, :nodes_count, :nodes_count
6
+
7
+ attr_accessor
8
+
9
+ def initialize(page:,limit:, page_count:, nodes_count:, payload:)
10
+ @page = page
11
+ @limit = limit
12
+ @nodes_count = nodes_count
13
+ @page_count = page_count
14
+ @payload = payload
15
+ end
16
+ end
17
+ end
18
+
19
+
@@ -0,0 +1,13 @@
1
+ module Synapse
2
+
3
+ class Subnet
4
+
5
+ attr_accessor :subnet_id, :payload
6
+
7
+ def initialize(subnet_id:, payload:, node_id:)
8
+ @subnet_id = subnet_id
9
+ @payload = payload
10
+ @node_id = node_id
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,16 @@
1
+ module Synapse
2
+
3
+ class Subnets
4
+
5
+ attr_accessor :page, :page_count, :limit, :payload, :subnets_count, :node_id
6
+
7
+ def initialize(limit:, page:, page_count:, subnets_count:, payload:, node_id:)
8
+ @page = page
9
+ @limit = limit
10
+ @subnets_count = subnets_count
11
+ @payload = payload
12
+ @page_count = page_count
13
+ @node_id = node_id
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,15 @@
1
+ module Synapse
2
+
3
+ class Subscription
4
+
5
+ attr_reader :subscription_id, :url, :payload
6
+
7
+ attr_accessor
8
+
9
+ def initialize(subscription_id:, url:, payload:)
10
+ @subscription_id = subscription_id
11
+ @url = url
12
+ @payload = payload
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,17 @@
1
+ module Synapse
2
+
3
+ class Subscriptions
4
+
5
+ attr_reader :subscriptions_count, :page, :limit, :payload, :page_count
6
+
7
+ attr_accessor
8
+
9
+ def initialize(page:,limit:, subscriptions_count:, payload:, page_count:)
10
+ @subscriptions_count = subscriptions_count
11
+ @page = page
12
+ @limit = limit
13
+ @payload = payload
14
+ @page_count = page_count
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,21 @@
1
+ module Synapse
2
+
3
+ class Transaction
4
+
5
+
6
+ attr_accessor :trans_id, :payload, :node_id, :user
7
+
8
+ def initialize(trans_id:, payload:, node_id:nil, user:nil)
9
+ @trans_id = trans_id
10
+ @payload = payload
11
+ @node_id = node_id
12
+ @user = user
13
+ end
14
+ end
15
+ end
16
+
17
+
18
+
19
+
20
+
21
+
@@ -0,0 +1,19 @@
1
+ module Synapse
2
+
3
+ class Transactions
4
+
5
+ attr_reader :page, :page_count, :limit, :payload, :trans_count
6
+
7
+ attr_accessor
8
+
9
+ def initialize(page:,limit:, trans_count:, payload:, page_count:)
10
+ @page = page
11
+ @limit = limit
12
+ @trans_count = trans_count
13
+ @payload = payload
14
+ @page_count = page_count
15
+ end
16
+ end
17
+ end
18
+
19
+