Class: Copilot2gpt::Token
- Inherits:
-
Object
- Object
- Copilot2gpt::Token
- Defined in:
- lib/copilot2gpt/token.rb
Constant Summary collapse
- CACHE =
{}
Instance Attribute Summary collapse
-
#expires_at ⇒ Object
Returns the value of attribute expires_at.
-
#token ⇒ Object
Returns the value of attribute token.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(token, expires_at) ⇒ Token
constructor
A new instance of Token.
Constructor Details
#initialize(token, expires_at) ⇒ Token
Returns a new instance of Token.
12 13 14 15 |
# File 'lib/copilot2gpt/token.rb', line 12 def initialize(token, expires_at) @token = token @expires_at = expires_at end |
Instance Attribute Details
#expires_at ⇒ Object
Returns the value of attribute expires_at.
8 9 10 |
# File 'lib/copilot2gpt/token.rb', line 8 def expires_at @expires_at end |
#token ⇒ Object
Returns the value of attribute token.
8 9 10 |
# File 'lib/copilot2gpt/token.rb', line 8 def token @token end |
Class Method Details
.get_copilot_token(github_token) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/copilot2gpt/token.rb', line 26 def get_copilot_token(github_token) token = get_token_from_cache(github_token) if token.token.empty? get_token_url = "https://api.github.com/copilot_internal/v2/token" uri = URI(get_token_url) req = Net::HTTP::Get.new(uri) req['Authorization'] = "token #{github_token}" res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') {|http| http.request(req) } if res.code != "200" raise "Failed to get copilot token: #{res.code} #{res.body}" end copilot_token = JSON.parse(res.body, object_class: OpenStruct) token.token = copilot_token.token CACHE[github_token] = Token.new(copilot_token.token, copilot_token.expires_at) end token.token end |
.get_token_from_cache(github_token) ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/copilot2gpt/token.rb', line 18 def get_token_from_cache(github_token) extra_time = rand(600) + 300 if CACHE.key?(github_token) && CACHE[github_token].expires_at > Time.now.to_i + extra_time return CACHE[github_token] end Token.new("", 0) end |