browsertrix/backend/test/echo_server.py
Ilya Kreymer 9a67e28f13
Adds Subscription API (#1914)
Fixes https://github.com/webrecorder/browsertrix/issues/1905

- adds a new top-level `/api/subscriptions` endpoint and SubOps handler on
the backend.
- enable subscriptions API endpoints available only if `billing_enabled` is
set in helm chart
- new POST /subscriptions/create, /subscriptions/update,
/subscriptions/cancel API endpoints
- Subscriptions mongo collection storing timestamped /subscription
API events
- GET /subscriptions/events API to get subscription events, support for filtering and sorting
- Subscription data model 
- Support for setting and handling readOnlyOnCancel on org
- /orgs/<id>/billing-portal to lookup portalUrl using external API
- subscription in org getter and list views
- mark org as readOnly for subscription status `paused_payment_failed`, clears it on status `active`

---------
Co-authored-by: Tessa Walsh <tessa@bitarchivist.net>
2024-07-10 17:41:16 -07:00

40 lines
1.1 KiB
Python

#!/usr/bin/env python3
"""
A web server to record POST requests and return them on a GET request
"""
from http.server import HTTPServer, BaseHTTPRequestHandler
import json
BIND_HOST = "0.0.0.0"
PORT = 18080
post_bodies = []
class EchoServerHTTPRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.end_headers()
self.wfile.write(json.dumps({"post_bodies": post_bodies}).encode("utf-8"))
def do_POST(self):
content_length = int(self.headers.get("content-length", 0))
body = self.rfile.read(content_length)
self.send_response(200)
if self.path.endswith("/portalUrl"):
self.send_header("Content-Type", "application/json")
self.end_headers()
self.wfile.write(
json.dumps({"portalUrl": "https://portal.example.com/path/"}).encode(
"utf-8"
)
)
else:
self.end_headers()
post_bodies.append(json.loads(body.decode("utf-8").replace("'", '"')))
httpd = HTTPServer((BIND_HOST, PORT), EchoServerHTTPRequestHandler)
httpd.serve_forever()