msgraph-client 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: deb1e081a5adddf01fa0927f457f1732d39792cb69fbc6de29bd0ce550449677
4
+ data.tar.gz: 951f00a6fabe765fda7541d29863f7e11b0f02ce6f62f9f0cc8b3771a657f115
5
+ SHA512:
6
+ metadata.gz: 758491707e84d1ba0dbfa482d8ceb0bd4400170d51b99097038a4d7c4d8824d1c29b72d02adbf1fa661d24c359cbdd4455d64ef985cb68a904a2032b6a3c7338
7
+ data.tar.gz: 9d8e113d0e6ec50a58cde41422b92d4b7340263dea74e33be39cf6c9b75f59adc94b46ffcc52c12372da4fe8ec1d4d827c61bd3d5f2b97bea0f548e62454e571
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MSGraph
4
+ class Client
5
+ VERSION = '0.0.1'
6
+ end
7
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'net/http'
4
+ require 'json'
5
+
6
+ require 'msgraph/error'
7
+
8
+ module MSGraph
9
+ SCOPE = 'https://graph.microsoft.com/.default'
10
+ NEXT_LINK_KEY = :'@odata.nextLink'
11
+
12
+ # Simple client for Microsoft Graph.
13
+ class Client
14
+ # Create a new client object.
15
+ #
16
+ # @param [Boolean] symbolize_names whether hash keys of API responses are
17
+ # symbolized.
18
+ def initialize(symbolize_names: true)
19
+ @symbolize_names = symbolize_names
20
+ end
21
+
22
+ # @return [Boolean] whether hash keys of API responses are symbolized.
23
+ attr_accessor :symbolize_names
24
+
25
+ # Calls an API and returns the parsed response.
26
+ #
27
+ # @param [String, Symbol] command the HTTP method used on the request.
28
+ # @param [String] path the path of the calling endpoint, and query if
29
+ # necessary.
30
+ # @param [#to_s] token an access token.
31
+ # @param [Object] data an object to be converted to JSON for the request
32
+ # body.
33
+ # @param [Hash] header optional headers.
34
+ def request(command, path, token, data = nil, header = nil)
35
+ req = Net::HTTPGenericRequest.new(
36
+ command.to_s.upcase, !data.nil?, true, url(path), header
37
+ )
38
+ res = http_request(req, token, data)
39
+ raise Error, res unless res.is_a? Net::HTTPSuccess
40
+ raise Error, res unless res.content_type&.start_with? 'application/json'
41
+
42
+ JSON.parse(res.body, symbolize_names: @symbolize_names)
43
+ end
44
+
45
+ private
46
+
47
+ BASE_URL = URI.parse('https://graph.microsoft.com').freeze
48
+ private_constant :BASE_URL
49
+
50
+ def url(path)
51
+ BASE_URL.merge(path)
52
+ end
53
+
54
+ def http_request(request, token, data = nil)
55
+ request['Authorization'] = "Bearer #{token}"
56
+ request['Accept'] = 'application/json'
57
+ if data
58
+ request.content_type = 'application/json; charset=utf-8'
59
+ body = JSON.generate(data)
60
+ else
61
+ body = nil
62
+ end
63
+ http.request(request, body)
64
+ end
65
+
66
+ def http
67
+ @http ||= Net::HTTP.new(BASE_URL.host, BASE_URL.port).tap do |http|
68
+ http.use_ssl = true
69
+ http.min_version = OpenSSL::SSL::TLS1_2_VERSION
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MSGraph
4
+ # Error from Microsoft Graph.
5
+ class Error < StandardError
6
+ # @return [Net::HTTPResponse] the HTTP response.
7
+ attr_reader :response
8
+ # @return [String, Hash] the parsed body of the response in JSON case,
9
+ # otherwise the raw body.
10
+ attr_reader :body
11
+ # @return [Hash] the parsed error property.
12
+ attr_reader :error
13
+
14
+ # @param [Net::HTTPResponse] res the HTTP response
15
+ def initialize(res)
16
+ @response = res
17
+ if res.content_type&.start_with? 'application/json'
18
+ @body = JSON.parse(res.body, symbolize_names: true)
19
+ @error = @body[:error]
20
+ else
21
+ @body = res.body
22
+ end
23
+ super("#{res.code}: #{res.message}\n#{@body}")
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,18 @@
1
+ module MSGraph
2
+ SCOPE: String
3
+ NEXT_LINK_KEY: :"@odata.nextLink"
4
+
5
+ class Client
6
+ BASE_URL: URI::Generic
7
+ @http: Net::HTTP
8
+
9
+ attr_accessor symbolize_names: bool
10
+ def initialize: (?symbolize_names: bool) -> void
11
+ def request: (String | Symbol command, String path, untyped token, ?untyped data, ?Hash[untyped, untyped] header) -> untyped
12
+
13
+ private
14
+ def url: (String path) -> URI::Generic
15
+ def http_request: (Net::HTTPGenericRequest request, untyped token, ?untyped data) -> Net::HTTPResponse
16
+ def http: -> Net::HTTP
17
+ end
18
+ end
@@ -0,0 +1,8 @@
1
+ module MSGraph
2
+ class Error < StandardError
3
+ attr_reader response: Net::HTTPResponse
4
+ attr_reader body: Hash[Symbol, untyped] | String
5
+ attr_reader error: Hash[Symbol, untyped]
6
+ def initialize: (Net::HTTPResponse res) -> void
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: msgraph-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - SHIMAYOSHI, Takao
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-01-18 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Simple Microsoft Graph client gem.
14
+ email:
15
+ - simayosi@cc.kyushu-u.ac.jp
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/msgraph/client.rb
21
+ - lib/msgraph/client/version.rb
22
+ - lib/msgraph/error.rb
23
+ - sig/msgraph/client.rbs
24
+ - sig/msgraph/error.rbs
25
+ homepage: https://github.com/simayosi/rb-msgraph-client
26
+ licenses:
27
+ - MIT
28
+ metadata:
29
+ homepage_uri: https://github.com/simayosi/rb-msgraph-client
30
+ source_code_uri: https://github.com/simayosi/rb-msgraph-client
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: 2.6.0
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubygems_version: 3.0.3.1
47
+ signing_key:
48
+ specification_version: 4
49
+ summary: MSGraph::Client
50
+ test_files: []