Class: Lich::Util::Update::GitHubClient
- Inherits:
-
Object
- Object
- Lich::Util::Update::GitHubClient
- Defined in:
- documented/common/update/github_client.rb
Overview
HTTP client for GitHub API with caching and token authentication.
Fetches JSON from GitHub API endpoints with in-memory caching (TTL-based). Supports optional Bearer token authentication from DATA_DIR/githubtoken.txt. Failed requests do not print; instead they record a classified FetchError in #last_error and write details to the debug log.
Instance Attribute Summary collapse
-
#http_cache ⇒ Object
readonly
Returns the value of attribute http_cache.
Instance Method Summary collapse
-
#fetch_github_json(url) ⇒ Hash, ...
Fetches and parses JSON from GitHub API with caching.
-
#github_token ⇒ String?
Loads GitHub token from DATA_DIR/githubtoken.txt (lazy, once).
-
#http_get(url, auth: true) ⇒ String?
Performs HTTP GET request with optional token auth.
-
#initialize(cache_ttl: 60) ⇒ GitHubClient
constructor
A new instance of GitHubClient.
-
#last_error ⇒ FetchError?
Why the calling thread's most recent request failed, or nil if it succeeded.
Constructor Details
#initialize(cache_ttl: 60) ⇒ GitHubClient
Returns a new instance of GitHubClient.
56 57 58 59 60 61 62 |
# File 'documented/common/update/github_client.rb', line 56 def initialize(cache_ttl: 60) @http_cache = {} @cache_ttl = cache_ttl @github_token = nil @github_token_loaded = false @last_error_key = :"lich5_update_last_error_#{object_id}" end |
Instance Attribute Details
#http_cache ⇒ Object (readonly)
Returns the value of attribute http_cache.
53 54 55 |
# File 'documented/common/update/github_client.rb', line 53 def http_cache @http_cache end |
Instance Method Details
#fetch_github_json(url) ⇒ Hash, ...
Fetches and parses JSON from GitHub API with caching.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'documented/common/update/github_client.rb', line 77 def fetch_github_json(url) now = Time.now.to_i entry = @http_cache[url] if entry && (now - entry[:ts] < @cache_ttl) self.last_error = nil return entry[:data] end begin raw = http_get(url) return nil unless raw data = JSON.parse(raw) @http_cache[url] = { ts: now, data: data } data rescue => e debug_log("could not parse response from #{url}: #{e.}") self.last_error = FetchError.new(kind: :bad_response) nil end end |
#github_token ⇒ String?
Loads GitHub token from DATA_DIR/githubtoken.txt (lazy, once).
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'documented/common/update/github_client.rb', line 133 def github_token return @github_token if @github_token_loaded @github_token_loaded = true token_path = File.join(DATA_DIR, 'githubtoken.txt') return nil unless File.exist?(token_path) token = File.read(token_path).strip if token.empty? respond "[lich5-update: GitHub token file is empty. Using unauthenticated access.]" return nil end @github_token = "Bearer #{token}" end |
#http_get(url, auth: true) ⇒ String?
Performs HTTP GET request with optional token auth.
Never prints. On failure, sets #last_error and returns nil. If GitHub rejects the token (401), retries once anonymously: public repositories do not need a token, so a stale githubtoken.txt should not block updates.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'documented/common/update/github_client.rb', line 107 def http_get(url, auth: true) self.last_error = nil uri = URI.parse(url) token = auth ? github_token : nil response = perform_get(uri, token) if response.code == '401' && token debug_log("HTTP 401 with token fetching #{uri.path}; retrying without token") response = perform_get(uri, nil) token_rejected if response.code == '200' end return response.body if response.code == '200' debug_log("HTTP #{response.code} fetching #{uri.path}") self.last_error = classify(response) nil rescue => e debug_log("network error fetching #{url}: #{e.class}: #{e.}") self.last_error = FetchError.new(kind: :network) nil end |
#last_error ⇒ FetchError?
Why the calling thread's most recent request failed, or nil if it succeeded. Kept per thread: the client is shared, and the login sync runs on its own thread while the user may run lich5-update.
69 70 71 |
# File 'documented/common/update/github_client.rb', line 69 def last_error Thread.current[@last_error_key] end |