Class: ChatSDK::Teams::GraphClient
- Inherits:
-
Object
- Object
- ChatSDK::Teams::GraphClient
- Defined in:
- lib/chat_sdk/teams/graph_client.rb
Constant Summary collapse
- TOKEN_URL_TEMPLATE =
"https://login.microsoftonline.com/%s/oauth2/v2.0/token"- GRAPH_BASE_URL =
"https://graph.microsoft.com/v1.0"- SCOPE =
"https://graph.microsoft.com/.default"
Instance Method Summary collapse
- #fetch_messages(chat_id:, limit: 50) ⇒ Object
- #fetch_token ⇒ Object
-
#initialize(client_id, client_secret, tenant_id) ⇒ GraphClient
constructor
A new instance of GraphClient.
Constructor Details
#initialize(client_id, client_secret, tenant_id) ⇒ GraphClient
Returns a new instance of GraphClient.
10 11 12 13 14 15 16 |
# File 'lib/chat_sdk/teams/graph_client.rb', line 10 def initialize(client_id, client_secret, tenant_id) @client_id = client_id @client_secret = client_secret @tenant_id = tenant_id @access_token = nil @token_expires_at = nil end |
Instance Method Details
#fetch_messages(chat_id:, limit: 50) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/chat_sdk/teams/graph_client.rb', line 18 def (chat_id:, limit: 50) token = fetch_token url = "#{GRAPH_BASE_URL}/chats/#{chat_id}/messages?$top=#{limit}&$orderby=createdDateTime desc" response = api_connection.get(url) do |req| req.headers["Authorization"] = "Bearer #{token}" end unless response.success? raise ChatSDK::PlatformError.new( "Graph API error: #{response.status}", status: response.status, body: response.body, adapter_name: :teams ) end response.body end |
#fetch_token ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/chat_sdk/teams/graph_client.rb', line 38 def fetch_token return @access_token if @access_token && @token_expires_at && Time.now < @token_expires_at token_url = format(TOKEN_URL_TEMPLATE, @tenant_id) response = token_connection.post(token_url, { grant_type: "client_credentials", client_id: @client_id, client_secret: @client_secret, scope: SCOPE }) unless response.success? raise ChatSDK::PlatformError.new( "Failed to acquire Graph API token: #{response.status}", status: response.status, body: response.body, adapter_name: :teams ) end data = JSON.parse(response.body) @access_token = data["access_token"] @token_expires_at = Time.now + (data["expires_in"].to_i - 60) @access_token end |