Skip to content

Commit

Permalink
[FIX] manage cookie_jar and default values for header/footer
Browse files Browse the repository at this point in the history
  • Loading branch information
royaurelien committed Aug 21, 2024
1 parent 8d838b7 commit 1a28914
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 16 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,3 @@ Same as wkhtmltopdf, see options : https://wkhtmltopdf.org/usage/wkhtmltopdf.txt
wkhtmltopdf-api --disable-local-file-access --cookie session_id abcd --quiet --page-size A4 --margin-top 40.0 --dpi 90 --zoom 1.0666666666666667 --header-spacing 35 --margin-left 7.0 --margin-bottom 28.0 --margin-right 7.0 --orientation Portrait --header-html /tmp/report.header.tmp.xxx.html --footer-html /tmp/report.footer.tmp.xxx.html /tmp/report.body.tmp.xxx.html /tmp/report.tmp.xxx.pdf

```

19 changes: 16 additions & 3 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import logging
import os
import re
import sys
import requests
from functools import wraps
import time
from functools import wraps
from logging import FileHandler, Formatter, getLogger
from typing import List

from logging import Formatter, getLogger, FileHandler
import requests

_logger = getLogger(__name__)
_logger.setLevel(logging.DEBUG)
Expand All @@ -17,6 +18,8 @@
LOG_FILEPATH = os.path.join(os.path.expanduser("~"), "wkhtmltopdf.log")
REPORT_API_URL = os.getenv("REPORT_API_URL")

SESSION_PATTERN = r"session_id=([a-z0-9]*)"

handler = FileHandler(LOG_FILEPATH)

formatter = Formatter(
Expand Down Expand Up @@ -63,6 +66,8 @@ def removeprefix(value, prefix="--"):
"output": args.pop(),
"header": False,
"footer": False,
"header-html": False,
"footer-html": False,
}
dict_args = {}
first_index, last_index = 0, 0
Expand Down Expand Up @@ -94,6 +99,12 @@ def removeprefix(value, prefix="--"):
else:
dict_args[name] = values

# session_id=af8671bxxxxxxxxxxxxxxxx; HttpOnly; domain=test.com; path=/;
if cookie_jar := dict_args.pop("cookie-jar", None):
with open(cookie_jar, encoding="utf-8") as file:
cookie = re.search(SESSION_PATTERN, file.read().strip()).group(0).split("=")
dict_args["cookie"] = cookie

vals.update(
{
"dict_args": dict_args,
Expand Down Expand Up @@ -160,6 +171,8 @@ def main(args: list = []) -> None:

parsed_args = parse_args(args)

_logger.debug(parsed_args)

header_path = parsed_args["dict_args"].get("header-html", "")
footer_path = parsed_args["dict_args"].get("footer-html", "")
paths = parsed_args.get("bodies", [])
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name="wkhtmltopdf-api",
version="0.0.2",
version="0.1.0",
description="Wkhtmltopdf API Wrapper",
long_description=open("README.md", encoding="utf-8").read(),
long_description_content_type="text/markdown",
Expand Down
4 changes: 1 addition & 3 deletions tests/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import pytest

from app.main import main, parse_args
from app.main import parse_args


class BaseClass:
Expand Down
3 changes: 1 addition & 2 deletions tests/test_0.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import pytest

from app.main import main, parse_args
from base import BaseClass
from app.main import main


def test_01_exit():
Expand Down
3 changes: 0 additions & 3 deletions tests/test_1.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import pytest

from app.main import main, parse_args
from base import BaseClass


Expand Down
3 changes: 0 additions & 3 deletions tests/test_2.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import pytest

from app.main import main, parse_args
from base import BaseClass


Expand Down
60 changes: 60 additions & 0 deletions tests/test_3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import pytest

from app.main import main, parse_args
from base import BaseClass


class TestClass3(BaseClass):
args = [
"--disable-local-file-access",
"--quiet",
"--page-size",
"A4",
"--margin-top",
"40.0",
"--dpi",
"90",
"--zoom",
"1.0666666666666667",
"--header-spacing",
"35",
"--margin-left",
"7.0",
"--margin-bottom",
"32.0",
"--margin-right",
"7.0",
"--orientation",
"Portrait",
"--javascript-delay",
"1000",
"--cookie-jar",
"test_3_cookie.txt",
"--header-html",
"/tmp/report.header.tmp.mhumnhzg.html",
"--footer-html",
"/tmp/report.footer.tmp.rgg3nfk0.html",
"/tmp/report.body.tmp.0.2x3usqzz.html",
"/tmp/output.pdf",
]

dict_args = {
"disable-local-file-access": None,
"quiet": None,
"page-size": "A4",
"margin-top": "40.0",
"dpi": "90",
"zoom": "1.0666666666666667",
"header-spacing": "35",
"margin-left": "7.0",
"margin-bottom": "32.0",
"margin-right": "7.0",
"orientation": "Portrait",
"javascript-delay": "1000",
"header-html": "/tmp/report.header.tmp.mhumnhzg.html",
"footer-html": "/tmp/report.footer.tmp.rgg3nfk0.html",
"cookie": ["session_id", "af8671bxxxxxxxxxxxxxxxx"],
}

output = "/tmp/output.pdf"
bodies = ["/tmp/report.body.tmp.0.2x3usqzz.html"]
1 change: 1 addition & 0 deletions tests/test_3_cookie.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
session_id=af8671bxxxxxxxxxxxxxxxx; HttpOnly; domain=test.com; path=/;

0 comments on commit 1a28914

Please sign in to comment.