Class: Lich::Util::Update::GitHubClient

Inherits:
Object
  • Object
show all
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.

Examples:

Fetch a GitHub repository's release data

client = GitHubClient.new(cache_ttl: 300)
data = client.fetch_github_json("https://api.github.com/repos/owner/repo/releases")
puts client.last_error.kind if client.last_error

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cache_ttl: 60) ⇒ GitHubClient

Returns a new instance of GitHubClient.

Parameters:

  • cache_ttl (Integer) (defaults to: 60) —

    cache TTL in seconds (default: 60)



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.

Parameters:

Returns:

  • (Hash, Array, nil) —

    parsed JSON or nil on error



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.message}")
    self.last_error = FetchError.new(kind: :bad_response)
    nil
  end
end

#github_token ⇒ String?

Loads GitHub token from DATA_DIR/githubtoken.txt (lazy, once).

Returns:

  • (String, nil) —

    Bearer token header value or nil



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.

Parameters:

  • url (String) —

    target URL

  • auth (Boolean) (defaults to: true) —

    whether to include token auth (default: true)

Returns:

  • (String, nil) —

    response body or nil on error



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.message}")
  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.

Returns:



69
70
71
# File 'documented/common/update/github_client.rb', line 69

def last_error
  Thread.current[@last_error_key]
end