use session object to allow reusing auth across requests (#14)

This commit is contained in:
Simon Stamm 2022-10-14 19:14:34 +09:00 committed by GitHub
parent e35c4bf0cc
commit 2935251528
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 19 deletions

View File

@ -51,6 +51,7 @@ class MkdocsWithConfluence(BasePlugin):
self.confluence_mistune = mistune.Markdown(renderer=self.confluence_renderer)
self.simple_log = False
self.flen = 1
self.session = requests.Session()
def on_nav(self, nav, config, files):
MkdocsWithConfluence.tab_nav = []
@ -143,8 +144,7 @@ class MkdocsWithConfluence(BasePlugin):
def on_page_markdown(self, markdown, page, config, files):
MkdocsWithConfluence._id += 1
self.pw = self.config["password"]
self.user = self.config["username"]
self.session.auth = (self.config["username"], self.config["password"])
if self.enabled:
if self.simple_log is True:
@ -221,7 +221,7 @@ class MkdocsWithConfluence(BasePlugin):
for match in re.finditer(r"!\[[\w\. -]*\]\((?!http|file)(.*)\)", markdown):
if self.config["debug"]:
print(f"DEBUG - FOUND IMAGE: {match.group(1)}")
attachments.append("docs/" + match.group(1))
attachments.append("docs/" + match.group(1).replace("../", ""))
except AttributeError as e:
if self.config["debug"]:
print(f"DEBUG - WARN(({e}): No images found in markdown. Proceed..")
@ -428,9 +428,8 @@ class MkdocsWithConfluence(BasePlugin):
headers = {"X-Atlassian-Token": "no-check"} # no content-type here!
if self.config["debug"]:
print(f"URL: {url}")
auth = (self.user, self.pw)
r = requests.get(url, headers=headers, params={"filename": name, "expand": "version"}, auth=auth)
r = self.session.get(url, headers=headers, params={"filename": name, "expand": "version"})
r.raise_for_status()
with nostdout():
response_json = r.json()
@ -446,7 +445,6 @@ class MkdocsWithConfluence(BasePlugin):
if self.config["debug"]:
print(f"URL: {url}")
filename = filepath
auth = (self.user, self.pw)
# determine content-type
content_type, encoding = mimetypes.guess_type(filename)
@ -455,7 +453,7 @@ class MkdocsWithConfluence(BasePlugin):
files = {"file": (filename, open(filename, "rb"), content_type), "comment": message}
if not self.dryrun:
r = requests.post(url, headers=headers, files=files, auth=auth)
r = self.session.post(url, headers=headers, files=files)
r.raise_for_status()
print(r.json())
if r.status_code == 200:
@ -472,7 +470,6 @@ class MkdocsWithConfluence(BasePlugin):
if self.config["debug"]:
print(f"URL: {url}")
filename = filepath
auth = (self.user, self.pw)
# determine content-type
content_type, encoding = mimetypes.guess_type(filename)
@ -481,7 +478,7 @@ class MkdocsWithConfluence(BasePlugin):
files = {"file": (filename, open(filename, "rb"), content_type), "comment": message}
if not self.dryrun:
r = requests.post(url, headers=headers, files=files, auth=auth)
r = self.session.post(url, headers=headers, files=files)
print(r.json())
r.raise_for_status()
if r.status_code == 200:
@ -496,8 +493,7 @@ class MkdocsWithConfluence(BasePlugin):
url = self.config["host_url"] + "?title=" + name_confl + "&spaceKey=" + self.config["space"] + "&expand=history"
if self.config["debug"]:
print(f"URL: {url}")
auth = (self.user, self.pw)
r = requests.get(url, auth=auth)
r = self.session.get(url)
r.raise_for_status()
with nostdout():
response_json = r.json()
@ -519,7 +515,6 @@ class MkdocsWithConfluence(BasePlugin):
if self.config["debug"]:
print(f"URL: {url}")
headers = {"Content-Type": "application/json"}
auth = (self.user, self.pw)
space = self.config["space"]
data = {
"type": "page",
@ -531,7 +526,7 @@ class MkdocsWithConfluence(BasePlugin):
if self.config["debug"]:
print(f"DATA: {data}")
if not self.dryrun:
r = requests.post(url, json=data, headers=headers, auth=auth)
r = self.session.post(url, json=data, headers=headers)
r.raise_for_status()
if r.status_code == 200:
if self.config["debug"]:
@ -552,7 +547,6 @@ class MkdocsWithConfluence(BasePlugin):
if self.config["debug"]:
print(f"URL: {url}")
headers = {"Content-Type": "application/json"}
auth = (self.user, self.pw)
space = self.config["space"]
data = {
"id": page_id,
@ -564,7 +558,7 @@ class MkdocsWithConfluence(BasePlugin):
}
if not self.dryrun:
r = requests.put(url, json=data, headers=headers, auth=auth)
r = self.session.put(url, json=data, headers=headers)
r.raise_for_status()
if r.status_code == 200:
if self.config["debug"]:
@ -581,8 +575,7 @@ class MkdocsWithConfluence(BasePlugin):
print(f"INFO - * Mkdocs With Confluence: Find PAGE VERSION, PAGE NAME: {page_name}")
name_confl = page_name.replace(" ", "+")
url = self.config["host_url"] + "?title=" + name_confl + "&spaceKey=" + self.config["space"] + "&expand=version"
auth = (self.user, self.pw)
r = requests.get(url, auth=auth)
r = self.session.get(url)
r.raise_for_status()
with nostdout():
response_json = r.json()
@ -601,8 +594,7 @@ class MkdocsWithConfluence(BasePlugin):
idp = self.find_page_id(name)
url = self.config["host_url"] + "/" + idp + "?expand=ancestors"
auth = (self.user, self.pw)
r = requests.get(url, auth=auth)
r = self.session.get(url)
r.raise_for_status()
with nostdout():
response_json = r.json()