Github OAuth tokens wallet

Introduced properties

fun add(token: String)

github :: GithubWallet :: add

Add a new token in the wallet
fun api: GithubAPI

github :: GithubWallet :: api

Get an instance of GithubAPI based on the next available token.
fun check_token(token: String): Bool

github :: GithubWallet :: check_token

Check if a token is valid
fun current_token: String

github :: GithubWallet :: current_token

The current token in the tokens array based on current_index
init defaultinit(tokens: nullable Array[String], logger: nullable Logger)

github :: GithubWallet :: defaultinit

fun get_next_token: String

github :: GithubWallet :: get_next_token

Get the next token in token array based on current_token.
fun logger: Logger

github :: GithubWallet :: logger

Logger used to display info about tokens state
fun logger=(logger: nullable Logger)

github :: GithubWallet :: logger=

Logger used to display info about tokens state
fun show_status(no_color: nullable Bool)

github :: GithubWallet :: show_status

Show wallet status in console
fun tokens: Array[String]

github :: GithubWallet :: tokens

Github API tokens
protected fun tokens=(tokens: nullable Array[String])

github :: GithubWallet :: tokens=

Github API tokens

Redefined properties

redef type SELF: GithubWallet

github $ GithubWallet :: SELF

Type of this instance, automatically specialized in every class
redef fun api: GithubAPI

github :: loader $ GithubWallet :: api

Get an instance of GithubAPI based on the next available token.

All properties

fun !=(other: nullable Object): Bool

core :: Object :: !=

Have self and other different values?
fun ==(other: nullable Object): Bool

core :: Object :: ==

Have self and other the same value?
type CLASS: Class[SELF]

core :: Object :: CLASS

The type of the class of self.
type SELF: Object

core :: Object :: SELF

Type of this instance, automatically specialized in every class
fun add(token: String)

github :: GithubWallet :: add

Add a new token in the wallet
fun api: GithubAPI

github :: GithubWallet :: api

Get an instance of GithubAPI based on the next available token.
fun check_token(token: String): Bool

github :: GithubWallet :: check_token

Check if a token is valid
protected fun class_factory(name: String): CLASS

core :: Object :: class_factory

Implementation used by get_class to create the specific class.
fun class_name: String

core :: Object :: class_name

The class name of the object.
fun current_token: String

github :: GithubWallet :: current_token

The current token in the tokens array based on current_index
init defaultinit(tokens: nullable Array[String], logger: nullable Logger)

github :: GithubWallet :: defaultinit

fun get_class: CLASS

core :: Object :: get_class

The meta-object representing the dynamic type of self.
fun get_next_token: String

github :: GithubWallet :: get_next_token

Get the next token in token array based on current_token.
fun hash: Int

core :: Object :: hash

The hash code of the object.
init init

core :: Object :: init

fun inspect: String

core :: Object :: inspect

Developer readable representation of self.
protected fun inspect_head: String

core :: Object :: inspect_head

Return "CLASSNAME:#OBJECTID".
intern fun is_same_instance(other: nullable Object): Bool

core :: Object :: is_same_instance

Return true if self and other are the same instance (i.e. same identity).
fun is_same_serialized(other: nullable Object): Bool

core :: Object :: is_same_serialized

Is self the same as other in a serialization context?
intern fun is_same_type(other: Object): Bool

core :: Object :: is_same_type

Return true if self and other have the same dynamic type.
fun logger: Logger

github :: GithubWallet :: logger

Logger used to display info about tokens state
fun logger=(logger: nullable Logger)

github :: GithubWallet :: logger=

Logger used to display info about tokens state
intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
fun output

core :: Object :: output

Display self on stdout (debug only).
intern fun output_class_name

core :: Object :: output_class_name

Display class name on stdout (debug only).
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
fun show_status(no_color: nullable Bool)

github :: GithubWallet :: show_status

Show wallet status in console
intern fun sys: Sys

core :: Object :: sys

Return the global sys object, the only instance of the Sys class.
abstract fun to_jvalue(env: JniEnv): JValue

core :: Object :: to_jvalue

fun to_s: String

core :: Object :: to_s

User readable representation of self.
fun tokens: Array[String]

github :: GithubWallet :: tokens

Github API tokens
protected fun tokens=(tokens: nullable Array[String])

github :: GithubWallet :: tokens=

Github API tokens
package_diagram github::GithubWallet GithubWallet core::Object Object github::GithubWallet->core::Object

Parents

interface Object

core :: Object

The root of the class hierarchy.

Class definitions

github $ GithubWallet
# Github OAuth tokens wallet
class GithubWallet

	# Github API tokens
	var tokens = new Array[String] is optional

	# Logger used to display info about tokens state
	var logger = new Logger is optional, writable

	# Add a new token in the wallet
	fun add(token: String) do tokens.add token

	# Get an instance of GithubAPI based on the next available token.
	#
	# If no token is found, return an api based on the last exhausted token.
	fun api: GithubAPI do
		var token
		if tokens.is_empty then
			logger.warn "No tokens, using `get_github_oauth`"
			token = get_github_oauth
		else
			token = get_next_token
			var tried = 0
			while not check_token(token) do
				if tried >= tokens.length - 1 then
					logger.warn "Exhausted all tokens, using {token}"
					break
				end
				tried += 1
				token = get_next_token
			end
		end
		var api = new GithubAPI(token)
		api.enable_cache = true
		return api
	end

	# The current index in the `tokens` array
	private var current_index = 0

	# The current token in the `tokens` array based on `current_index`
	fun current_token: String do return tokens[current_index]

	# Get the next token in token `array` based on `current_token`.
	#
	# If the end of the list is reached, start again from the begining.
	fun get_next_token: String do
		if tokens.is_empty then
			return get_github_oauth
		end
		var token = current_token

		if current_index < tokens.length - 1 then
			current_index += 1
		else
			current_index = 0
		end
		return token
	end

	# Check if a token is valid
	fun check_token(token: String): Bool do
		logger.debug "Try token {token}"
		var api = new GithubAPI(token)
		api.get_auth_user
		return not api.was_error
	end

	# Show wallet status in console
	fun show_status(no_color: nullable Bool) do
		no_color = no_color or else false

		if tokens.is_empty then
			print "Wallet is empty"
			return
		end
		print "Wallet ({tokens.length} tokens):"
		for token in tokens do
			var status
			if check_token(token) then
				status = if no_color then "OK" else "OK".green
			else
				status = if no_color then "KO" else "KO".red
			end
			print " * [{status}] {token}"
		end
	end
end
lib/github/wallet.nit:86,1--173,3

github :: loader $ GithubWallet
redef class GithubWallet
	redef fun api do
		var api = super
		api.enable_cache = true
		return api
	end
end
lib/github/loader.nit:192,1--198,3