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, web_origin: nil) ⇒ 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

  • web_origin (String) (defaults to: nil)

    WEB 域,用于 Origin / Referer 头(BFF CORS 需具体 origin)



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

def initialize(base_url: nil, token: nil, default_timeout: nil, max_retry_times: 3, web_origin: nil)
  @base_url = base_url
  @token = token
  @default_timeout = default_timeout || 60
  @max_retry_times = max_retry_times || 3
  @web_origin = web_origin
  @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 请求



112
113
114
# File 'lib/jpsclient/http/http_client.rb', line 112

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



96
97
98
# File 'lib/jpsclient/http/http_client.rb', line 96

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



102
103
104
# File 'lib/jpsclient/http/http_client.rb', line 102

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

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

PUT 请求



107
108
109
# File 'lib/jpsclient/http/http_client.rb', line 107

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

#update_token(new_token) ⇒ Object

更新 token



117
118
119
# File 'lib/jpsclient/http/http_client.rb', line 117

def update_token(new_token)
  @token = new_token
end