hik_openapi 0.1.2 → 0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.rubocop.yml +67 -0
- data/Gemfile +10 -5
- data/Gemfile.lock +80 -1
- data/Guardfile +6 -0
- data/README.md +19 -34
- data/hik_openapi.gemspec +3 -5
- data/lib/hik_openapi.rb +85 -14
- data/lib/hik_openapi/version.rb +1 -1
- metadata +22 -9
- data/lib/hik_openapi/api.rb +0 -78
- data/lib/hik_openapi/configuration.rb +0 -9
- data/lib/hik_openapi/result.rb +0 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 124810a1613f0b8e8ae9491dd535697611763308962dc61dd9dc07ddb62d4a9a
|
4
|
+
data.tar.gz: fe2cbb8f1f6f87b607bc5ce1c1886c9d28553b47593a50af7e0f5ba7e73b77bf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a7f1722cf24803942375ffc473416d40d2dee05e5e4918948b65ef635bf8d7475967c1831d77573338e607f30a5ec566f685bafd5cec9e010591987ea09cc2df
|
7
|
+
data.tar.gz: b2d14d7d2419a793c9b5d05ead14adf205a241afc81ef3427f35c6d382d40633ecde524341cdfa212a47a41fc87df79e6efd20cedb68575d921d3d5db06f346e
|
data/.gitignore
CHANGED
data/.rubocop.yml
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
AllCops:
|
2
|
+
NewCops: disable
|
3
|
+
SuggestExtensions: false
|
4
|
+
TargetRubyVersion: 2.4
|
5
|
+
|
6
|
+
Layout/AccessModifierIndentation:
|
7
|
+
EnforcedStyle: outdent
|
8
|
+
|
9
|
+
Layout/LineLength:
|
10
|
+
AllowURI: true
|
11
|
+
Enabled: false
|
12
|
+
|
13
|
+
Layout/SpaceInsideHashLiteralBraces:
|
14
|
+
EnforcedStyle: no_space
|
15
|
+
|
16
|
+
Lint/Void:
|
17
|
+
Enabled: false
|
18
|
+
|
19
|
+
Metrics/BlockLength:
|
20
|
+
Max: 36
|
21
|
+
Exclude:
|
22
|
+
- spec/**/*.rb
|
23
|
+
|
24
|
+
Metrics/BlockNesting:
|
25
|
+
Max: 2
|
26
|
+
|
27
|
+
Metrics/MethodLength:
|
28
|
+
CountComments: false
|
29
|
+
Max: 10
|
30
|
+
|
31
|
+
Metrics/ModuleLength:
|
32
|
+
Max: 150
|
33
|
+
|
34
|
+
Metrics/ParameterLists:
|
35
|
+
Max: 5
|
36
|
+
MaxOptionalParameters: 5
|
37
|
+
CountKeywordArgs: true
|
38
|
+
|
39
|
+
Style/AsciiComments:
|
40
|
+
Enabled: false
|
41
|
+
|
42
|
+
Style/CollectionMethods:
|
43
|
+
Enabled: true
|
44
|
+
PreferredMethods:
|
45
|
+
map: 'collect'
|
46
|
+
map!: 'collect!'
|
47
|
+
reduce: 'inject'
|
48
|
+
find: 'detect'
|
49
|
+
find_all: 'select'
|
50
|
+
|
51
|
+
Style/Documentation:
|
52
|
+
Enabled: false
|
53
|
+
|
54
|
+
Style/DoubleNegation:
|
55
|
+
Enabled: false
|
56
|
+
|
57
|
+
Style/FrozenStringLiteralComment:
|
58
|
+
Enabled: false
|
59
|
+
|
60
|
+
Style/NumericPredicate:
|
61
|
+
Enabled: false
|
62
|
+
|
63
|
+
Style/TrailingCommaInArrayLiteral:
|
64
|
+
EnforcedStyleForMultiline: 'comma'
|
65
|
+
|
66
|
+
Style/TrailingCommaInHashLiteral:
|
67
|
+
EnforcedStyleForMultiline: 'comma'
|
data/Gemfile
CHANGED
@@ -1,7 +1,12 @@
|
|
1
|
-
source
|
1
|
+
source 'https://rubygems.org'
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
gem 'rake', '~> 12.0'
|
4
|
+
|
5
|
+
group :development, :test do
|
6
|
+
gem 'pry'
|
7
|
+
gem 'guard-rspec', require: false
|
8
|
+
gem 'rspec', '~> 3.0'
|
9
|
+
gem 'rubocop'
|
10
|
+
end
|
5
11
|
|
6
|
-
|
7
|
-
gem "rspec", "~> 3.0"
|
12
|
+
gemspec
|
data/Gemfile.lock
CHANGED
@@ -1,13 +1,71 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
hik_openapi (0.
|
4
|
+
hik_openapi (0.2)
|
5
|
+
http (~> 4.4.1)
|
5
6
|
|
6
7
|
GEM
|
7
8
|
remote: https://rubygems.org/
|
8
9
|
specs:
|
10
|
+
addressable (2.7.0)
|
11
|
+
public_suffix (>= 2.0.2, < 5.0)
|
12
|
+
ast (2.4.2)
|
13
|
+
coderay (1.1.3)
|
9
14
|
diff-lcs (1.4.4)
|
15
|
+
domain_name (0.5.20190701)
|
16
|
+
unf (>= 0.0.5, < 1.0.0)
|
17
|
+
ffi (1.15.0)
|
18
|
+
ffi-compiler (1.0.1)
|
19
|
+
ffi (>= 1.0.0)
|
20
|
+
rake
|
21
|
+
formatador (0.2.5)
|
22
|
+
guard (2.16.2)
|
23
|
+
formatador (>= 0.2.4)
|
24
|
+
listen (>= 2.7, < 4.0)
|
25
|
+
lumberjack (>= 1.0.12, < 2.0)
|
26
|
+
nenv (~> 0.1)
|
27
|
+
notiffany (~> 0.0)
|
28
|
+
pry (>= 0.9.12)
|
29
|
+
shellany (~> 0.0)
|
30
|
+
thor (>= 0.18.1)
|
31
|
+
guard-compat (1.2.1)
|
32
|
+
guard-rspec (4.7.3)
|
33
|
+
guard (~> 2.1)
|
34
|
+
guard-compat (~> 1.1)
|
35
|
+
rspec (>= 2.99.0, < 4.0)
|
36
|
+
http (4.4.1)
|
37
|
+
addressable (~> 2.3)
|
38
|
+
http-cookie (~> 1.0)
|
39
|
+
http-form_data (~> 2.2)
|
40
|
+
http-parser (~> 1.2.0)
|
41
|
+
http-cookie (1.0.3)
|
42
|
+
domain_name (~> 0.5)
|
43
|
+
http-form_data (2.3.0)
|
44
|
+
http-parser (1.2.3)
|
45
|
+
ffi-compiler (>= 1.0, < 2.0)
|
46
|
+
listen (3.5.0)
|
47
|
+
rb-fsevent (~> 0.10, >= 0.10.3)
|
48
|
+
rb-inotify (~> 0.9, >= 0.9.10)
|
49
|
+
lumberjack (1.2.8)
|
50
|
+
method_source (1.0.0)
|
51
|
+
nenv (0.3.0)
|
52
|
+
notiffany (0.1.3)
|
53
|
+
nenv (~> 0.1)
|
54
|
+
shellany (~> 0.0)
|
55
|
+
parallel (1.20.1)
|
56
|
+
parser (3.0.0.0)
|
57
|
+
ast (~> 2.4.1)
|
58
|
+
pry (0.14.0)
|
59
|
+
coderay (~> 1.1)
|
60
|
+
method_source (~> 1.0)
|
61
|
+
public_suffix (4.0.6)
|
62
|
+
rainbow (3.0.0)
|
10
63
|
rake (12.3.3)
|
64
|
+
rb-fsevent (0.10.4)
|
65
|
+
rb-inotify (0.10.1)
|
66
|
+
ffi (~> 1.0)
|
67
|
+
regexp_parser (2.1.1)
|
68
|
+
rexml (3.2.4)
|
11
69
|
rspec (3.9.0)
|
12
70
|
rspec-core (~> 3.9.0)
|
13
71
|
rspec-expectations (~> 3.9.0)
|
@@ -21,14 +79,35 @@ GEM
|
|
21
79
|
diff-lcs (>= 1.2.0, < 2.0)
|
22
80
|
rspec-support (~> 3.9.0)
|
23
81
|
rspec-support (3.9.3)
|
82
|
+
rubocop (1.11.0)
|
83
|
+
parallel (~> 1.10)
|
84
|
+
parser (>= 3.0.0.0)
|
85
|
+
rainbow (>= 2.2.2, < 4.0)
|
86
|
+
regexp_parser (>= 1.8, < 3.0)
|
87
|
+
rexml
|
88
|
+
rubocop-ast (>= 1.2.0, < 2.0)
|
89
|
+
ruby-progressbar (~> 1.7)
|
90
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
91
|
+
rubocop-ast (1.4.1)
|
92
|
+
parser (>= 2.7.1.5)
|
93
|
+
ruby-progressbar (1.11.0)
|
94
|
+
shellany (0.0.1)
|
95
|
+
thor (1.1.0)
|
96
|
+
unf (0.1.4)
|
97
|
+
unf_ext
|
98
|
+
unf_ext (0.0.7.7)
|
99
|
+
unicode-display_width (2.0.0)
|
24
100
|
|
25
101
|
PLATFORMS
|
26
102
|
ruby
|
27
103
|
|
28
104
|
DEPENDENCIES
|
105
|
+
guard-rspec
|
29
106
|
hik_openapi!
|
107
|
+
pry
|
30
108
|
rake (~> 12.0)
|
31
109
|
rspec (~> 3.0)
|
110
|
+
rubocop
|
32
111
|
|
33
112
|
BUNDLED WITH
|
34
113
|
2.1.4
|
data/Guardfile
ADDED
data/README.md
CHANGED
@@ -1,37 +1,22 @@
|
|
1
|
-
# HikOpenapi
|
2
|
-
|
3
|
-
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/hik_openapi`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
6
|
-
|
7
|
-
## Installation
|
8
|
-
|
9
|
-
Add this line to your application's Gemfile:
|
10
|
-
|
11
|
-
```ruby
|
12
|
-
gem 'hik_openapi'
|
13
|
-
```
|
14
|
-
|
15
|
-
And then execute:
|
16
|
-
|
17
|
-
$ bundle install
|
18
|
-
|
19
|
-
Or install it yourself as:
|
20
|
-
|
21
|
-
$ gem install hik_openapi
|
22
|
-
|
23
1
|
## Usage
|
24
2
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
3
|
+
```ruby
|
4
|
+
@client = HikOpenapi::Client.new do |config|
|
5
|
+
config.host = "https://192.168.3.230"
|
6
|
+
config.prefix = "/artemis"
|
7
|
+
config.app_key = "app_key"
|
8
|
+
config.app_secret = "app_secret"
|
9
|
+
config.proxy = { host: "127.0.0.1", port: 8123 } if Rails.env.development?
|
10
|
+
end
|
11
|
+
|
12
|
+
params = {
|
13
|
+
'cameraIndexCode': [url],
|
14
|
+
'streamType': 0,
|
15
|
+
'protocol': [video_type],
|
16
|
+
'transmode': 0,
|
17
|
+
'expand': "transcode=0",
|
18
|
+
}
|
19
|
+
|
20
|
+
res = @client.post("/api/video/v1/cameras/previewURLs", params)
|
36
21
|
|
37
|
-
|
22
|
+
```
|
data/hik_openapi.gemspec
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
require_relative 'lib/hik_openapi/version'
|
2
2
|
|
3
3
|
Gem::Specification.new do |spec|
|
4
|
+
spec.add_dependency 'http', '~> 4.4.1'
|
5
|
+
|
4
6
|
spec.name = 'hik_openapi'
|
5
7
|
spec.version = HikOpenapi::VERSION
|
6
8
|
spec.authors = ['palytoxin']
|
@@ -9,7 +11,7 @@ Gem::Specification.new do |spec|
|
|
9
11
|
spec.summary = 'hikvision openapi for ruby.'
|
10
12
|
spec.description = 'hikvision openapi for ruby.'
|
11
13
|
spec.homepage = 'https://github.com/palytoxin/hik_openapi'
|
12
|
-
spec.required_ruby_version = Gem::Requirement.new('>= 2.
|
14
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 2.4.0')
|
13
15
|
|
14
16
|
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
15
17
|
|
@@ -17,12 +19,8 @@ Gem::Specification.new do |spec|
|
|
17
19
|
spec.metadata['source_code_uri'] = 'https://github.com/palytoxin/hik_openapi'
|
18
20
|
spec.metadata['changelog_uri'] = 'https://github.com/palytoxin/hik_openapi'
|
19
21
|
|
20
|
-
# Specify which files should be added to the gem when it is released.
|
21
|
-
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
22
22
|
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
23
23
|
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
24
24
|
end
|
25
|
-
spec.bindir = 'exe'
|
26
|
-
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
27
25
|
spec.require_paths = ['lib']
|
28
26
|
end
|
data/lib/hik_openapi.rb
CHANGED
@@ -1,25 +1,96 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'hik_openapi/version'
|
4
|
-
require '
|
5
|
-
require '
|
6
|
-
require '
|
4
|
+
require 'securerandom'
|
5
|
+
require 'openssl'
|
6
|
+
require 'http'
|
7
7
|
|
8
8
|
module HikOpenapi
|
9
|
-
class
|
9
|
+
class Client
|
10
|
+
attr_accessor :host, :prefix, :app_key, :app_secret, :proxy, :timeouts
|
11
|
+
attr_writer :user_agent
|
10
12
|
|
11
|
-
|
12
|
-
|
13
|
+
def initialize(options = {})
|
14
|
+
options.each do |key, value|
|
15
|
+
instance_variable_set("@#{key}", value)
|
16
|
+
end
|
17
|
+
yield(self) if block_given?
|
18
|
+
end
|
13
19
|
|
14
|
-
|
15
|
-
|
16
|
-
|
20
|
+
def get(path, params)
|
21
|
+
request(:get, path, params)
|
22
|
+
end
|
17
23
|
|
18
|
-
|
19
|
-
|
20
|
-
|
24
|
+
def post(path, params)
|
25
|
+
request(:post, path, params)
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def request(request_method, path, params)
|
31
|
+
@uri = host + prefix + path
|
32
|
+
@request_method = request_method
|
33
|
+
@path = path
|
34
|
+
@params = params
|
35
|
+
@options_key = {get: :params, post: :json}[request_method] || :form
|
36
|
+
@headers = {'Content-Type': 'application/json', 'Accept': '*/*', 'x-ca-timestamp': (Time.now.to_f * 1000).to_i.to_s, 'x-ca-nonce': SecureRandom.uuid, 'x-ca-key': app_key}
|
37
|
+
perform
|
38
|
+
end
|
39
|
+
|
40
|
+
def perform
|
41
|
+
ctx = OpenSSL::SSL::SSLContext.new
|
42
|
+
ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
43
|
+
|
44
|
+
response = http_client.headers(sign_headers(@headers)).public_send(@request_method, @uri.to_s, request_options.merge(ssl_context: ctx))
|
45
|
+
response_body = response.body.empty? ? '' : symbolize_keys!(response.parse)
|
46
|
+
response_headers = response.headers
|
47
|
+
{
|
48
|
+
headers: response_headers,
|
49
|
+
body: response_body,
|
50
|
+
}
|
51
|
+
end
|
52
|
+
|
53
|
+
def symbolize_keys!(object)
|
54
|
+
case object
|
55
|
+
when Array
|
56
|
+
object.each_with_index { |val, index| object[index] = symbolize_keys!(val) }
|
57
|
+
when Hash
|
58
|
+
object.dup.each_key { |key| object[key.to_sym] = symbolize_keys!(object.delete(key)) }
|
59
|
+
end
|
60
|
+
object
|
61
|
+
end
|
62
|
+
|
63
|
+
def timeout_keys_defined
|
64
|
+
(%i[write connect read] - (timeouts&.keys || [])).empty?
|
65
|
+
end
|
66
|
+
|
67
|
+
def http_client
|
68
|
+
client = proxy ? HTTP.via(*proxy.values_at(:host, :port, :username, :password).compact) : HTTP
|
69
|
+
client = client.timeout(connect: timeouts[:connect], read: timeouts[:read], write: timeouts[:write]) if timeout_keys_defined
|
70
|
+
client
|
71
|
+
end
|
72
|
+
|
73
|
+
def request_options
|
74
|
+
{@options_key => @params}
|
75
|
+
end
|
76
|
+
|
77
|
+
def sign_headers(headers)
|
78
|
+
headers.merge!({
|
79
|
+
"x-ca-signature": sign(@request_method, (prefix + @path), headers),
|
80
|
+
})
|
81
|
+
end
|
82
|
+
|
83
|
+
def sign(method, path, headers)
|
84
|
+
sign_str = headers.reject { |a| a.to_s.start_with?('x-ca') }.values.sort
|
85
|
+
sign_str = sign_str.unshift(method.to_s.upcase).push(path).join("\n")
|
21
86
|
|
22
|
-
|
23
|
-
|
87
|
+
Base64.encode64(
|
88
|
+
OpenSSL::HMAC.digest(
|
89
|
+
OpenSSL::Digest.new('sha256'),
|
90
|
+
app_secret,
|
91
|
+
sign_str
|
92
|
+
)
|
93
|
+
).strip
|
94
|
+
end
|
24
95
|
end
|
25
96
|
end
|
data/lib/hik_openapi/version.rb
CHANGED
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hik_openapi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: '0.2'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- palytoxin
|
8
8
|
autorequire:
|
9
|
-
bindir:
|
9
|
+
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
12
|
-
dependencies:
|
11
|
+
date: 2021-05-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: http
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 4.4.1
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 4.4.1
|
13
27
|
description: hikvision openapi for ruby.
|
14
28
|
email:
|
15
29
|
- sxty32@gmail.com
|
@@ -19,16 +33,15 @@ extra_rdoc_files: []
|
|
19
33
|
files:
|
20
34
|
- ".gitignore"
|
21
35
|
- ".rspec"
|
36
|
+
- ".rubocop.yml"
|
22
37
|
- ".travis.yml"
|
23
38
|
- Gemfile
|
24
39
|
- Gemfile.lock
|
40
|
+
- Guardfile
|
25
41
|
- README.md
|
26
42
|
- Rakefile
|
27
43
|
- hik_openapi.gemspec
|
28
44
|
- lib/hik_openapi.rb
|
29
|
-
- lib/hik_openapi/api.rb
|
30
|
-
- lib/hik_openapi/configuration.rb
|
31
|
-
- lib/hik_openapi/result.rb
|
32
45
|
- lib/hik_openapi/version.rb
|
33
46
|
homepage: https://github.com/palytoxin/hik_openapi
|
34
47
|
licenses: []
|
@@ -45,14 +58,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
45
58
|
requirements:
|
46
59
|
- - ">="
|
47
60
|
- !ruby/object:Gem::Version
|
48
|
-
version: 2.
|
61
|
+
version: 2.4.0
|
49
62
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
63
|
requirements:
|
51
64
|
- - ">="
|
52
65
|
- !ruby/object:Gem::Version
|
53
66
|
version: '0'
|
54
67
|
requirements: []
|
55
|
-
rubygems_version: 3.
|
68
|
+
rubygems_version: 3.1.6
|
56
69
|
signing_key:
|
57
70
|
specification_version: 4
|
58
71
|
summary: hikvision openapi for ruby.
|
data/lib/hik_openapi/api.rb
DELETED
@@ -1,78 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'net/http'
|
4
|
-
require 'json'
|
5
|
-
require 'base64'
|
6
|
-
require 'uri'
|
7
|
-
require 'securerandom'
|
8
|
-
require 'openssl'
|
9
|
-
|
10
|
-
module HikOpenapi
|
11
|
-
module Api
|
12
|
-
class << self
|
13
|
-
def get
|
14
|
-
# TODO
|
15
|
-
puts 'WIP'
|
16
|
-
end
|
17
|
-
|
18
|
-
def post(path, body)
|
19
|
-
uri = URI(HikOpenapi.config.host)
|
20
|
-
uri.merge!(path)
|
21
|
-
|
22
|
-
https = Net::HTTP.new(uri.host, uri.port)
|
23
|
-
https.use_ssl = true
|
24
|
-
https.open_timeout = 10
|
25
|
-
https.read_timeout = 10
|
26
|
-
https.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
27
|
-
headers = init_header('POST', uri.path)
|
28
|
-
# headers.transform_values! { |v| v.force_encoding('iso-8859-1') }
|
29
|
-
|
30
|
-
begin
|
31
|
-
request = Net::HTTP::Post.new(uri.request_uri, headers)
|
32
|
-
request.body = body.to_json
|
33
|
-
res = https.request(request)
|
34
|
-
|
35
|
-
result = HikOpenapi::Result.new
|
36
|
-
result.code = res.code
|
37
|
-
result.body = JSON.parse(res.body.force_encoding('utf-8'))
|
38
|
-
result.origin = res
|
39
|
-
result
|
40
|
-
rescue StandardError => e
|
41
|
-
result = HikOpenapi::Result.new
|
42
|
-
result.error = HikOpenapi::Error.new(e)
|
43
|
-
result
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
private
|
48
|
-
|
49
|
-
def init_header(method, path)
|
50
|
-
headers = {
|
51
|
-
'Content-Type': 'application/json',
|
52
|
-
"Accept": '*/*',
|
53
|
-
'x-ca-timestamp': (Time.now.to_f * 1000).to_i.to_s,
|
54
|
-
'x-ca-nonce': SecureRandom.uuid,
|
55
|
-
'x-ca-key': HikOpenapi.config.app_key
|
56
|
-
}
|
57
|
-
|
58
|
-
headers.merge({
|
59
|
-
"x-ca-signature": sign(method, path, headers)
|
60
|
-
})
|
61
|
-
end
|
62
|
-
|
63
|
-
def sign(method, path, headers)
|
64
|
-
sign_str = headers.reject { |a| a.to_s.start_with?('x-ca') }.values.sort
|
65
|
-
sign_str = sign_str.unshift(method).push(path).join("\n")
|
66
|
-
|
67
|
-
enc = Base64.encode64(
|
68
|
-
OpenSSL::HMAC.digest(
|
69
|
-
OpenSSL::Digest.new('sha256'),
|
70
|
-
HikOpenapi.config.app_secret,
|
71
|
-
sign_str
|
72
|
-
)
|
73
|
-
).strip
|
74
|
-
enc
|
75
|
-
end
|
76
|
-
end
|
77
|
-
end
|
78
|
-
end
|