Class: PandaDoc::ApiClient

Inherits:
Object
  • Object
show all
Defined in:
lib/panda_doc/api_client.rb

Overview

This class is responsible for making HTTP requests to the PandaDoc API.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(multipart: false) ⇒ ApiClient

Returns a new instance of ApiClient.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/panda_doc/api_client.rb', line 20

def initialize(multipart: false)
  @url_prefix = "/public/v1"
  @connection = Faraday.new(PandaDoc.configuration.endpoint) do |conn|
    if PandaDoc.configuration.api_key
      conn.request :authorization, "API-Key", PandaDoc.configuration.api_key
    else
      conn.request :authorization, :Bearer, PandaDoc.configuration.access_token
    end

    if multipart
      conn.request     :multipart
      conn.request     :url_encoded
    else
      conn.request     :json
    end

    if PandaDoc.configuration.logger
      conn.response    :logger, PandaDoc.configuration.logger, bodies: true
    end

    conn.response      :json, content_type: /\bjson$/
    conn.adapter       Faraday.default_adapter
  end
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



16
17
18
# File 'lib/panda_doc/api_client.rb', line 16

def connection
  @connection
end

Class Method Details

.request(verb, path, data = {}) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/panda_doc/api_client.rb', line 7

def request(verb, path, data = {})
  if file = data.delete(:file)
    data = { file: file, data: JSON.generate(data) }
  end

  new(multipart: !!file).public_send(verb, path, **data)
end

Instance Method Details

#get(path, data = {}) ⇒ Object



49
50
51
# File 'lib/panda_doc/api_client.rb', line 49

def get(path, data = {})
  connection.get(normalized_path(path), **data)
end

#patch(path, data = {}) ⇒ Object



53
54
55
# File 'lib/panda_doc/api_client.rb', line 53

def patch(path, data = {})
  connection.patch(normalized_path(path), **data)
end

#post(path, data = {}) ⇒ Object



45
46
47
# File 'lib/panda_doc/api_client.rb', line 45

def post(path, data = {})
  connection.post(normalized_path(path), **data)
end