Class: AuthingRuby::Common::HttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/authing_ruby/common/HttpClient.rb

Direct Known Subclasses

NaiveHttpClient

Constant Summary collapse

METHODS =
%i[get post put delete]

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, tokenProvider = nil) ⇒ HttpClient

METHODS = %i[get post put delete head patch options trace]



61
62
63
64
# File 'lib/authing_ruby/common/HttpClient.rb', line 61

def initialize(options = {}, tokenProvider = nil)
  @options = options
  @tokenProvider = tokenProvider
end

Instance Method Details

#faraday_conn(config = {}) ⇒ Object

目的: 把 Faraday 稍微封装一下,因为 Faraday 不能动态指定方法(文档里没写),不像 js 的 axios。 github.com/lostisland/faraday/blob/main/lib/faraday/connection.rb www.rubydoc.info/github/lostisland/faraday/Faraday/Connection 返回 <Faraday::Response>



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/authing_ruby/common/HttpClient.rb', line 70

def faraday_conn(config = {})
  method = config.fetch(:method, nil)
  if method == nil
    raise "必须传入 method,可选值 #{METHODS.join(', ')}"
  end
  method_downcase = method.downcase # 转成小写
  if METHODS.include?(method_downcase.to_sym) == false
    raise "传入的 method 错误,可选值 #{METHODS.join(', ')}"
  end

  url = config.fetch(:url, nil)
  data = config.fetch(:data, nil) # data 和 body 一回事
  params = config.fetch(:params, nil)
  headers = config.fetch(:headers, nil)

  if method_downcase == 'get'
    conn = Faraday::Connection.new url
    return conn.get nil, params, headers
  end

  if method_downcase == 'post'
    conn = Faraday::Connection.new url
    return conn.post nil, data, headers
  end

  if method_downcase == 'put'
    conn = Faraday::Connection.new url
    return conn.put nil, data, headers
  end

  if method_downcase == 'delete'
    conn = Faraday::Connection.new url
    return conn.delete nil, params, headers
  end
end

#request(config = {}) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/authing_ruby/common/HttpClient.rb', line 106

def request(config = {})
  # 处理 config 里5个参数:method, url, data, params, headers
  method = config.fetch(:method, nil)
  url = config.fetch(:url, nil)
  data = config.fetch(:data, nil) # data 和 body 一回事
  params = config.fetch(:params, nil)
  headers = {
    'x-authing-sdk-version': "ruby:#{AuthingRuby::VERSION}",
    'x-authing-userpool-id': @options.fetch(:userPoolId, ''),
    'x-authing-request-from': @options.fetch(:requestFrom, 'sdk'),
    'x-authing-app-id': @options.fetch(:appId, ''),
    'x-authing-lang': @options.fetch(:lang, ''),
  };

  # 如果用户传了 authorization 进来。
  config_headers_authorization = config.dig(:headers, :authorization)
  if config_headers_authorization != nil
    headers['Authorization'] = config_headers_authorization
  else
    # 如果用户不传 token,就使用 sdk 自己维护的。
    token = @tokenProvider.getToken() if @tokenProvider
    headers['Authorization'] = "Bearer #{token}" if token
  end

  resp = faraday_conn({
    method: method,
    params: params,
    url: url,
    data: data,
    headers: headers,
  })

  # const { code, message } = data;
  # if (code !== 200) {
  #   this.options.onError(code, message, data.data);
  #   throw new Error(JSON.stringify({ code, message, data: data.data }));
  # }
  # return data.data;

  return resp
end