Property definitions

github $ GithubWallet :: defaultinit
# 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