feat: Enable user and pass authentification

Token is not the only way to authentificate to gitea. Using credentials
should also be enabled.

When initializing the Gitea object, the token, the auth or none of them
can be provided.
pull/13/head
Etienne Monier 2022-03-08 10:23:01 +01:00
rodzic 41e5364105
commit 69e786cdb2
1 zmienionych plików z 15 dodań i 2 usunięć

Wyświetl plik

@ -19,17 +19,30 @@ class Gitea:
CREATE_ORG = """/admin/users/%s/orgs""" # <username>
CREATE_TEAM = """/orgs/%s/teams""" # <orgname>
def __init__(self, gitea_url: str, token_text: str, log_level="INFO"):
def __init__(
self,
gitea_url: str,
token_text=None,
auth=None,
log_level="INFO"
):
""" Initializing Gitea-instance."""
self.logger = logging.getLogger(__name__)
self.logger.setLevel(log_level)
self.headers = {
"Authorization": "token " + token_text,
"Content-type": "application/json",
}
self.url = gitea_url
self.requests = requests.Session()
# Manage authentification
if not token_text and not auth:
raise ValueError("Please provide auth or token_text, but not both")
if token_text:
self.headers["Authorization"] = "token " + token_text
if auth:
self.requests.auth = tuple(auth.split(":"))
def __get_url(self, endpoint):
url = self.url + "/api/v1" + endpoint
self.logger.debug("Url: %s" % url)