Class: Nylas::HttpClient

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/nylas/http_client.rb

Overview

Plain HTTP client that can be used to interact with the Nylas API sans any type casting.

Defined Under Namespace

Modules: AuthMethod

Constant Summary collapse

HTTP_SUCCESS_CODES =
[200, 201, 202, 302].freeze
HTTP_CODE_TO_EXCEPTIONS =
{
  400 => InvalidRequest,
  401 => UnauthorizedRequest,
  402 => MessageRejected,
  403 => AccessDenied,
  404 => ResourceNotFound,
  405 => MethodNotAllowed,
  410 => ResourceRemoved,
  418 => TeapotError,
  422 => MailProviderError,
  429 => SendingQuotaExceeded,
  500 => InternalError,
  501 => EndpointNotYetImplemented,
  502 => BadGateway,
  503 => ServiceUnavailable,
  504 => RequestTimedOut
}.freeze
ENDPOINT_TIMEOUTS =
{
  "/oauth/authorize" => 345,
  "/messages/search" => 350,
  "/threads/search" => 350,
  "/delta" => 3650,
  "/delta/longpoll" => 3650,
  "/delta/streaming" => 3650
}.freeze
SUPPORTED_API_VERSION =
"2.5"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

included, level, logger, logger=

Constructor Details

#initialize(app_id:, app_secret:, access_token: nil, api_server: "https://api.nylas.com") ⇒ Nylas::HttpClient

Parameters:

  • app_id (String)

    Your application id from the Nylas Dashboard

  • app_secret (String)

    Your application secret from the Nylas Dashboard

  • access_token (String) (defaults to: nil)

    (Optional) Your users access token.

  • api_server (String) (defaults to: "https://api.nylas.com")

    (Optional) Which Nylas API Server to connect to. Only change this if you're using a self-hosted Nylas instance.



58
59
60
61
62
63
64
65
66
67
# File 'lib/nylas/http_client.rb', line 58

def initialize(app_id:, app_secret:, access_token: nil, api_server: "https://api.nylas.com")
  unless api_server.include?("://")
    raise "When overriding the Nylas API server address, you must include https://"
  end

  @api_server = api_server
  @access_token = access_token
  @app_secret = app_secret
  @app_id = app_id
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



48
49
50
# File 'lib/nylas/http_client.rb', line 48

def access_token
  @access_token
end

#api_serverObject

Returns the value of attribute api_server.



46
47
48
# File 'lib/nylas/http_client.rb', line 46

def api_server
  @api_server
end

#app_idObject (readonly)

Returns the value of attribute app_id.



49
50
51
# File 'lib/nylas/http_client.rb', line 49

def app_id
  @app_id
end

#app_secretObject (readonly)

Returns the value of attribute app_secret.



50
51
52
# File 'lib/nylas/http_client.rb', line 50

def app_secret
  @app_secret
end

#default_headersObject



179
180
181
182
183
184
185
186
187
# File 'lib/nylas/http_client.rb', line 179

def default_headers
  @default_headers ||= {
    "X-Nylas-API-Wrapper" => "ruby",
    "X-Nylas-Client-Id" => @app_id,
    "Nylas-API-Version" => SUPPORTED_API_VERSION,
    "User-Agent" => "Nylas Ruby SDK #{Nylas::VERSION} - #{RUBY_VERSION}",
    "Content-type" => "application/json"
  }
end

Instance Method Details

#as(access_token) ⇒ Nylas::HttpClient[]

Returns Nylas::HttpClient[].

Returns:



70
71
72
73
# File 'lib/nylas/http_client.rb', line 70

def as(access_token)
  HttpClient.new(app_id: app_id, access_token: access_token,
                 app_secret: app_secret, api_server: api_server)
end

#build_request(method:, path: nil, headers: {}, query: {}, payload: nil, timeout: nil, auth_method: nil) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/nylas/http_client.rb', line 119

def build_request(
  method:,
  path: nil,
  headers: {},
  query: {},
  payload: nil,
  timeout: nil,
  auth_method: nil
)
  url ||= url_for_path(path)
  url = add_query_params_to_url(url, query)
  resulting_headers = default_headers.merge(headers).merge(auth_header(auth_method))
  { method: method, url: url, payload: payload, headers: resulting_headers, timeout: timeout }
end

#delete(path: nil, payload: nil, headers: {}, query: {}, auth_method: nil) ⇒ Object

Syntactical sugar for making DELETE requests via the API.

See Also:



168
169
170
171
172
173
174
175
176
177
# File 'lib/nylas/http_client.rb', line 168

def delete(path: nil, payload: nil, headers: {}, query: {}, auth_method: nil)
  execute(
    method: :delete,
    path: path,
    headers: headers,
    query: query,
    payload: payload,
    auth_method: auth_method
  )
end

#execute(method:, path: nil, headers: {}, query: {}, payload: nil, auth_method: nil) ⇒ Array Hash Stringn

Sends a request to the Nylas API and rai rubocop:disable Metrics/MethodLength

Parameters:

  • method (Symbol)

    HTTP method for the API call. Either :get, :post, :delete, or :patch

  • path (String) (defaults to: nil)

    (Optional, defaults to nil) - Relative path from the API Base. Preferred way to execute arbitrary or-not-yet-SDK-ified API commands.

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

    (Optional, defaults to {}) - Additional HTTP headers to include in the payload.

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

    (Optional, defaults to {}) - Hash of names and values to include in the query section of the URI fragment

  • payload (String, Hash) (defaults to: nil)

    (Optional, defaults to nil) - Body to send with the request.

  • auth_method (AuthMethod) (defaults to: nil)

    (Optional, defaults to BEARER) - The authentication method.

Returns:

  • (Array Hash Stringn)


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/nylas/http_client.rb', line 86

def execute(method:, path: nil, headers: {}, query: {}, payload: nil, auth_method: nil)
  timeout = ENDPOINT_TIMEOUTS.fetch(path, 230)
  request = build_request(
    method: method,
    path: path,
    headers: headers,
    query: query,
    payload: payload,
    timeout: timeout,
    auth_method: auth_method || AuthMethod::BEARER
  )
  rest_client_execute(**request) do |response, _request, result|
    content_type = nil

    if response.headers && response.headers[:content_type]
      content_type = response.headers[:content_type].downcase
    end

    begin
      response = parse_response(response) if content_type == "application/json"
    rescue Nylas::JsonParseError
      handle_failed_response(result: result, response: response)
      raise
    end

    handle_failed_response(result: result, response: response)
    response
  end
end

#get(path: nil, headers: {}, query: {}, auth_method: nil) ⇒ Object

Syntactical sugar for making GET requests via the API.

See Also:



136
137
138
# File 'lib/nylas/http_client.rb', line 136

def get(path: nil, headers: {}, query: {}, auth_method: nil)
  execute(method: :get, path: path, query: query, headers: headers, auth_method: auth_method)
end

#parse_response(response) ⇒ Object



189
190
191
192
193
194
195
# File 'lib/nylas/http_client.rb', line 189

def parse_response(response)
  return response if response.is_a?(Enumerable)

  Yajl::Parser.new(symbolize_names: true).parse(response)
rescue Yajl::ParseError
  raise Nylas::JsonParseError
end

#post(path: nil, payload: nil, headers: {}, query: {}, auth_method: nil) ⇒ Object

Syntactical sugar for making POST requests via the API.

See Also:



142
143
144
145
146
147
148
149
150
151
# File 'lib/nylas/http_client.rb', line 142

def post(path: nil, payload: nil, headers: {}, query: {}, auth_method: nil)
  execute(
    method: :post,
    path: path,
    headers: headers,
    query: query,
    payload: payload,
    auth_method: auth_method
  )
end

#put(path: nil, payload:, headers: {}, query: {}, auth_method: nil) ⇒ Object

Syntactical sugar for making PUT requests via the API.

See Also:



155
156
157
158
159
160
161
162
163
164
# File 'lib/nylas/http_client.rb', line 155

def put(path: nil, payload:, headers: {}, query: {}, auth_method: nil)
  execute(
    method: :put,
    path: path,
    headers: headers,
    query: query,
    payload: payload,
    auth_method: auth_method
  )
end

#url_for_path(path) ⇒ Object



198
199
200
201
# File 'lib/nylas/http_client.rb', line 198

def url_for_path(path)
  protocol, domain = api_server.split("//")
  "#{protocol}//#{access_token}:@#{domain}#{path}"
end