Class: Dingtalk::HttpClient

Inherits:
Object
  • Object
show all
Defined in:
app/apis/dingtalk/http_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHttpClient

Returns a new instance of HttpClient.



8
9
10
# File 'app/apis/dingtalk/http_client.rb', line 8

def initialize
  @http = HTTPX.with(**RailsDingtalk.config.httpx)
end

Instance Attribute Details

#httpObject (readonly)

Returns the value of attribute http.



6
7
8
# File 'app/apis/dingtalk/http_client.rb', line 6

def http
  @http
end

Instance Method Details

#get(path, headers: {}, params: {}, base: nil, **options) ⇒ Object



12
13
14
15
16
17
18
# File 'app/apis/dingtalk/http_client.rb', line 12

def get(path, headers: {}, params: {}, base: nil, **options)
  headers['Accept'] ||= 'application/json'
  url = base + path

  response = @http.with_headers(headers).get(url, params: params)
  parse_response(response, options[:as])
end

#post(path, payload, headers: {}, params: {}, base: nil, **options) ⇒ Object



20
21
22
23
24
25
26
27
# File 'app/apis/dingtalk/http_client.rb', line 20

def post(path, payload, headers: {}, params: {}, base: nil, **options)
  headers['Accept'] ||= 'application/json'
  headers['Content-Type'] ||= 'application/json'
  url = base + path

  response = @http.with_headers(headers).post(url, params: params, body: payload)
  parse_response(response, options[:as])
end

#post_file(path, file, headers: {}, params: {}, base: nil, **options) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/apis/dingtalk/http_client.rb', line 29

def post_file(path, file, headers: {}, params: {}, base: nil, **options)
  headers['Accept'] ||= 'application/json'
  url = base + path

  form_file = file.is_a?(HTTP::FormData::File) ? file : HTTP::FormData::File.new(file)
  response = @http.plugin(:multipart).with_headers(headers).post(
    url,
    params: params,
    form: { media: form_file }
  )
  parse_response(response, options[:as])
end