|
| 1 | +"""Mock Pretix HTTP Server.""" |
| 2 | + |
| 3 | +import argparse |
| 4 | +import http.server |
| 5 | +import json |
| 6 | +import logging |
| 7 | +import socketserver |
| 8 | +import sys |
| 9 | +from typing import Any |
| 10 | +from urllib.parse import urlparse |
| 11 | + |
| 12 | +logger = logging.getLogger(__name__) |
| 13 | + |
| 14 | +DESCRIPTION = """\ |
| 15 | +Mock Pretix HTTP Server with the following orders: |
| 16 | +
|
| 17 | +Order 'AAAAA' (paid) |
| 18 | +- Business Combined Ticket for 'Jane Doe' |
| 19 | +- Business Tutorial Ticket for 'John Doe' |
| 20 | +- Childcare |
| 21 | +
|
| 22 | +Order 'BBB22' (paid) |
| 23 | +- Volunteer Ticket for 'Minta János' |
| 24 | +- Speaker Ticket for 'Minta Kata' |
| 25 | +- T-Shirt |
| 26 | +
|
| 27 | +Order 'CCC33' (paid) |
| 28 | +- Personal Remote Ticket for 'Martina Mustermann' |
| 29 | +
|
| 30 | +Order 'DDDD44' (paid) |
| 31 | +- Sponsor Ticket for Seán Ó Rudaí |
| 32 | +- T-Shirt |
| 33 | +
|
| 34 | +Order 'EEE55' (payment pending) |
| 35 | +- Personal Late Conference Ticket for 'Numerius Negidius' |
| 36 | +""" |
| 37 | + |
| 38 | +PRETIX_ITEMS = { |
| 39 | + "count": 10, |
| 40 | + "next": None, |
| 41 | + "results": [ |
| 42 | + { |
| 43 | + "id": 1, |
| 44 | + "name": {"en": "Business"}, |
| 45 | + "variations": [ |
| 46 | + {"id": 101, "value": {"en": "Conference"}}, |
| 47 | + {"id": 102, "value": {"en": "Tutorials"}}, |
| 48 | + {"id": 103, "value": {"en": "Combined (Conference + Tutorials)"}}, |
| 49 | + {"id": 104, "value": {"en": "Late Conference"}}, |
| 50 | + {"id": 105, "value": {"en": "Late Combined"}}, |
| 51 | + ], |
| 52 | + }, |
| 53 | + { |
| 54 | + "id": 2, |
| 55 | + "name": {"en": "Personal"}, |
| 56 | + "variations": [ |
| 57 | + {"id": 201, "value": {"en": "Conference"}}, |
| 58 | + {"id": 202, "value": {"en": "Tutorials"}}, |
| 59 | + {"id": 203, "value": {"en": "Combined (Conference + Tutorials)"}}, |
| 60 | + {"id": 204, "value": {"en": "Late Conference"}}, |
| 61 | + {"id": 205, "value": {"en": "Late Combined"}}, |
| 62 | + ], |
| 63 | + }, |
| 64 | + { |
| 65 | + "id": 3, |
| 66 | + "name": {"en": "Education"}, |
| 67 | + "variations": [ |
| 68 | + {"id": 301, "value": {"en": "Conference"}}, |
| 69 | + {"id": 302, "value": {"en": "Tutorials"}}, |
| 70 | + {"id": 303, "value": {"en": "Combined (Conference + Tutorials)"}}, |
| 71 | + ], |
| 72 | + }, |
| 73 | + { |
| 74 | + "id": 4, |
| 75 | + "name": {"en": "Community Contributors"}, |
| 76 | + "variations": [ |
| 77 | + {"id": 401, "value": {"en": "Volunteer"}}, |
| 78 | + {"id": 402, "value": {"en": "Python Community Organiser"}}, |
| 79 | + ], |
| 80 | + }, |
| 81 | + { |
| 82 | + "id": 5, |
| 83 | + "name": {"en": "Childcare (Free for children aged 18 months and older)"}, |
| 84 | + "variations": [], |
| 85 | + }, |
| 86 | + { |
| 87 | + "id": 6, |
| 88 | + "name": {"en": "Presenter"}, |
| 89 | + "variations": [ |
| 90 | + {"id": 601, "value": {"en": "Speaker"}}, |
| 91 | + {"id": 602, "value": {"en": "Tutorial & Workshop Presenter"}}, |
| 92 | + {"id": 603, "value": {"en": "Keynote Presenter"}}, |
| 93 | + ], |
| 94 | + }, |
| 95 | + {"id": 7, "name": {"en": "T-shirt (free)"}, "variations": []}, |
| 96 | + {"id": 8, "name": {"en": "Grant ticket"}, "variations": []}, |
| 97 | + {"id": 9, "name": {"en": "Sponsor Conference Pass"}, "variations": []}, |
| 98 | + { |
| 99 | + "id": 10, |
| 100 | + "name": {"en": "Remote Participation Ticket"}, |
| 101 | + "variations": [ |
| 102 | + {"id": 1001, "value": {"en": "Business Remote"}}, |
| 103 | + {"id": 1002, "value": {"en": "Personal Remote"}}, |
| 104 | + ], |
| 105 | + }, |
| 106 | + ], |
| 107 | +} |
| 108 | +PRETIX_ORDERS = { |
| 109 | + "count": 5, |
| 110 | + "next": None, |
| 111 | + "results": [ |
| 112 | + { |
| 113 | + "code": "AAA11", |
| 114 | + "status": "p", |
| 115 | + "positions": [ |
| 116 | + {"order": "AAA11", "item": 1, "variation": 103, "attendee_name": "Jane Doe"}, |
| 117 | + {"order": "AAA11", "item": 1, "variation": 102, "attendee_name": "John Doe"}, |
| 118 | + {"order": "AAA11", "item": 5, "variation": None, "attendee_name": None}, |
| 119 | + ], |
| 120 | + }, |
| 121 | + { |
| 122 | + "code": "BBB22", |
| 123 | + "status": "p", |
| 124 | + "positions": [ |
| 125 | + {"order": "BBB22", "item": 4, "variation": 401, "attendee_name": "Minta János"}, |
| 126 | + {"order": "BBB22", "item": 6, "variation": 601, "attendee_name": "Minta Kata"}, |
| 127 | + {"order": "BBB22", "item": 7, "variation": None, "attendee_name": None}, |
| 128 | + ], |
| 129 | + }, |
| 130 | + { |
| 131 | + "code": "CCC33", |
| 132 | + "status": "p", |
| 133 | + "positions": [ |
| 134 | + { |
| 135 | + "order": "CCC33", |
| 136 | + "item": 10, |
| 137 | + "variation": 1002, |
| 138 | + "attendee_name": "Martina Mustermann", |
| 139 | + } |
| 140 | + ], |
| 141 | + }, |
| 142 | + { |
| 143 | + "code": "DDD44", |
| 144 | + "status": "p", |
| 145 | + "positions": [ |
| 146 | + {"order": "DDD44", "item": 9, "variation": None, "attendee_name": "Seán Ó Rudaí"}, |
| 147 | + {"order": "DDD44", "item": 7, "variation": None, "attendee_name": None}, |
| 148 | + ], |
| 149 | + }, |
| 150 | + { |
| 151 | + "code": "EEE55", |
| 152 | + "status": "n", |
| 153 | + "positions": [ |
| 154 | + { |
| 155 | + "order": "EEE55", |
| 156 | + "item": 2, |
| 157 | + "variation": 204, |
| 158 | + "attendee_name": "Numerius Negidius", |
| 159 | + } |
| 160 | + ], |
| 161 | + }, |
| 162 | + ], |
| 163 | +} |
| 164 | + |
| 165 | + |
| 166 | +class RequestHandler(http.server.BaseHTTPRequestHandler): |
| 167 | + def do_GET(self) -> None: # noqa: N802 (function name should be lowercase) |
| 168 | + """Handle GET requests.""" |
| 169 | + path = urlparse(self.path).path # strip query parameters |
| 170 | + |
| 171 | + path_to_response_body = { |
| 172 | + "/items.json": PRETIX_ITEMS, |
| 173 | + "/orders.json": PRETIX_ORDERS, |
| 174 | + } |
| 175 | + response_body = path_to_response_body.get(path) |
| 176 | + if response_body is None: |
| 177 | + self.send_error(http.HTTPStatus.NOT_FOUND, "Not Found") |
| 178 | + logger.warning(f"GET {path} - 404") |
| 179 | + else: |
| 180 | + self.send_response(http.HTTPStatus.OK, "OK") |
| 181 | + self.send_header("Content-type", "application/json; charset=utf-8") |
| 182 | + self.end_headers() |
| 183 | + self.wfile.write(json.dumps(response_body).encode("utf-8")) |
| 184 | + logger.info(f"GET {path} - 200") |
| 185 | + |
| 186 | + def log_message(self, *args: Any, **kwargs: Any) -> None: # noqa: ANN401 (typing.Any) |
| 187 | + pass # disable built-in logging |
| 188 | + |
| 189 | + |
| 190 | +def main(args: list[str] | None) -> None: |
| 191 | + parser = argparse.ArgumentParser( |
| 192 | + description=DESCRIPTION, |
| 193 | + formatter_class=argparse.RawTextHelpFormatter, |
| 194 | + ) |
| 195 | + parser.add_argument("--port", type=int, default=8080, help="Port to listen on") |
| 196 | + |
| 197 | + args = parser.parse_args(args) |
| 198 | + |
| 199 | + with socketserver.ThreadingTCPServer(("localhost", args.port), RequestHandler) as httpd: |
| 200 | + logger.info("Serving at localhost:%d", args.port) |
| 201 | + httpd.serve_forever() |
| 202 | + |
| 203 | + |
| 204 | +if __name__ == "__main__": |
| 205 | + logging.basicConfig( |
| 206 | + level=logging.INFO, |
| 207 | + stream=sys.stdout, |
| 208 | + format="%(asctime)s - %(levelname)s - %(message)s", |
| 209 | + ) |
| 210 | + main(sys.argv[1:]) |
0 commit comments