lazop_api_client 1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8002a8fc30db52eb23048bbff30db21f8468aa835f0b97effa7c4de0e81a8a49
4
+ data.tar.gz: c0d86034965199baf659eac96e91bcbaacf5406c0f5178e9410fb3f66fa4f154
5
+ SHA512:
6
+ metadata.gz: 1f90ec2b6e6ebd9cabdc2414b4cccdeb7a3c616d59087a594ef62d38b2347f98e327711095994715450985283b4b91d745e00fe1b66a46fb3c80c1a6cd55d70b
7
+ data.tar.gz: 102f24f21265dda2c06ed5e8a6dd5c6f8b5a8ef4c81fdf4d1cfc8a466af6c538d50afc4b8b6a58ad0e7f03adedef427e26c0710b3d66efd92ce6a218d7f26fc2
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem install rest-client
4
+
5
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 xuteng.xt
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,56 @@
1
+ # LazopApiClient
2
+
3
+ A simple rest API client For Lazop Open Platform for ruby
4
+
5
+
6
+ ## Requirements
7
+
8
+ MRI Ruby 2.0 and newer are supported. Alternative interpreters compatible with
9
+ 2.0+ should work as well.
10
+
11
+ The lazop_api_client gem depends on these other gems for usage at runtime:
12
+
13
+ * [rest-client](http://rubygems.org/gems/rest-client)
14
+
15
+ ## Installation
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+
20
+ ```ruby
21
+ gem 'lazop_api_client'
22
+ ```
23
+
24
+ And then execute:
25
+
26
+ $ bundle
27
+
28
+ Or install it yourself as:
29
+
30
+ $ gem install lazop_api_client
31
+
32
+ ## Usage
33
+
34
+ ```ruby
35
+
36
+ require 'lazop_api_client'
37
+ require 'pp'
38
+
39
+ client = LazopApiClient::Client.new('http://api.lazada.test/rest','576214','gI1PBMAxmsYOfc2NPJztyx9DFS4aMtK6')
40
+ request = LazopApiClient::Request.new('/xiaoxuan/mockfileupload')
41
+ request.add_api_parameter("file_name","pom.xml")
42
+ request.add_file_parameter("file_bytes","/Users/xt/Documents/work/tasp/tasp/pom.xml")
43
+
44
+ response = client.execute(request)
45
+
46
+ puts response.success?
47
+
48
+ pp response
49
+
50
+ ```
51
+
52
+
53
+ ## License
54
+
55
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
56
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,29 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "lazop_api_client/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "lazop_api_client"
8
+ spec.version = LazopApiClient::VERSION
9
+ spec.authors = ["xuteng.xt"]
10
+ spec.email = ["xuteng.xt@alibaba-inc.com"]
11
+
12
+ spec.summary = "Rest API Client For Lazop Open Platform"
13
+ spec.description = "A simple rest API client For Lazop Open Platform for ruby"
14
+ spec.homepage = "http://rubygems.org/gems/lazop_api_client"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = ["lib/lazop_api_client.rb","lib/lazop_api_client/version.rb","Gemfile","lazop_api_client.gemspec","LICENSE.txt","Rakefile","README.md"]
18
+
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.16"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+
26
+ spec.add_runtime_dependency 'rest-client', '~> 2.0', '>= 2.0.2'
27
+ spec.required_ruby_version = '>= 2.0.0'
28
+
29
+ end
@@ -0,0 +1,3 @@
1
+ module LazopApiClient
2
+ VERSION = "1.0.2"
3
+ end
@@ -0,0 +1,256 @@
1
+ #!/usr/bin/ruby
2
+ # -*- coding: UTF-8 -*-
3
+
4
+ require "lazop_api_client/version"
5
+
6
+ require 'rest-client'
7
+ require 'json'
8
+ require 'OpenSSL'
9
+
10
+ module LazopApiClient
11
+
12
+ class Client
13
+
14
+ @serverUrl = nil
15
+ @appkey = nil
16
+ @appSecret = nil
17
+
18
+ def initialize(serverUrl,appkey,appSecret)
19
+ @serverUrl,@appkey,@appSecret = serverUrl,appkey,appSecret
20
+ end
21
+
22
+ def execute(request,accessToken = nil)
23
+
24
+ sys_params = Hash.new
25
+ sys_params[:app_key] = @appkey
26
+
27
+ timestamp = request.timestamp
28
+ if timestamp == nil
29
+ timestamp = (Time.now.to_f * 1000).to_i
30
+ end
31
+ sys_params[:timestamp] = timestamp
32
+
33
+ sys_params[:sign_method] = 'hmac'
34
+
35
+ if accessToken != nil
36
+ sys_params[:access_token] = accessToken
37
+ end
38
+
39
+ sys_params[:sign] = sign_api_request(sys_params,request.api_params)
40
+
41
+ rpcUrl = get_rest_url(@serverUrl,request.api_name)
42
+ fullUrl = get_full_url(rpcUrl,sys_params)
43
+
44
+ obj = nil
45
+ if request.file_params.size() > 0 || request.http_method == 'POST'
46
+ obj = perform_post(fullUrl,request.api_params,request.file_params,request.header_params)
47
+ else
48
+ obj = perform_get(fullUrl,request.api_params,request.header_params)
49
+ end
50
+
51
+ return LazopApiClient::Response.new(obj['type'],obj['code'],obj['message'],obj['request_id'],obj)
52
+ end
53
+
54
+ def perform_get url,api_params,header_params
55
+
56
+ param_str = ''
57
+
58
+ if api_params != nil
59
+ api_params.each do |k,v|
60
+ param_str += '&'
61
+ param_str += k.to_s()
62
+ param_str += '='
63
+ param_str += v.to_s()
64
+ end
65
+ end
66
+
67
+ puts url + param_str
68
+
69
+ res = JSON.parse(RestClient.get(url + param_str, header_params))
70
+
71
+ return res
72
+
73
+ end
74
+
75
+ def perform_post url, api_params,file_params,header_params
76
+
77
+ all_params = api_params
78
+
79
+ if file_params != nil
80
+ file_params.each do |k,v|
81
+ all_params[k] = File.open(v, "rb")
82
+ end
83
+ end
84
+
85
+ res = JSON.parse(RestClient.post(url,all_params))
86
+ return res
87
+ end
88
+
89
+ def sign_api_request(sys_params,api_params)
90
+ sort_arrays = nil
91
+
92
+ if api_params != nil
93
+ sort_arrays = sys_params.merge(api_params).sort_by do |k,v|
94
+ k.to_s()
95
+ end
96
+ else
97
+ sort_arrays = sys_params.sort_by do |k,v|
98
+ k
99
+ end
100
+ end
101
+
102
+ sign_str = ''
103
+ sort_arrays.each do |k,v|
104
+ sign_str += k.to_s()
105
+ sign_str += v.to_s()
106
+ end
107
+
108
+ return OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('md5'), @appSecret, sign_str).upcase
109
+ end
110
+
111
+ def get_rest_url(url,api_name)
112
+ length = url.length()
113
+ if url[length -1] == '/'
114
+ return url + api_name.index('/');
115
+ end
116
+
117
+ return url + api_name;
118
+ end
119
+
120
+ def get_full_url(url,params)
121
+
122
+ full_url = url
123
+ param_str = ''
124
+
125
+ params.each do |k,v|
126
+ if param_str.length() > 0
127
+ param_str += '&'
128
+ end
129
+ param_str += k.to_s()
130
+ param_str += '='
131
+ param_str += v.to_s()
132
+ end
133
+
134
+ full_url += '?'
135
+ full_url += param_str
136
+
137
+ return full_url
138
+ end
139
+ end
140
+
141
+ class Request
142
+ # this hash will hold all api params
143
+ @api_params = nil
144
+ # this hash will hold http header params
145
+ @header_params = nil
146
+ # this hash will hold byte arrays params , such as file path
147
+ @file_params = nil
148
+
149
+ @api_name = nil
150
+ @http_method = 'POST'
151
+ @timestamp = nil
152
+
153
+ def initialize(api_name = nil,http_method = 'POST')
154
+ @api_name , @http_method = api_name,http_method
155
+ @api_params = Hash.new
156
+ @header_params = Hash.new
157
+ @file_params = Hash.new
158
+ end
159
+
160
+ def add_api_parameter(key,value)
161
+ if key.kind_of? String
162
+ @api_params[key] = value
163
+ else
164
+ raise 'api param key is not String'
165
+ end
166
+ end
167
+
168
+ def add_http_parameter(key,value)
169
+ if key.kind_of? String
170
+ @header_params[key] = value
171
+ else
172
+ raise 'http param key is not String'
173
+ end
174
+ end
175
+
176
+ # file_path must be String
177
+ def add_file_parameter(key,file_path)
178
+ if (key.kind_of? String) && (file_path.kind_of? String)
179
+ @file_params[key] = file_path
180
+ else
181
+ raise 'http param key is not String'
182
+ end
183
+ end
184
+
185
+ def timestamp
186
+ @timestamp
187
+ end
188
+
189
+ def set_timestamp=(value)
190
+ @timestamp = value
191
+ end
192
+
193
+ def api_params
194
+ @api_params
195
+ end
196
+
197
+ def api_name
198
+ @api_name
199
+ end
200
+
201
+ def http_method
202
+ @http_method
203
+ end
204
+
205
+ def file_params
206
+ @file_params
207
+ end
208
+
209
+ def header_params
210
+ @header_params
211
+ end
212
+ end
213
+
214
+ class Response
215
+
216
+ def initialize(type,code,message,r_id,body)
217
+ @type = type
218
+ @code = code
219
+ @message = message
220
+ @body = body
221
+ @request_id = r_id
222
+ end
223
+
224
+ def type
225
+ # response type nil,ISP,ISV,SYSTEM
226
+ # nil :no error
227
+ # ISP : API Service Provider Error
228
+ # ISV : API Request Client Error
229
+ # SYSTEM : Lazop platform Error
230
+ @type
231
+ end
232
+
233
+ def code
234
+ # response code, 0 is no error
235
+ @code
236
+ end
237
+
238
+ def message
239
+ @message
240
+ end
241
+
242
+ def body
243
+ # http response body, contains all fileds
244
+ @body
245
+ end
246
+
247
+ def request_id
248
+ # api uniqe request id
249
+ @request_id
250
+ end
251
+
252
+ def success?
253
+ @code == '0'
254
+ end
255
+ end
256
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lazop_api_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.2
5
+ platform: ruby
6
+ authors:
7
+ - xuteng.xt
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-02-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rest-client
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.0'
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: 2.0.2
51
+ type: :runtime
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - "~>"
56
+ - !ruby/object:Gem::Version
57
+ version: '2.0'
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: 2.0.2
61
+ description: A simple rest API client For Lazop Open Platform for ruby
62
+ email:
63
+ - xuteng.xt@alibaba-inc.com
64
+ executables: []
65
+ extensions: []
66
+ extra_rdoc_files: []
67
+ files:
68
+ - Gemfile
69
+ - LICENSE.txt
70
+ - README.md
71
+ - Rakefile
72
+ - lazop_api_client.gemspec
73
+ - lib/lazop_api_client.rb
74
+ - lib/lazop_api_client/version.rb
75
+ homepage: http://rubygems.org/gems/lazop_api_client
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: 2.0.0
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.7.6
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Rest API Client For Lazop Open Platform
99
+ test_files: []