From 804e27fd6d889f1db30c20d2ec8bfdbbaf3a232c Mon Sep 17 00:00:00 2001 From: jrconlin Date: Mon, 4 Mar 2024 09:52:53 -0800 Subject: [PATCH] feat: Add `--wns` option to handle WNS header Microsoft has introduced [extra header](https://learn.microsoft.com/en-us/windows/apps/design/shell/tiles-and-notifications/push-request-response-headers#request-parameters) requirements for incoming push messages. I kind of want to avoid adding a lot of system specific smarts to pywebpush, mostly because that's an endless road of feature creep. The preferred way to handle this would be to include the extra, call specific headers in the `webpush(..., headers=dict(...))` argument. Closes #162 --- pywebpush/__init__.py | 3 ++- pywebpush/__main__.py | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/pywebpush/__init__.py b/pywebpush/__init__.py index e58cbd3..ff7367d 100644 --- a/pywebpush/__init__.py +++ b/pywebpush/__init__.py @@ -424,9 +424,10 @@ def send(self, *args, **kwargs) -> Union[Response, str]: **params, ) self.verb( - "\nResponse:\n\tcode: {}\n\tbody: {}\n", + "\nResponse:\n\tcode: {}\n\tbody: {}\n\theaders: {}", resp.status_code, resp.text or "Empty", + resp.headers or "None" ) return resp diff --git a/pywebpush/__main__.py b/pywebpush/__main__.py index 0a88859..c2f0270 100644 --- a/pywebpush/__main__.py +++ b/pywebpush/__main__.py @@ -2,6 +2,7 @@ import os import json import logging +import math from requests import JSONDecodeError @@ -15,6 +16,11 @@ def get_config(): parser.add_argument("--head", help="Header Info JSON file") parser.add_argument("--claims", help="Vapid claim file") parser.add_argument("--key", help="Vapid private key file path") + parser.add_argument( + "--wns", + help="Include WNS cache header based on TTL", + default=False, + action="store_true") parser.add_argument( "--curl", help="Don't send, display as curl command", @@ -53,6 +59,15 @@ def get_config(): args.head = json.loads(r.read()) except JSONDecodeError as e: raise WebPushException("Could not read the header arguments: {}", e) + # Set the default "TTL" + args.head["ttl"] = args.head.get("ttl", "0") + if args.wns: + # NOTE: Microsoft also requires `X-WNS-Type` as + # `tile`, `toast`, `badge` or `raw`. This is not provided by this code. + if int(args.head.get("ttl", "0")) > 0: + args.head["x-wns-cache-policy"] = "cache" + else: + args.head["x-wns-cache-policy"] = "no-cache" if args.claims: if not args.key: raise WebPushException("No private --key specified for claims")