Class: M365ActiveStorage::Http
- Inherits:
-
Object
- Object
- M365ActiveStorage::Http
- 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:
-
Ensures a valid OAuth2 token is available
-
Creates the appropriate HTTP request object
-
Adds the Authorization header with the Bearer token
-
Adds any custom headers (Content-Type, etc.)
-
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
-
#auth ⇒ Authentication
readonly
The authentication handler for obtaining tokens.
Instance Method Summary collapse
-
#delete(delete_url) ⇒ Net::HTTPResponse
Perform a DELETE request.
-
#get(url, options = nil) ⇒ Net::HTTPResponse
Perform a GET request.
-
#head(check_url) ⇒ Net::HTTPResponse
Perform a HEAD request.
-
#initialize(auth) ⇒ Http
constructor
Initialize the HTTP handler.
-
#post(url, body, options = nil) ⇒ Net::HTTPResponse
Perform a POST request.
-
#put(url, body, options = nil) ⇒ Net::HTTPResponse
Perform a PUT request.
-
#redirect_to(url) ⇒ Net::HTTPResponse
Perform a request following a redirect URL without authorization.
Constructor Details
#initialize(auth) ⇒ Http
Initialize the HTTP handler
48 49 50 51 |
# File 'lib/active_storage/http.rb', line 48 def initialize(auth) @auth = auth @config = auth.config end |
Instance Attribute Details
#auth ⇒ Authentication (readonly)
The authentication handler for obtaining tokens
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.
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.
99 100 101 |
# File 'lib/active_storage/http.rb', line 99 def get(url, = nil) perform_and_request(Net::HTTP::Get, url, ) 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.
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.
166 167 168 169 170 |
# File 'lib/active_storage/http.rb', line 166 def post(url, body, = nil) http, request = perform(Net::HTTP::Post, url, ) 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.
142 143 144 145 146 |
# File 'lib/active_storage/http.rb', line 142 def put(url, body, = nil) http, request = perform(Net::HTTP::Put, url, ) 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.
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 |