sticapi_client 4.0.1 → 4.0.3
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 +111 -53
- 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: 7f3371b95279762a94030c62e10800d62055fb95674b44303250e87eb311c858
|
|
4
|
+
data.tar.gz: ea17919f9b725ccc5e09beb593edb3968ea5348860e7830736fef391f22a97f6
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: a554af2400a3baaf1c51e76906ad525013cd848903190656588ff6de4a865c735db8672efd52f451880094693c5e88072fd0e28d2fd9cd0560418368014cb7cd
|
|
7
|
+
data.tar.gz: ca3b1c42e5c7ac016fb228185fc0486733739d75853b948de828b7601ab8b08cc0d2848063d56ba36693af8766410a90dc0f2a1a20ae3c970d121278ac7396e4
|
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"]
|
|
@@ -126,15 +142,28 @@ module Sticapi
|
|
|
126
142
|
end
|
|
127
143
|
end
|
|
128
144
|
|
|
145
|
+
def flatten_params(params, prefix = nil)
|
|
146
|
+
case params
|
|
147
|
+
when Hash
|
|
148
|
+
params.flat_map { |key, value| flatten_params(value, prefix ? "#{prefix}[#{key}]" : key.to_s) }
|
|
149
|
+
when Array
|
|
150
|
+
params.flat_map { |value| flatten_params(value, "#{prefix}[]") }
|
|
151
|
+
else
|
|
152
|
+
[[prefix, params]]
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
|
|
129
156
|
def http_request(route, options = {})
|
|
130
157
|
kind = options[:kind] || "post"
|
|
131
158
|
uri = URI.parse("#{self.uri}#{route}")
|
|
132
159
|
http = Net::HTTP.new(uri.host, uri.port)
|
|
133
160
|
http.use_ssl = @ssl
|
|
161
|
+
http.open_timeout = OPEN_TIMEOUT
|
|
162
|
+
http.read_timeout = READ_TIMEOUT
|
|
134
163
|
|
|
135
164
|
request = if kind == "get"
|
|
136
|
-
|
|
137
|
-
uri.query = URI.encode_www_form(
|
|
165
|
+
flat_params = flatten_params(options.except(:kind))
|
|
166
|
+
uri.query = URI.encode_www_form(flat_params) if flat_params.any?
|
|
138
167
|
Net::HTTP::Get.new(uri.request_uri)
|
|
139
168
|
else
|
|
140
169
|
req = Net::HTTP::Post.new(uri.request_uri)
|
|
@@ -152,7 +181,11 @@ module Sticapi
|
|
|
152
181
|
end
|
|
153
182
|
|
|
154
183
|
def token_cache_path
|
|
155
|
-
"#{
|
|
184
|
+
"#{app_root}/tmp/sticapi_token_#{host_namespace}.yml"
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
def host_namespace
|
|
188
|
+
(@host || "default").gsub(/[^a-zA-Z0-9]/, "_")
|
|
156
189
|
end
|
|
157
190
|
|
|
158
191
|
def load_token
|
|
@@ -181,12 +214,37 @@ module Sticapi
|
|
|
181
214
|
expiry: @expiry
|
|
182
215
|
}.to_yaml)
|
|
183
216
|
end
|
|
217
|
+
|
|
218
|
+
def token_expired?
|
|
219
|
+
@access_token.blank? || (DateTime.now > Time.at(@expiry.to_i))
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
def parse_response(response)
|
|
223
|
+
JSON.parse(response.body)
|
|
224
|
+
rescue JSON::ParserError => e
|
|
225
|
+
log_error("Invalid JSON (status #{response.code}): #{e.message}")
|
|
226
|
+
raise JSON::ParserError, "Sticapi #{response.code}: #{e.message}"
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
def log_error(msg)
|
|
230
|
+
if defined?(Rails) && Rails.respond_to?(:logger) && Rails.logger
|
|
231
|
+
Rails.logger.error("[SticapiClient] #{msg}")
|
|
232
|
+
else
|
|
233
|
+
$stderr.puts("[SticapiClient] #{msg}")
|
|
234
|
+
end
|
|
235
|
+
end
|
|
236
|
+
|
|
237
|
+
def app_root
|
|
238
|
+
defined?(Rails) ? Rails.root : Dir.pwd
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
def app_env
|
|
242
|
+
defined?(Rails) ? Rails.env : ENV.fetch("RAILS_ENV", "development")
|
|
243
|
+
end
|
|
184
244
|
end
|
|
185
245
|
end
|
|
186
246
|
|
|
187
|
-
# Add sticapi_authenticatable strategy to defaults.
|
|
188
|
-
#
|
|
189
247
|
Devise.add_module(:sticapi_authenticatable,
|
|
190
|
-
route: :session,
|
|
248
|
+
route: :session,
|
|
191
249
|
strategy: true,
|
|
192
250
|
controller: :sessions)
|