sticapi_client 4.0.1 → 4.0.2
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/sticapi_client/version.rb +1 -1
- data/lib/sticapi_client.rb +98 -51
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 842accc086921b1cb14fcc607e37a70050104724d1ea984a200f47e9c315a1a9
|
|
4
|
+
data.tar.gz: cc36ccf42b4bb49b5c654a3bb1f29797d8af06ff0deb2d0814ac86869e6f14c2
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 915f1686cee6715e68b18f2e1a9ba02f5119419ebab8d5b177088e6dfcfe5d14dff5d045df4f98f682cf927d2221c661fbd9ea079a754ba3f4aa7ed10fc0936e
|
|
7
|
+
data.tar.gz: 7127b8f5a3435f98aaff66cc50015ff75efdf2fccb3e0efaaa80974ddc9cd0ab9959cdbedf546e5fbb28182a1b850d873a57dab37c7ecf0575829780a41f0305
|
data/lib/sticapi_client.rb
CHANGED
|
@@ -21,15 +21,19 @@ require "net/http"
|
|
|
21
21
|
require "erb"
|
|
22
22
|
require "yaml"
|
|
23
23
|
require "fileutils"
|
|
24
|
+
require "monitor"
|
|
24
25
|
|
|
25
26
|
module Sticapi
|
|
26
27
|
class SticapiClient
|
|
27
28
|
include Singleton
|
|
28
29
|
|
|
30
|
+
MUTEX = Monitor.new
|
|
31
|
+
|
|
29
32
|
attr_accessor :host, :urn, :port, :ssl, :user, :password
|
|
30
33
|
attr_accessor :access_token, :client, :uid, :expiry
|
|
31
34
|
|
|
32
|
-
|
|
35
|
+
OPEN_TIMEOUT = 10
|
|
36
|
+
READ_TIMEOUT = 30
|
|
33
37
|
|
|
34
38
|
def initialize
|
|
35
39
|
load_config
|
|
@@ -41,65 +45,77 @@ module Sticapi
|
|
|
41
45
|
end
|
|
42
46
|
|
|
43
47
|
def get_token
|
|
44
|
-
return unless
|
|
48
|
+
return unless token_expired?
|
|
45
49
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
end
|
|
50
|
+
MUTEX.synchronize do
|
|
51
|
+
return unless token_expired?
|
|
49
52
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
53
|
+
sign_out if @access_token.present?
|
|
54
|
+
|
|
55
|
+
uri = URI.parse("#{self.uri}/auth/sign_in")
|
|
56
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
|
57
|
+
http.use_ssl = @ssl
|
|
58
|
+
http.open_timeout = OPEN_TIMEOUT
|
|
59
|
+
http.read_timeout = READ_TIMEOUT
|
|
60
|
+
request = Net::HTTP::Post.new(uri.request_uri)
|
|
61
|
+
request["Content-Type"] = "application/json"
|
|
62
|
+
request.body = { email: @user, password: @password }.to_json
|
|
63
|
+
response = http.request(request)
|
|
64
|
+
@access_token = response["access-token"] if response["access-token"]
|
|
65
|
+
@client = response["client"] if response["client"]
|
|
66
|
+
@uid = response["uid"] if response["uid"]
|
|
67
|
+
@expiry = response["expiry"] if response["expiry"]
|
|
68
|
+
save_token
|
|
69
|
+
end
|
|
62
70
|
end
|
|
63
71
|
|
|
64
72
|
def expiry_now
|
|
65
|
-
|
|
73
|
+
MUTEX.synchronize do
|
|
74
|
+
@expiry = 0
|
|
75
|
+
end
|
|
66
76
|
end
|
|
67
77
|
|
|
68
78
|
def sticapi_request(route, options = {})
|
|
69
|
-
|
|
70
|
-
response = http_request(route, options)
|
|
71
|
-
|
|
72
|
-
if response.code.to_i == 401
|
|
73
|
-
expiry_now
|
|
79
|
+
MUTEX.synchronize do
|
|
74
80
|
get_token
|
|
75
81
|
response = http_request(route, options)
|
|
76
|
-
end
|
|
77
82
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
+
if response.code.to_i == 401
|
|
84
|
+
expiry_now
|
|
85
|
+
get_token
|
|
86
|
+
response = http_request(route, options)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
@access_token = response["access-token"] if response["access-token"]
|
|
90
|
+
@client = response["client"] if response["client"]
|
|
91
|
+
@uid = response["uid"] if response["uid"]
|
|
92
|
+
@expiry = response["expiry"] if response["expiry"]
|
|
93
|
+
parse_response(response)
|
|
94
|
+
end
|
|
83
95
|
end
|
|
84
96
|
|
|
85
97
|
def sign_out
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
98
|
+
MUTEX.synchronize do
|
|
99
|
+
if @access_token.present?
|
|
100
|
+
uri = URI.parse("#{self.uri}/auth/sign_out")
|
|
101
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
|
102
|
+
http.use_ssl = @ssl
|
|
103
|
+
http.open_timeout = OPEN_TIMEOUT
|
|
104
|
+
http.read_timeout = READ_TIMEOUT
|
|
105
|
+
request = Net::HTTP::Delete.new(uri.request_uri)
|
|
106
|
+
request["Content-Type"] = "application/json"
|
|
107
|
+
request["access-token"] = @access_token
|
|
108
|
+
request["client"] = @client
|
|
109
|
+
request["uid"] = @uid
|
|
110
|
+
request["expiry"] = @expiry
|
|
111
|
+
http.request(request)
|
|
112
|
+
end
|
|
113
|
+
@access_token = ""
|
|
114
|
+
@client = ""
|
|
115
|
+
@uid = ""
|
|
116
|
+
@expiry = ""
|
|
117
|
+
File.delete(token_cache_path) if File.exist?(token_cache_path)
|
|
97
118
|
end
|
|
98
|
-
@access_token = ""
|
|
99
|
-
@client = ""
|
|
100
|
-
@uid = ""
|
|
101
|
-
@expiry = ""
|
|
102
|
-
File.delete(token_cache_path) if File.exist?(token_cache_path)
|
|
103
119
|
end
|
|
104
120
|
|
|
105
121
|
private
|
|
@@ -114,9 +130,9 @@ module Sticapi
|
|
|
114
130
|
@ssl = ENV.fetch("STICAPI_SSL", "false") == "true"
|
|
115
131
|
else
|
|
116
132
|
configs = YAML.safe_load(
|
|
117
|
-
ERB.new(File.read("#{
|
|
133
|
+
ERB.new(File.read("#{app_root}/config/sticapi.yml")).result,
|
|
118
134
|
aliases: true
|
|
119
|
-
)[
|
|
135
|
+
)[app_env]
|
|
120
136
|
@host = configs["host"]
|
|
121
137
|
@port = configs["port"] || 80
|
|
122
138
|
@user = configs["user"]
|
|
@@ -131,6 +147,8 @@ module Sticapi
|
|
|
131
147
|
uri = URI.parse("#{self.uri}#{route}")
|
|
132
148
|
http = Net::HTTP.new(uri.host, uri.port)
|
|
133
149
|
http.use_ssl = @ssl
|
|
150
|
+
http.open_timeout = OPEN_TIMEOUT
|
|
151
|
+
http.read_timeout = READ_TIMEOUT
|
|
134
152
|
|
|
135
153
|
request = if kind == "get"
|
|
136
154
|
params = options.except(:kind)
|
|
@@ -152,7 +170,11 @@ module Sticapi
|
|
|
152
170
|
end
|
|
153
171
|
|
|
154
172
|
def token_cache_path
|
|
155
|
-
"#{
|
|
173
|
+
"#{app_root}/tmp/sticapi_token_#{host_namespace}.yml"
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
def host_namespace
|
|
177
|
+
(@host || "default").gsub(/[^a-zA-Z0-9]/, "_")
|
|
156
178
|
end
|
|
157
179
|
|
|
158
180
|
def load_token
|
|
@@ -181,12 +203,37 @@ module Sticapi
|
|
|
181
203
|
expiry: @expiry
|
|
182
204
|
}.to_yaml)
|
|
183
205
|
end
|
|
206
|
+
|
|
207
|
+
def token_expired?
|
|
208
|
+
@access_token.blank? || (DateTime.now > Time.at(@expiry.to_i))
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
def parse_response(response)
|
|
212
|
+
JSON.parse(response.body)
|
|
213
|
+
rescue JSON::ParserError => e
|
|
214
|
+
log_error("Invalid JSON (status #{response.code}): #{e.message}")
|
|
215
|
+
raise JSON::ParserError, "Sticapi #{response.code}: #{e.message}"
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
def log_error(msg)
|
|
219
|
+
if defined?(Rails) && Rails.respond_to?(:logger) && Rails.logger
|
|
220
|
+
Rails.logger.error("[SticapiClient] #{msg}")
|
|
221
|
+
else
|
|
222
|
+
$stderr.puts("[SticapiClient] #{msg}")
|
|
223
|
+
end
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
def app_root
|
|
227
|
+
defined?(Rails) ? Rails.root : Dir.pwd
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
def app_env
|
|
231
|
+
defined?(Rails) ? Rails.env : ENV.fetch("RAILS_ENV", "development")
|
|
232
|
+
end
|
|
184
233
|
end
|
|
185
234
|
end
|
|
186
235
|
|
|
187
|
-
# Add sticapi_authenticatable strategy to defaults.
|
|
188
|
-
#
|
|
189
236
|
Devise.add_module(:sticapi_authenticatable,
|
|
190
|
-
route: :session,
|
|
237
|
+
route: :session,
|
|
191
238
|
strategy: true,
|
|
192
239
|
controller: :sessions)
|