Class: M365ActiveStorage::Http

Inherits:
Object
  • Object
show all
Defined in:
lib/active_storage/http.rb

Overview

HTTP Request Handler

Manages authenticated HTTPS requests to the Microsoft Graph API.

Responsibilities

  • Construct authenticated HTTP requests with OAuth2 tokens

  • Support all HTTP methods (GET, POST, PUT, DELETE, HEAD)

  • Automatically include authorization headers

  • Handle redirect responses

  • Manage SSL/TLS connections

Architecture

The Http class wraps the standard library Net::HTTP with authentication. It automatically:

  1. Ensures a valid OAuth2 token is available

  2. Creates the appropriate HTTP request object

  3. Adds the Authorization header with the Bearer token

  4. Adds any custom headers (Content-Type, etc.)

  5. Executes the request over HTTPS

Example Usage

config = M365ActiveStorage::Configuration.new(**config_params)
auth = M365ActiveStorage::Authentication.new(config)
http = M365ActiveStorage::Http.new(auth)

# GET request
response = http.get("https://graph.microsoft.com/v1.0/sites/site-id")

# PUT request with body
response = http.put(upload_url, file_content, {"Content-Type" => "application/octet-stream"})

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(auth) ⇒ Http

Initialize the HTTP handler

Parameters:



48
49
50
51
# File 'lib/active_storage/http.rb', line 48

def initialize(auth)
  @auth = auth
  @config = auth.config
end

Instance Attribute Details

#authAuthentication (readonly)

The authentication handler for obtaining tokens

Returns:



42
43
44
# File 'lib/active_storage/http.rb', line 42

def auth
  @auth
end

Instance Method Details

#delete(delete_url) ⇒ Net::HTTPResponse

Perform a DELETE request

Sends an HTTPS DELETE request to delete a resource at the specified URL.

Examples:

response = http.delete("https://graph.microsoft.com/v1.0/drives/drive-id/items/file-id")
puts response.code  # => "204"

Parameters:

  • delete_url (String)

    The URL of the resource to delete

Returns:

  • (Net::HTTPResponse)

    The HTTP response object

See Also:

  • #perform_and_request


82
83
84
# File 'lib/active_storage/http.rb', line 82

def delete(delete_url)
  perform_and_request(Net::HTTP::Delete, delete_url)
end

#get(url, options = nil) ⇒ Net::HTTPResponse

Perform a GET request

Sends an HTTPS GET request to retrieve a resource.

Examples:

response = http.get("https://graph.microsoft.com/v1.0/sites/site-id")
response = http.get(url, {"Accept" => "application/json"})

Parameters:

  • url (String)

    The URL to request

  • options (Hash, nil) (defaults to: nil)

    Optional headers to add to the request

Returns:

  • (Net::HTTPResponse)

    The HTTP response object

See Also:

  • #perform_and_request


99
100
101
# File 'lib/active_storage/http.rb', line 99

def get(url, options = nil)
  perform_and_request(Net::HTTP::Get, url, options)
end

#head(check_url) ⇒ Net::HTTPResponse

Perform a HEAD request

Sends an HTTPS HEAD request to the specified URL. HEAD requests are useful for checking resource existence or metadata without downloading the full body.

Examples:

response = http.head("https://graph.microsoft.com/v1.0/sites/site-id")
puts response.code  # => "200"

Parameters:

  • check_url (String)

    The URL to request

Returns:

  • (Net::HTTPResponse)

    The HTTP response object

See Also:

  • #perform_and_request


66
67
68
# File 'lib/active_storage/http.rb', line 66

def head(check_url)
  perform_and_request(Net::HTTP::Head, check_url)
end

#post(url, body, options = nil) ⇒ Net::HTTPResponse

Perform a POST request

Sends an HTTPS POST request to create or update a resource.

Examples:

json_body = {name: "document.pdf"}.to_json
response = http.post(
  "https://graph.microsoft.com/v1.0/drives/drive-id/items",
  json_body,
  {"Content-Type" => "application/json"}
)

Parameters:

  • url (String)

    The URL of the resource

  • body (String)

    The request body (JSON, form data, etc.)

  • options (Hash, nil) (defaults to: nil)

    Optional headers to add to the request

Returns:

  • (Net::HTTPResponse)

    The HTTP response object

See Also:

  • #perform


166
167
168
169
170
# File 'lib/active_storage/http.rb', line 166

def post(url, body, options = nil)
  http, request = perform(Net::HTTP::Post, url, options)
  request.body = body
  http.request(request)
end

#put(url, body, options = nil) ⇒ Net::HTTPResponse

Perform a PUT request

Sends an HTTPS PUT request to upload or update a resource.

Examples:

file_content = File.read("document.pdf")
response = http.put(
  "https://graph.microsoft.com/v1.0/drives/drive-id/root:/file.pdf:/content",
  file_content,
  {"Content-Type" => "application/octet-stream"}
)

Parameters:

  • url (String)

    The URL of the resource

  • body (String)

    The request body (file content, etc.)

  • options (Hash, nil) (defaults to: nil)

    Optional headers to add to the request (should include Content-Type)

Returns:

  • (Net::HTTPResponse)

    The HTTP response object

See Also:

  • #perform


142
143
144
145
146
# File 'lib/active_storage/http.rb', line 142

def put(url, body, options = nil)
  http, request = perform(Net::HTTP::Put, url, options)
  request.body = body
  http.request(request)
end

#redirect_to(url) ⇒ Net::HTTPResponse

Perform a request following a redirect URL without authorization

Sends a GET request to a redirect URL (typically from Microsoft or Azure storage). Explicitly removes the Authorization header to allow following CDN or Azure Blob Storage redirects that would be invalid with a Bearer token.

Examples:

# In follow_redirect after receiving a 302 response
response = http.redirect_to(download_url)

Parameters:

  • url (String)

    The redirect URL to follow

Returns:

  • (Net::HTTPResponse)

    The response from the redirect target

See Also:

  • ActiveStorage::Service::SharepointService#handle_download_response


117
118
119
120
121
122
# File 'lib/active_storage/http.rb', line 117

def redirect_to(url)
  http, request = perform(Net::HTTP::Get, url)
  # do not set authorization header to redirect url from Microsoft, it could redirect to a CDN or Azure blob storage
  request.delete("Authorization")
  http.request(request)
end