Class: JPSClient::HttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/jpsclient/http/http_client.rb

Overview

JPS 专用 HTTP 客户端 - 优化版

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url: nil, token: nil, default_timeout: nil, max_retry_times: 3) ⇒ HttpClient

Returns a new instance of HttpClient.

Parameters:

  • base_url (String) (defaults to: nil)

    API 基础 URL

  • token (String) (defaults to: nil)

    认证 token

  • default_timeout (Integer) (defaults to: nil)

    默认超时时间(秒),默认 60 秒

  • max_retry_times (Integer) (defaults to: 3)

    最大重试次数,默认 3



84
85
86
87
88
89
90
# File 'lib/jpsclient/http/http_client.rb', line 84

def initialize(base_url: nil, token: nil, default_timeout: nil, max_retry_times: 3)
  @base_url = base_url
  @token = token
  @default_timeout = default_timeout || 60
  @max_retry_times = max_retry_times || 3
  @connection = nil
end

Instance Attribute Details

#base_urlObject

Returns the value of attribute base_url.



75
76
77
# File 'lib/jpsclient/http/http_client.rb', line 75

def base_url
  @base_url
end

#default_timeoutObject

Returns the value of attribute default_timeout.



77
78
79
# File 'lib/jpsclient/http/http_client.rb', line 77

def default_timeout
  @default_timeout
end

#max_retry_timesObject

Returns the value of attribute max_retry_times.



78
79
80
# File 'lib/jpsclient/http/http_client.rb', line 78

def max_retry_times
  @max_retry_times
end

#tokenObject

Returns the value of attribute token.



76
77
78
# File 'lib/jpsclient/http/http_client.rb', line 76

def token
  @token
end

Instance Method Details

#delete(path, params: nil, timeout: nil) ⇒ Object

DELETE 请求



110
111
112
# File 'lib/jpsclient/http/http_client.rb', line 110

def delete(path, params: nil, timeout: nil)
  request(:delete, path, params: params, timeout: timeout)
end

#get(path, params: nil, timeout: nil) ⇒ Object

GET 请求

Parameters:

  • timeout (Integer, nil) (defaults to: nil)

    超时时间,nil 时使用 default_timeout



94
95
96
# File 'lib/jpsclient/http/http_client.rb', line 94

def get(path, params: nil, timeout: nil)
  request(:get, path, params: params, timeout: timeout)
end

#post(path, body: nil, timeout: nil) ⇒ Object

POST 请求

Parameters:

  • timeout (Integer, nil) (defaults to: nil)

    超时时间,nil 时使用 default_timeout



100
101
102
# File 'lib/jpsclient/http/http_client.rb', line 100

def post(path, body: nil, timeout: nil)
  request(:post, path, body: body, timeout: timeout)
end

#put(path, body: nil, timeout: nil) ⇒ Object

PUT 请求



105
106
107
# File 'lib/jpsclient/http/http_client.rb', line 105

def put(path, body: nil, timeout: nil)
  request(:put, path, body: body, timeout: timeout)
end

#update_token(new_token) ⇒ Object

更新 token



115
116
117
# File 'lib/jpsclient/http/http_client.rb', line 115

def update_token(new_token)
  @token = new_token
end