Module: ApiTools::DefaultRestModule
- Included in:
- DefaultRest
- Defined in:
- lib/api_tools/default_rest_module.rb
Instance Method Summary collapse
- #base_params ⇒ Object
- #basic_request(request_dict, user_options) ⇒ Object
- #basic_url ⇒ Object
- #build_similar_get_request(word, path, user_params, user_options) ⇒ Object
- #build_similar_post_request(word, path, user_params, user_options) ⇒ Object
- #build_whole_url(path) ⇒ Object
- #default_options ⇒ Object
Instance Method Details
#base_params ⇒ Object
103 104 105 |
# File 'lib/api_tools/default_rest_module.rb', line 103 def base_params {} # 子类中复写 end |
#basic_request(request_dict, user_options) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/api_tools/default_rest_module.rb', line 62 def basic_request(request_dict, ) exception = nil [:retry_times].times do begin response = ::RestClient::Request.execute(request_dict) return MultiJson.load(response.body, symbolize_keys: true) if [:response_json] return response.body rescue RestClient::Exception => e exception = e next end end puts "Restclient error, body = #{exception.response.body}" if exception.respond_to? :response raise exception unless [:ensure_no_exception] { status: false, message: exception., response_code: exception&.response&.code, response_body: exception&.response&.body } end |
#basic_url ⇒ Object
99 100 101 |
# File 'lib/api_tools/default_rest_module.rb', line 99 def basic_url 'http://www.example.com' # 子类中复写 end |
#build_similar_get_request(word, path, user_params, user_options) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/api_tools/default_rest_module.rb', line 23 def build_similar_get_request(word, path, user_params, ) # 生成类get 请求的URL path_params = URI.encode_www_form(user_params) tmp = path.include?('?') ? '&' : '?' path = path + tmp + path_params url = build_whole_url(path) { method: word, url: url, headers: [:header], timeout: [:timeout] }.merge([:other_base_execute_option]) end |
#build_similar_post_request(word, path, user_params, user_options) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/api_tools/default_rest_module.rb', line 37 def build_similar_post_request(word, path, user_params, ) url = build_whole_url(path) payload = [:params_to_json] ? user_params.to_json : user_params { method: word, url: url, payload: payload, headers: [:header], timeout: [:timeout] }.merge([:other_base_execute_option]) end |
#build_whole_url(path) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/api_tools/default_rest_module.rb', line 49 def build_whole_url(path) web = basic_url return web if path.length.zero? return path if path.start_with?('http') # path 是一个绝对路径 if web[-1] == '/' path = path[1..-1] if path[0] == '/' else path = "/#{path}" if path[0] != '/' end web + path end |
#default_options ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/api_tools/default_rest_module.rb', line 86 def @default_options ||= { timeout: 5, retry_times: 5, response_json: true, params_to_json: true, ensure_no_exception: false, header: { content_type: :json, accept: :json }, other_base_execute_option: {}, exception_with_response: true } end |