jieshun-parking 0.6.4 → 0.6.5

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 318dc7eecf5a1390f6027cff4886d4beb60068a4cdfb220d4b280bd95625e8a6
4
- data.tar.gz: 484b8893041d1c8ae4bb7b1880dc437dcaaaa0529f02cea311503fb0c12e87b4
3
+ metadata.gz: 68926a0aeaf9f28755c8f054a766714f1ebe50919458c8085bc69d09a3b4c150
4
+ data.tar.gz: 1b625c14754f9dd1b34892d0f552412809d1cf4c67859ee24544f8a6269fcd90
5
5
  SHA512:
6
- metadata.gz: b6cea4b4d3353a966032dd245e4b2055c5f56f65af528f437f11bd4baab1c60fa4208aec10d64b43db82606ad836d3d8d55bee64a6af8706aff7b9b2db862d5c
7
- data.tar.gz: f0dd3715ca33b6639dd3bd2a120ab0686d1f7affd9cd58e01fe01a2a050cac304b442c35480022fdc066b1076ad89e1eb02c2778b191d7bc303f226f1a650793
6
+ metadata.gz: 2b27373583050c8574fbfbf209c03e2f8e44b06f4a65bb56e1873f526e1f8f04c64c3fdae520141654a4062b2b80594f73cdae843ca4d5806513c89e679abae2
7
+ data.tar.gz: 3a1ab8e23b5b34339d65364835cb33872ad3a000156c24e557a85d7bd556dfaa43e87157bda9ff9517b76ca42c6bbc3082db60ba8a5e3e4e509ead55892dcc49
@@ -1,104 +1,106 @@
1
1
  module Jieshun
2
- module Parking
3
- # 捷顺数据中心 Client, 目前主要用来获取图片, 对应文档为 QX205_数据中心标准推送协议-1.1.6(1)
4
- class DataCenterClient
2
+ module Parking
3
+ # 捷顺数据中心 Client, 目前主要用来获取图片, 对应文档为 QX205_数据中心标准推送协议-1.1.6(1)
4
+ class DataCenterClient
5
+ REDIS_AUTH_KEY = 'jieshun.parking.datacenter.auth.token'
6
+ REDIS_AUTH_KEY_LOCK = 'jieshun.parking.datacenter.auth.token.lock'
7
+ MAX_RETRY_COUNT = 3 # 定义refresh_token 最大重试次数
8
+ def initialize(configuration, http_client = StandardHttpClient.new)
9
+ @configuration = configuration
10
+ @http_client = http_client
11
+ @redis_client = RedisClient.new(@configuration.redis_config)
12
+ end
5
13
 
6
- REDIS_AUTH_KEY = 'jieshun.parking.datacenter.auth.token'
7
- REDIS_AUTH_KEY_LOCK = 'jieshun.parking.datacenter.auth.token.lock'
8
- MAX_RETRY_COUNT = 3 # 定义refresh_token 最大重试次数
9
- def initialize(configuration, http_client = StandardHttpClient.new)
10
- @configuration = configuration
11
- @http_client = http_client
12
- @redis_client = RedisClient.new(@configuration.redis_config)
13
- end
14
+ def get_auth_token
15
+ @redis_client.redis_get(REDIS_AUTH_KEY)
16
+ end
14
17
 
15
- def get_auth_token
16
- @redis_client.redis_get(REDIS_AUTH_KEY)
17
- end
18
+ def login_if_required
19
+ auth_token = get_auth_token
20
+ if Jieshun::Parking.always_login || auth_token.nil? || auth_token.empty?
21
+ # 使用 Redis 锁来避免并发刷新
22
+ lock_key = REDIS_AUTH_KEY_LOCK
23
+ begin
24
+ # 设置一个5秒过期的锁
25
+ if @redis_client.redis_set_nx_lock(lock_key, 5)
26
+ # 重试机制
27
+ Retry.with_retries(max_tries: MAX_RETRY_COUNT, sleep_seconds: 1) do
28
+ # 过程中遇到错误重试时, 检查本地token是否存在
29
+ auth_token = get_auth_token
30
+ if auth_token
31
+ yield(auth_token)
32
+ else
33
+ refresh_token do |auth_token|
34
+ yield(auth_token)
35
+ end
36
+ end
37
+ end
38
+ end
39
+ rescue StandardError => e
40
+ Jieshun::Parking.logger.info("Failed to refresh token;error: #{e.message}")
41
+ ensure
42
+ # 确保锁最终被释放
43
+ @redis_client.redis_del(lock_key)
44
+ end
45
+ else
46
+ yield(auth_token)
47
+ end
48
+ end
18
49
 
50
+ def refresh_token
51
+ hash = { pno: @configuration.pno, secret: @configuration.secret }
52
+ @http_client.post_json("#{@configuration.server_url}/api/login", {}, hash) do |result|
53
+ auth_token = result.body['tn']
54
+ expire_time = result.body['timeOut'] - 1000
55
+ @redis_client.redis_set(REDIS_AUTH_KEY, auth_token, expire_time) # token will be expired after 2 hours
56
+ yield(auth_token)
57
+ end
58
+ end
19
59
 
20
- def login_if_required
21
- auth_token = get_auth_token
22
- if Jieshun::Parking.always_login || auth_token.nil? || auth_token.empty?
23
- # 使用 Redis 锁来避免并发刷新
24
- lock_key = REDIS_AUTH_KEY_LOCK
25
- begin
26
- # 设置一个5秒过期的锁
27
- if @redis_client.redis_set_nx_lock(lock_key, 5)
28
- # 重试机制
29
- Retry.with_retries(max_tries: MAX_RETRY_COUNT, sleep_seconds: 1) do
30
- refresh_token do |auth_token|
31
- yield(auth_token)
32
- end
33
- end
34
- end
35
- rescue StandardError => e
36
- Jieshun::Parking.logger.info("Failed to refresh token;error: #{e.message}")
37
- ensure
38
- # 确保锁最终被释放
39
- @redis_client.redis_del(lock_key)
40
- end
41
- end
42
- yield(auth_token)
43
- end
60
+ def get_picture_base64(file_path)
61
+ data_items = []
62
+ data_items << { filePath: file_path }
63
+ unify_call("/api/pic/picSearch", data_items.to_json) do |result|
64
+ image = result.body['dataItems'].first['image']
65
+ mime_type = detect_mime_type(image)
66
+ yield({ mime_type_prefix: "data:#{mime_type};base64,", content: image })
67
+ end
68
+ end
44
69
 
45
- def refresh_token
46
- hash = { pno: @configuration.pno, secret: @configuration.secret }
47
- @http_client.post_json("#{@configuration.server_url}/api/login", {}, hash) do |result|
48
- auth_token = result.body['tn']
49
- expire_time = result.body['timeOut'] - 1000
50
- @redis_client.redis_set(REDIS_AUTH_KEY, auth_token, expire_time) # token will be expired after 2 hours
51
- yield(auth_token)
52
- end
53
- end
70
+ def detect_mime_type(base64_content)
71
+ return "application/pdf" if content_matches?(base64_content, ['JVBERi0'])
72
+ return "image/gif" if content_matches?(base64_content, ['R0lGODdh', 'R0lGODlh'])
73
+ return "image/png" if content_matches?(base64_content, ['iVBORw0KGgo'])
74
+ return "image/jpg" if content_matches?(base64_content, ['/9j/'])
75
+ return "image/bmp" if content_matches?(base64_content, ['Qk'])
76
+ "application/octet-stream"
77
+ end
54
78
 
55
- def get_picture_base64(file_path)
56
- data_items = []
57
- data_items << { filePath: file_path }
58
- unify_call("/api/pic/picSearch", data_items.to_json) do |result|
59
- image = result.body['dataItems'].first['image']
60
- mime_type = detect_mime_type(image)
61
- yield( { mime_type_prefix: "data:#{mime_type};base64,", content: image } )
62
- end
63
- end
79
+ def content_matches?(base64_content, prefixes)
80
+ prefixes.any? { |prefix| base64_content.start_with?(prefix) }
81
+ end
64
82
 
65
- def detect_mime_type(base64_content)
66
- return "application/pdf" if content_matches?(base64_content, ['JVBERi0'])
67
- return "image/gif" if content_matches?(base64_content, ['R0lGODdh', 'R0lGODlh'])
68
- return "image/png" if content_matches?(base64_content, ['iVBORw0KGgo'])
69
- return "image/jpg" if content_matches?(base64_content, ['/9j/'])
70
- return "image/bmp" if content_matches?(base64_content, ['Qk'])
71
- "application/octet-stream"
72
- end
83
+ def unify_call(api_path, data_items)
84
+ login_if_required do |auth_token|
85
+ ts = Time.now.strftime("%Y%m%d%H%M%S%L")
86
+ sn = generate_signature("#{@configuration.secret}#{ts}#{data_items}")
87
+ hash = {
88
+ pno: @configuration.pno,
89
+ tn: auth_token,
90
+ ts: ts,
91
+ sn: sn,
92
+ dataItems: data_items
93
+ }
94
+ @http_client.post_json("#{@configuration.server_url}/#{api_path}", {}, hash) do |result|
95
+ yield(result)
96
+ end
97
+ end
98
+ end
73
99
 
74
- def content_matches?(base64_content, prefixes)
75
- prefixes.any? { |prefix| base64_content.start_with?(prefix) }
76
- end
77
-
78
- def unify_call(api_path, data_items)
79
- login_if_required do |auth_token|
80
- ts = Time.now.strftime("%Y%m%d%H%M%S%L")
81
- sn = generate_signature("#{@configuration.secret}#{ts}#{data_items}")
82
- hash = {
83
- pno: @configuration.pno,
84
- tn: auth_token,
85
- ts: ts,
86
- sn: sn,
87
- dataItems: data_items
88
- }
89
-
90
- @http_client.post_json("#{@configuration.server_url}/#{api_path}", { }, hash) do |result|
91
- yield(result)
92
- end
93
-
94
- end
95
- end
96
-
97
- def generate_signature(payload)
98
- md5_string = Digest::MD5.hexdigest(payload)
99
- md5_string.upcase
100
- end
101
-
102
- end
103
- end
100
+ def generate_signature(payload)
101
+ md5_string = Digest::MD5.hexdigest(payload)
102
+ md5_string.upcase
103
+ end
104
+ end
105
+ end
104
106
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Jieshun
4
4
  module Parking
5
- VERSION = "0.6.4"
5
+ VERSION = "0.6.5"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jieshun-parking
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.4
4
+ version: 0.6.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - LCola