# 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, return_full_response: false) NativeAuthentication.new(api: self).authenticate( name: name, email_address: email_address, provider: provider, settings: settings, reauth_account_id: reauth_account_id, scopes: scopes, return_full_response: return_full_response ) end def authentication_url(redirect_uri:, scopes:, response_type: "code", login_hint: nil, state: nil, provider: nil, redirect_on_error: nil, disable_provider_selection: 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 params[:provider] = provider if provider params[:redirect_on_error] = redirect_on_error if redirect_on_error params[:disable_provider_selection] = disable_provider_selection if disable_provider_selection "#{api_server}/oauth/authorize?#{URI.encode_www_form(params)}" end # Exchanges an authorization code for an access token # @param code [String] The authorization code to exchange # @param return_full_response [Boolean] If true, returns the full response body instead of just the token # @return [String | Hash] Returns just the access token as a string, or the full response as a hash def exchange_code_for_token(code, return_full_response: false) data = { "client_id" => app_id, "client_secret" => client.app_secret, "grant_type" => "authorization_code", "code" => code } response = execute(method: :post, path: "/oauth/token", payload: data) return_full_response ? response : response[: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 [CalendarCollection] A queryable collection of {Calendar}s def calendars @calendars ||= CalendarCollection.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 [EventCollection] 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