Class: Msgraph::Api::Client

Inherits:
MicrosoftGraphMailer::Client show all
Defined in:
lib/msgraph/api/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from MicrosoftGraphMailer::Client

#api_token

Constructor Details

#initialize(settings) ⇒ Client

Returns a new instance of Client.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/msgraph/api/client.rb', line 7

def initialize(settings)
  # Delivery already implements checking setting params and
  # applying defaults if required, so we're gonna abuse that
  # for our implementation.
  completed = MicrosoftGraphMailer::Delivery.new(settings).microsoft_graph_settings
  super(
    user_id: completed[:user_id],
    tenant: completed[:tenant],
    client_id: completed[:client_id],
    client_secret: completed[:client_secret],
    azure_ad_endpoint: completed[:azure_ad_endpoint],
    graph_endpoint: completed[:graph_endpoint]
  )
  self.debug = completed[:debug] == true
end

Instance Attribute Details

#debugObject

Returns the value of attribute debug.



5
6
7
# File 'lib/msgraph/api/client.rb', line 5

def debug
  @debug
end

Instance Method Details

#clientObject



27
28
29
# File 'lib/msgraph/api/client.rb', line 27

def client
  self
end

#meObject



23
24
25
# File 'lib/msgraph/api/client.rb', line 23

def me
  Resource::User.new(self, id: user_id)
end

#request(verb, path_or_url, **options) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/msgraph/api/client.rb', line 47

def request(verb, path_or_url, **options)
  # set content type header if it doesn't exist in options
  options[:headers] ||= {}
  options[:headers]["Content-Type"] ||= "application/json"

  # format body as json document with proper graph attribute names
  options[:body] = options[:body].to_graph_attributes.to_json unless options[:body].nil?

  url = url_for(path_or_url)
  puts "#{verb.to_s.upcase} #{url} ((#{options.inspect})" if self.debug
  rsp = api_token.send(verb, url, options)
  puts rsp.body if self.debug
  rsp
end

#resource_pathObject



35
36
37
# File 'lib/msgraph/api/client.rb', line 35

def resource_path
  ""
end

#url_for(path) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/msgraph/api/client.rb', line 39

def url_for(path)
  if path =~ /https?:\/\//
    path
  else
    graph_endpoint.join_path("v1.0", path)
  end
end

#userObject



31
32
33
# File 'lib/msgraph/api/client.rb', line 31

def user
  me
end