Class: Asana::HttpClient Private

Inherits:
Object
  • Object
show all
Defined in:
lib/asana/http_client.rb,
lib/asana/http_client/response.rb,
lib/asana/http_client/error_handling.rb,
lib/asana/http_client/environment_info.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Wrapper over Faraday that abstracts authentication, request parsing and common options.

Defined Under Namespace

Modules: ErrorHandling Classes: EnvironmentInfo, Response

Constant Summary collapse

BASE_URI =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The API base URI.

'https://app.asana.com/api/1.0'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(authentication: required('authentication'), adapter: nil, user_agent: nil, debug_mode: false, log_asana_change_warnings: true, default_headers: nil, &config) ⇒ HttpClient

Initializes an HttpClient to make requests to the Asana API.

Parameters:

  • authentication (Asana::Authentication) (defaults to: required('authentication'))

    An authentication strategy.

  • adapter (Symbol, Proc) (defaults to: nil)

    A Faraday adapter, eiter a Symbol for registered adapters or a Proc taking a builder for a custom one. Defaults to Faraday.default_adapter.

  • user_agent (String) (defaults to: nil)

    The user agent. Defaults to “ruby-asana vX.Y.Z”.

  • config (Proc)

    An optional block that yields the Faraday builder object for customization.


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/asana/http_client.rb', line 25

def initialize(authentication: required('authentication'),
               adapter: nil,
               user_agent: nil,
               debug_mode: false,
               log_asana_change_warnings: true,
               default_headers: nil,
               &config)
  @authentication             = authentication
  @adapter                    = adapter || Faraday.default_adapter
  @environment_info           = EnvironmentInfo.new(user_agent)
  @debug_mode                 = debug_mode
  @log_asana_change_warnings  = log_asana_change_warnings
  @default_headers            = default_headers
  @config                     = config
end

Instance Method Details

#delete(resource_uri, params: {}, options: {}) ⇒ Asana::HttpClient::Response

Performs a DELETE request against the API.

Parameters:

  • resource_uri (String)

    the resource URI relative to the base Asana API URL, e.g “/tags”.

  • options (Hash) (defaults to: {})

    the request I/O options

Returns:

Raises:


115
116
117
118
119
120
121
122
# File 'lib/asana/http_client.rb', line 115

def delete(resource_uri, params: {}, options: {})
  opts = options.reduce({}) do |acc, (k, v)|
    acc.tap do |hash|
      hash[:"opt_#{k}"] = v.is_a?(Array) ? v.join(',') : v
    end
  end
  perform_request(:delete, resource_uri, params.merge(opts), options[:headers])
end

#get(resource_uri, params: {}, options: {}) ⇒ Asana::HttpClient::Response

Performs a GET request against the API.

Parameters:

  • resource_uri (String)

    the resource URI relative to the base Asana API URL, e.g “/users/me”.

  • params (Hash) (defaults to: {})

    the request parameters

  • options (Hash) (defaults to: {})

    the request I/O options

Returns:

Raises:


50
51
52
53
54
55
56
57
# File 'lib/asana/http_client.rb', line 50

def get(resource_uri, params: {}, options: {})
  opts = options.reduce({}) do |acc, (k, v)|
    acc.tap do |hash|
      hash[:"opt_#{k}"] = v.is_a?(Array) ? v.join(',') : v
    end
  end
  perform_request(:get, resource_uri, params.merge(opts), options[:headers])
end

#post(resource_uri, body: {}, upload: nil, options: {}) ⇒ Asana::HttpClient::Response

Performs a POST request against the API.

Parameters:

  • resource_uri (String)

    the resource URI relative to the base Asana API URL, e.g “/tags”.

  • body (Hash) (defaults to: {})

    the body to POST.

  • upload (Faraday::UploadIO) (defaults to: nil)

    an upload object to post as multipart. Defaults to nil.

  • options (Hash) (defaults to: {})

    the request I/O options

Returns:

Raises:


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/asana/http_client.rb', line 90

def post(resource_uri, body: {}, upload: nil, options: {})
  opts = options.reduce({}) do |acc, (k, v)|
    acc.tap do |hash|
      hash[:"opt_#{k}"] = v.is_a?(Array) ? v.join(',') : v
    end
  end
  options.merge(opts)
  params = { data: body }.merge(options.empty? ? {} : { options: options })
  if upload
    perform_request(:post, resource_uri, params.merge(file: upload), options[:headers]) do |c|
      c.request :multipart
    end
  else
    perform_request(:post, resource_uri, params, options[:headers])
  end
end

#put(resource_uri, body: {}, options: {}) ⇒ Asana::HttpClient::Response

Performs a PUT request against the API.

Parameters:

  • resource_uri (String)

    the resource URI relative to the base Asana API URL, e.g “/users/me”.

  • body (Hash) (defaults to: {})

    the body to PUT.

  • options (Hash) (defaults to: {})

    the request I/O options

Returns:

Raises:


68
69
70
71
72
73
74
75
76
77
# File 'lib/asana/http_client.rb', line 68

def put(resource_uri, body: {}, options: {})
  opts = options.reduce({}) do |acc, (k, v)|
    acc.tap do |hash|
      hash[:"opt_#{k}"] = v.is_a?(Array) ? v.join(',') : v
    end
  end
  options.merge(opts)
  params = { data: body }.merge(options.empty? ? {} : { options: options })
  perform_request(:put, resource_uri, params, options[:headers])
end