X-Git-Url: http://nitlanguage.org diff --git a/lib/github/wallet.nit b/lib/github/wallet.nit index ecad4c2..000d042 100644 --- a/lib/github/wallet.nit +++ b/lib/github/wallet.nit @@ -45,7 +45,7 @@ # or # # ~~~ -# wallet = new GithubWallet.from_tokens(["token 1", "token 2"]) +# wallet = new GithubWallet(["token 1", "token 2"]) # ~~~ # # The `show_status` method can be used to display a summary of the validity of @@ -69,7 +69,7 @@ # instance with a fresh rate limit. # # ~~~ -# wallet = new GithubWallet.from_tokens(["token 1", "token 2"]) +# wallet = new GithubWallet(["token 1", "token 2"]) # var api = wallet.api # ~~~ # @@ -81,22 +81,16 @@ module wallet import github -import console +import logger # Github OAuth tokens wallet class GithubWallet # Github API tokens - var tokens = new Array[String] + var tokens = new Array[String] is optional - # Init `self` from a collection of tokens - init from_tokens(tokens: Collection[String]) do self.tokens.add_all tokens - - # Do not use colors in console output - var no_colors = false is writable - - # Display debug information about the token processing - var verbose = false is writable + # 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 @@ -107,14 +101,14 @@ class GithubWallet fun api: GithubAPI do var token if tokens.is_empty then - message "No tokens, using `get_github_oauth`" + 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 - message "Exhausted all tokens, using {token}" + logger.warn "Exhausted all tokens, using {token}" break end tried += 1 @@ -151,17 +145,16 @@ class GithubWallet # Check if a token is valid fun check_token(token: String): Bool do - message "Try token {token}" + logger.debug "Try token {token}" var api = new GithubAPI(token) - api.get_repo("nitlang/nit") + api.get_auth_user return not api.was_error end - # Print a message depending on `verbose` - fun message(message: String) do if verbose then print "[Github Wallet] {message}" - # Show wallet status in console - fun show_status do + 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 @@ -170,9 +163,9 @@ class GithubWallet for token in tokens do var status if check_token(token) then - status = if no_colors then "OK" else "OK".green + status = if no_color then "OK" else "OK".green else - status = if no_colors then "KO" else "KO".red + status = if no_color then "KO" else "KO".red end print " * [{status}] {token}" end