Skip to content
This repository was archived by the owner on Jul 9, 2025. It is now read-only.

Commit b21d5e1

Browse files
author
Martin Jesper Low Madsen
committed
Adds support for specifying API version
1 parent 52f6ffd commit b21d5e1

File tree

4 files changed

+36
-19
lines changed

4 files changed

+36
-19
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ tokens = context.acquire_token_for_user(resource, client_cred, user_cred)
4646
# add the access token to the request header
4747
callback = Proc.new { |r| r.headers["Authorization"] = "Bearer #{tokens.access_token}" }
4848

49-
graph = MicrosoftGraph.new(
50-
base_url: "https://graph.microsoft.com/v1.0",
51-
cached_metadata_file: File.join(MicrosoftGraph::CACHED_METADATA_DIRECTORY, "metadata_v1.0.xml"),
52-
&callback
49+
graph = MicrosoftGraph.new(base_url: "https://graph.microsoft.com/v1.0",
50+
cached_metadata_file: File.join(MicrosoftGraph::CACHED_METADATA_DIRECTORY, "metadata_v1.0.xml"),
51+
api_version: '1.6', # Optional
52+
&callback
5353
)
5454

5555
me = graph.me # get the current user

lib/microsoft_graph.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ class MicrosoftGraph
1414

1515
def initialize(options = {}, &auth_callback)
1616
@service = OData::Service.new(
17+
api_version: options[:api_version],
18+
auth_callback: auth_callback,
1719
base_url: BASE_URL,
18-
metadata_file: options[:cached_metadata_file],
19-
auth_callback: auth_callback
20+
metadata_file: options[:cached_metadata_file]
2021
)
2122
@association_collections = {}
2223
unless MicrosoftGraph::ClassBuilder.loaded?

lib/microsoft_graph/collection_association.rb

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -191,21 +191,24 @@ def last?
191191

192192
def fetch_next_page
193193
@next_link ||= query_path
194-
result = begin
195-
@graph.service.get(@next_link)
196-
rescue OData::ClientError => e
197-
if matches = /Unsupported sort property '([^']*)'/.match(e.message)
198-
raise MicrosoftGraph::TypeError.new("Cannot sort by #{matches[1]}")
199-
elsif /OrderBy not supported/.match(e.message)
200-
if @order_by.length == 1
201-
raise MicrosoftGraph::TypeError.new("Cannot sort by #{@order_by.first}")
194+
195+
result =
196+
begin
197+
@graph.service.get(@next_link)
198+
rescue OData::ClientError => e
199+
if matches = /Unsupported sort property '([^']*)'/.match(e.message)
200+
raise MicrosoftGraph::TypeError.new("Cannot sort by #{matches[1]}")
201+
elsif /OrderBy not supported/.match(e.message)
202+
if @order_by.length == 1
203+
raise MicrosoftGraph::TypeError.new("Cannot sort by #{@order_by.first}")
204+
else
205+
raise MicrosoftGraph::TypeError.new("Cannot sort by at least one field requested")
206+
end
202207
else
203-
raise MicrosoftGraph::TypeError.new("Cannot sort by at least one field requested")
208+
raise e
204209
end
205-
else
206-
raise e
207210
end
208-
end
211+
209212
@next_link = result[:attributes]['@odata.next_link']
210213
@next_link.sub!(MicrosoftGraph::BASE_URL, "") if @next_link
211214

lib/odata/service.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ class Service
44
attr_reader :metadata
55

66
def initialize(options = {}, &block)
7+
@api_version = {
8+
'api-version': options[:api_version]
9+
} if options[:api_version]
710
@auth_callback = options[:auth_callback] || block
811
@base_url = options[:base_url]
912
@metadata_file = options[:metadata_file]
@@ -64,7 +67,17 @@ def patch(path, data)
6467
end
6568

6669
def request(options = {})
67-
req = Request.new(options[:method], options[:uri], options[:data])
70+
uri = options[:uri]
71+
72+
if @api_version then
73+
parsed_uri = URI(uri)
74+
params = URI.decode_www_form(parsed_uri.query || '')
75+
.concat(@api_version.to_a)
76+
parsed_uri.query = URI.encode_www_form params
77+
uri = parsed_uri.to_s
78+
end
79+
80+
req = Request.new(options[:method], uri, options[:data])
6881
@auth_callback.call(req) if @auth_callback
6982
req.perform
7083
end

0 commit comments

Comments
 (0)