# frozen_string_literal: true module Nylas # Methods to retrieve data from the Nylas API as Ruby objects class API attr_accessor :client extend Forwardable def_delegators :client, :execute, :get, :post, :put, :delete, :app_id, :api_server include Logging # @param client [HttpClient] Http Client to use for retrieving data # @param app_id [String] Your application id from the Nylas Dashboard # @param app_secret [String] Your application secret from the Nylas Dashboard # @param access_token [String] (Optional) Your users access token. # @param api_server [String] (Optional) Which Nylas API Server to connect to. Only change this if # you're using a self-hosted Nylas instance. # @return [Nylas::API] def initialize(client: nil, app_id: nil, app_secret: nil, access_token: nil, api_server: "https://api.nylas.com") self.client = client || HttpClient.new(app_id: app_id, app_secret: app_secret, access_token: access_token, api_server: api_server) end # @return [String] A Nylas access token for that particular user. def authenticate(name:, email_address:, provider:, settings:, reauth_account_id: nil, scopes: nil) NativeAuthentication.new(api: self).authenticate( name: name, email_address: email_address, provider: provider, settings: settings, reauth_account_id: reauth_account_id, scopes: scopes ) end def authentication_url(redirect_uri:, scopes:, response_type: "code", login_hint: nil, state: nil) params = { client_id: app_id, redirect_uri: redirect_uri, response_type: response_type, login_hint: login_hint } params[:state] = state if state params[:scopes] = scopes.join(",") if scopes "#{api_server}/oauth/authorize?#{URI.encode_www_form(params)}" end def exchange_code_for_token(code) data = { "client_id" => app_id, "client_secret" => client.app_secret, "grant_type" => "authorization_code", "code" => code } response_json = execute(method: :post, path: "/oauth/token", payload: data) response_json[:access_token] end # @return [Collection] A queryable collection of Contacts def contacts @contacts ||= Collection.new(model: Contact, api: self) end # @return [Collection] A queryable collection of Contact Groups def contact_groups @contact_groups ||= Collection.new(model: ContactGroup, api: self) end # @return [CurrentAccount] The account details for whomevers access token is set def current_account prevent_calling_if_missing_access_token(:current_account) CurrentAccount.from_hash(execute(method: :get, path: "/account"), api: self) end # @return [Collection] A queryable collection of {Account}s def accounts @accounts ||= Collection.new(model: Account, api: as(client.app_secret)) end # @return [Collection] A queryable collection of {Calendar}s def calendars @calendars ||= Collection.new(model: Calendar, api: self) end # @return [DeltasCollection] A queryable collection of Deltas, which are themselves a collection. def deltas @deltas ||= DeltasCollection.new(api: self) end # @return[Collection] A queryable collection of {Draft} objects def drafts @drafts ||= Collection.new(model: Draft, api: self) end # @return [Collection] A queryable collection of {Event}s def events @events ||= EventCollection.new(model: Event, api: self) end # @return [Collection] A queryable collection of {Folder}s def folders @folders ||= Collection.new(model: Folder, api: self) end # @return [Collection] A queryable collection of {File}s def files @files ||= Collection.new(model: File, api: self) end # @return [Collection