api_tools 0.0.2 → 0.1.1
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 +5 -5
- data/.gitignore +1 -0
- data/Gemfile +1 -1
- data/README.md +39 -1
- data/api_tools.gemspec +8 -9
- data/lib/api_tools/default_rest.rb +3 -107
- data/lib/api_tools/default_rest_module.rb +107 -0
- data/lib/api_tools/version.rb +3 -1
- metadata +18 -59
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d6e2677fb5e7c1d3b55653927953900e3de618f7bfa764b4bfd187a2c8c5723f
|
4
|
+
data.tar.gz: e66bd28d4572c7f87aa4c0ac70d8b85b1c0240ffa252b2c531af7aa9963734fe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2f381ebbd1f8a1b8463c6a1fd57289288c730fcd9178cf953333ecb223dc2084a4de868baef4c1c36538d3e331c0673cfcd9cd1321b82fde0307dad9e1b7b1c9
|
7
|
+
data.tar.gz: 6888cb1bf2ff0b570c3df8d87707db5b4178e6b2805cce475215c8ded577992e9f84c53611d167915455f408732217eef9ecd5ddf48d787894f69d571e7696d2
|
data/.gitignore
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -7,4 +7,42 @@
|
|
7
7
|
|
8
8
|
|
9
9
|
## 使用
|
10
|
-
|
10
|
+
|
11
|
+
### add to Gemfile
|
12
|
+
gem 'api_tools', :git => 'https://github.com/jicheng1014/api_tools.git'
|
13
|
+
|
14
|
+
## 任意对象
|
15
|
+
|
16
|
+
直接使用 DefaultRest.post(url, xxx)
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
class XXXService < DefaultRest
|
22
|
+
# override(option)
|
23
|
+
def default_options
|
24
|
+
# 默认的参数
|
25
|
+
{
|
26
|
+
timeout: 5,
|
27
|
+
retry_times: 5,
|
28
|
+
response_json: true,
|
29
|
+
params_to_json: true,
|
30
|
+
ensure_no_exception: false,
|
31
|
+
header: { content_type: :json, accept: :json },
|
32
|
+
other_base_execute_option: {},
|
33
|
+
exception_with_response: true
|
34
|
+
}
|
35
|
+
end
|
36
|
+
|
37
|
+
# override (must)
|
38
|
+
def basic_url
|
39
|
+
# 填写基础版本的url
|
40
|
+
end
|
41
|
+
|
42
|
+
#override (option)
|
43
|
+
def base_params
|
44
|
+
# 默认每次提交时候附带的默认参数
|
45
|
+
{}
|
46
|
+
end
|
47
|
+
end
|
48
|
+
```
|
data/api_tools.gemspec
CHANGED
@@ -9,8 +9,8 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.authors = ["atpking"]
|
10
10
|
spec.email = ["atpking@gmail.com"]
|
11
11
|
|
12
|
-
spec.summary = '
|
13
|
-
spec.description = '
|
12
|
+
spec.summary = 'rest 的常用请求'
|
13
|
+
spec.description = '简单点'
|
14
14
|
spec.homepage = "http://jicheng1014.cnblogs.com"
|
15
15
|
spec.license = "MIT"
|
16
16
|
|
@@ -24,13 +24,12 @@ Gem::Specification.new do |spec|
|
|
24
24
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
25
25
|
spec.require_paths = ["lib"]
|
26
26
|
|
27
|
-
spec.add_development_dependency "bundler", "~>
|
28
|
-
spec.add_development_dependency "rake", "~>
|
27
|
+
spec.add_development_dependency "bundler", "~> 2.2"
|
28
|
+
spec.add_development_dependency "rake", "~> 12.3.3"
|
29
29
|
spec.add_development_dependency "rspec", "~> 3.0"
|
30
|
-
spec.add_development_dependency "
|
31
|
-
spec.add_development_dependency "
|
32
|
-
spec.add_development_dependency "webmock"
|
30
|
+
#spec.add_development_dependency "byebug"
|
31
|
+
# spec.add_development_dependency "webmock"
|
33
32
|
|
34
|
-
spec.add_dependency
|
35
|
-
spec.add_dependency "rest-client", '
|
33
|
+
spec.add_dependency 'multi_json'
|
34
|
+
spec.add_dependency "rest-client", '~> 2.0'
|
36
35
|
end
|
@@ -1,114 +1,10 @@
|
|
1
1
|
require_relative '../vendors/hash'
|
2
|
+
require_relative './default_rest_module.rb'
|
2
3
|
require 'json'
|
3
4
|
require 'uri'
|
4
5
|
require 'rest-client'
|
5
|
-
require '
|
6
|
+
require 'multi_json'
|
6
7
|
|
7
8
|
class DefaultRest
|
8
|
-
|
9
|
-
%w[get delete head].each do |word|
|
10
|
-
define_method(word) do |path, params = {}, options = {}|
|
11
|
-
user_params = base_params.merge(params)
|
12
|
-
user_options = default_options.deep_merge(options) # 这里注意一下,是深merge,hash 底下的子hash是merge
|
13
|
-
request_dict = build_similar_get_request(word, path, user_params, user_options)
|
14
|
-
basic_request(request_dict, user_options)
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
%w[post patch put].each do |word|
|
19
|
-
define_method(word) do |path, params = {}, options = {}|
|
20
|
-
user_params = base_params.merge(params)
|
21
|
-
user_options = default_options.deep_merge(options) # 这里注意一下,是深merge,hash 底下的子hash是merge
|
22
|
-
request_dict = build_similar_post_request(word, path, user_params, user_options)
|
23
|
-
basic_request(request_dict, user_options)
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
def build_similar_get_request(word, path, user_params, user_options)
|
28
|
-
# 生成类get 请求的URL
|
29
|
-
path_params = URI.escape(user_params.collect { |k, v| "#{k}=#{v}" }.join('&'))
|
30
|
-
tmp = path.include?('?') ? '&' : '?'
|
31
|
-
path = path + tmp + path_params
|
32
|
-
url = build_whole_url(path)
|
33
|
-
{
|
34
|
-
method: word,
|
35
|
-
url: url,
|
36
|
-
headers: user_options[:header],
|
37
|
-
timeout: user_options[:timeout]
|
38
|
-
}.merge(user_options[:other_base_execute_option])
|
39
|
-
end
|
40
|
-
|
41
|
-
def build_similar_post_request(word, path, user_params, user_options)
|
42
|
-
url = build_whole_url(path)
|
43
|
-
payload = user_options[:params_to_json] ? user_params.to_json : user_params
|
44
|
-
{
|
45
|
-
method: word,
|
46
|
-
url: url,
|
47
|
-
payload: payload,
|
48
|
-
headers: user_options[:header],
|
49
|
-
timeout: user_options[:timeout]
|
50
|
-
}.merge(user_options[:other_base_execute_option])
|
51
|
-
end
|
52
|
-
|
53
|
-
def build_whole_url(path)
|
54
|
-
web = basic_url
|
55
|
-
return web if path.length.zero?
|
56
|
-
return path if path.start_with?("http") # path 是一个绝对路径
|
57
|
-
if web[-1] == "/"
|
58
|
-
path = path[1..-1] if path[0] == "/"
|
59
|
-
else
|
60
|
-
path = "/#{path}" if path[0] != "/"
|
61
|
-
end
|
62
|
-
web + path
|
63
|
-
end
|
64
|
-
|
65
|
-
def basic_request(request_dict, user_options)
|
66
|
-
exception = nil
|
67
|
-
|
68
|
-
user_options[:retry_times].times do
|
69
|
-
begin
|
70
|
-
response = ::RestClient::Request.execute(request_dict)
|
71
|
-
return ::Oj.load(response.body, symbol_keys: true) if user_options[:response_json]
|
72
|
-
return response.body
|
73
|
-
rescue ::RestClient::ExceptionWithResponse => ex
|
74
|
-
raise ex if user_options[:exception_with_response]
|
75
|
-
return {
|
76
|
-
status: false,
|
77
|
-
response_code: ex.response.code,
|
78
|
-
response_body: ex.response.body,
|
79
|
-
message: ex.message
|
80
|
-
}
|
81
|
-
rescue RestClient::Exception => e
|
82
|
-
exception = e
|
83
|
-
next
|
84
|
-
end
|
85
|
-
end
|
86
|
-
raise exception unless user_options[:ensure_no_exception]
|
87
|
-
{
|
88
|
-
status: false,
|
89
|
-
message: ex.message
|
90
|
-
}
|
91
|
-
end
|
92
|
-
|
93
|
-
def default_options
|
94
|
-
@default_options ||= {
|
95
|
-
timeout: 5,
|
96
|
-
retry_times: 5,
|
97
|
-
response_json: true,
|
98
|
-
params_to_json: true,
|
99
|
-
ensure_no_exception: false,
|
100
|
-
header: { content_type: :json, accept: :json },
|
101
|
-
other_base_execute_option: {},
|
102
|
-
exception_with_response: true
|
103
|
-
}
|
104
|
-
end
|
105
|
-
|
106
|
-
def basic_url
|
107
|
-
'http://www.example.com' # 子类中复写
|
108
|
-
end
|
109
|
-
|
110
|
-
def base_params
|
111
|
-
{} # 子类中复写
|
112
|
-
end
|
113
|
-
end
|
9
|
+
extend ApiTools::DefaultRestModule
|
114
10
|
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ApiTools
|
4
|
+
module DefaultRestModule
|
5
|
+
%w[get delete head].each do |word|
|
6
|
+
define_method(word) do |path, params = {}, options = {}|
|
7
|
+
user_params = base_params.merge(params)
|
8
|
+
user_options = default_options.deep_merge(options) # 这里注意一下,是深merge,hash 底下的子hash是merge
|
9
|
+
request_dict = build_similar_get_request(word, path, user_params, user_options)
|
10
|
+
basic_request(request_dict, user_options)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
%w[post patch put].each do |word|
|
15
|
+
define_method(word) do |path, params = {}, options = {}|
|
16
|
+
user_params = base_params.merge(params)
|
17
|
+
user_options = default_options.deep_merge(options) # 这里注意一下,是深merge,hash 底下的子hash是merge
|
18
|
+
request_dict = build_similar_post_request(word, path, user_params, user_options)
|
19
|
+
basic_request(request_dict, user_options)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def build_similar_get_request(word, path, user_params, user_options)
|
24
|
+
# 生成类get 请求的URL
|
25
|
+
path_params = URI.encode_www_form(user_params)
|
26
|
+
tmp = path.include?('?') ? '&' : '?'
|
27
|
+
path = path + tmp + path_params
|
28
|
+
url = build_whole_url(path)
|
29
|
+
{
|
30
|
+
method: word,
|
31
|
+
url: url,
|
32
|
+
headers: user_options[:header],
|
33
|
+
timeout: user_options[:timeout]
|
34
|
+
}.merge(user_options[:other_base_execute_option])
|
35
|
+
end
|
36
|
+
|
37
|
+
def build_similar_post_request(word, path, user_params, user_options)
|
38
|
+
url = build_whole_url(path)
|
39
|
+
payload = user_options[:params_to_json] ? user_params.to_json : user_params
|
40
|
+
{
|
41
|
+
method: word,
|
42
|
+
url: url,
|
43
|
+
payload: payload,
|
44
|
+
headers: user_options[:header],
|
45
|
+
timeout: user_options[:timeout]
|
46
|
+
}.merge(user_options[:other_base_execute_option])
|
47
|
+
end
|
48
|
+
|
49
|
+
def build_whole_url(path)
|
50
|
+
web = basic_url
|
51
|
+
return web if path.length.zero?
|
52
|
+
return path if path.start_with?('http') # path 是一个绝对路径
|
53
|
+
|
54
|
+
if web[-1] == '/'
|
55
|
+
path = path[1..-1] if path[0] == '/'
|
56
|
+
else
|
57
|
+
path = "/#{path}" if path[0] != '/'
|
58
|
+
end
|
59
|
+
web + path
|
60
|
+
end
|
61
|
+
|
62
|
+
def basic_request(request_dict, user_options)
|
63
|
+
exception = nil
|
64
|
+
user_options[:retry_times].times do
|
65
|
+
begin
|
66
|
+
response = ::RestClient::Request.execute(request_dict)
|
67
|
+
return MultiJson.load(response.body, symbolize_keys: true) if user_options[:response_json]
|
68
|
+
|
69
|
+
return response.body
|
70
|
+
rescue RestClient::Exception => e
|
71
|
+
exception = e
|
72
|
+
next
|
73
|
+
end
|
74
|
+
end
|
75
|
+
puts "Restclient error, body = #{exception.response.body}" if exception.respond_to? :response
|
76
|
+
raise exception unless user_options[:ensure_no_exception]
|
77
|
+
|
78
|
+
{
|
79
|
+
status: false,
|
80
|
+
message: exception.message,
|
81
|
+
response_code: exception&.response&.code,
|
82
|
+
response_body: exception&.response&.body
|
83
|
+
}
|
84
|
+
end
|
85
|
+
|
86
|
+
def default_options
|
87
|
+
@default_options ||= {
|
88
|
+
timeout: 5,
|
89
|
+
retry_times: 5,
|
90
|
+
response_json: true,
|
91
|
+
params_to_json: true,
|
92
|
+
ensure_no_exception: false,
|
93
|
+
header: { content_type: :json, accept: :json },
|
94
|
+
other_base_execute_option: {},
|
95
|
+
exception_with_response: true
|
96
|
+
}
|
97
|
+
end
|
98
|
+
|
99
|
+
def basic_url
|
100
|
+
'http://www.example.com' # 子类中复写
|
101
|
+
end
|
102
|
+
|
103
|
+
def base_params
|
104
|
+
{} # 子类中复写
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
data/lib/api_tools/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: api_tools
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- atpking
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-01-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -16,28 +16,28 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '2.2'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '2.2'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: 12.3.3
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
40
|
+
version: 12.3.3
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -53,76 +53,34 @@ dependencies:
|
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '3.0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
56
|
+
name: multi_json
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '0'
|
62
|
-
type: :
|
63
|
-
prerelease: false
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
65
|
-
requirements:
|
66
|
-
- - ">="
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
version: '0'
|
69
|
-
- !ruby/object:Gem::Dependency
|
70
|
-
name: byebug
|
71
|
-
requirement: !ruby/object:Gem::Requirement
|
72
|
-
requirements:
|
73
|
-
- - ">="
|
74
|
-
- !ruby/object:Gem::Version
|
75
|
-
version: '0'
|
76
|
-
type: :development
|
77
|
-
prerelease: false
|
78
|
-
version_requirements: !ruby/object:Gem::Requirement
|
79
|
-
requirements:
|
80
|
-
- - ">="
|
81
|
-
- !ruby/object:Gem::Version
|
82
|
-
version: '0'
|
83
|
-
- !ruby/object:Gem::Dependency
|
84
|
-
name: webmock
|
85
|
-
requirement: !ruby/object:Gem::Requirement
|
86
|
-
requirements:
|
87
|
-
- - ">="
|
88
|
-
- !ruby/object:Gem::Version
|
89
|
-
version: '0'
|
90
|
-
type: :development
|
62
|
+
type: :runtime
|
91
63
|
prerelease: false
|
92
64
|
version_requirements: !ruby/object:Gem::Requirement
|
93
65
|
requirements:
|
94
66
|
- - ">="
|
95
67
|
- !ruby/object:Gem::Version
|
96
68
|
version: '0'
|
97
|
-
- !ruby/object:Gem::Dependency
|
98
|
-
name: oj
|
99
|
-
requirement: !ruby/object:Gem::Requirement
|
100
|
-
requirements:
|
101
|
-
- - ">"
|
102
|
-
- !ruby/object:Gem::Version
|
103
|
-
version: '2.0'
|
104
|
-
type: :runtime
|
105
|
-
prerelease: false
|
106
|
-
version_requirements: !ruby/object:Gem::Requirement
|
107
|
-
requirements:
|
108
|
-
- - ">"
|
109
|
-
- !ruby/object:Gem::Version
|
110
|
-
version: '2.0'
|
111
69
|
- !ruby/object:Gem::Dependency
|
112
70
|
name: rest-client
|
113
71
|
requirement: !ruby/object:Gem::Requirement
|
114
72
|
requirements:
|
115
|
-
- - "
|
73
|
+
- - "~>"
|
116
74
|
- !ruby/object:Gem::Version
|
117
75
|
version: '2.0'
|
118
76
|
type: :runtime
|
119
77
|
prerelease: false
|
120
78
|
version_requirements: !ruby/object:Gem::Requirement
|
121
79
|
requirements:
|
122
|
-
- - "
|
80
|
+
- - "~>"
|
123
81
|
- !ruby/object:Gem::Version
|
124
82
|
version: '2.0'
|
125
|
-
description:
|
83
|
+
description: 简单点
|
126
84
|
email:
|
127
85
|
- atpking@gmail.com
|
128
86
|
executables: []
|
@@ -142,13 +100,14 @@ files:
|
|
142
100
|
- bin/setup
|
143
101
|
- lib/api_tools.rb
|
144
102
|
- lib/api_tools/default_rest.rb
|
103
|
+
- lib/api_tools/default_rest_module.rb
|
145
104
|
- lib/api_tools/version.rb
|
146
105
|
- lib/vendors/hash.rb
|
147
106
|
homepage: http://jicheng1014.cnblogs.com
|
148
107
|
licenses:
|
149
108
|
- MIT
|
150
109
|
metadata: {}
|
151
|
-
post_install_message:
|
110
|
+
post_install_message:
|
152
111
|
rdoc_options: []
|
153
112
|
require_paths:
|
154
113
|
- lib
|
@@ -163,9 +122,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
163
122
|
- !ruby/object:Gem::Version
|
164
123
|
version: '0'
|
165
124
|
requirements: []
|
166
|
-
rubyforge_project:
|
167
|
-
rubygems_version: 2.
|
168
|
-
signing_key:
|
125
|
+
rubyforge_project:
|
126
|
+
rubygems_version: 2.7.7
|
127
|
+
signing_key:
|
169
128
|
specification_version: 4
|
170
|
-
summary:
|
129
|
+
summary: rest 的常用请求
|
171
130
|
test_files: []
|