Skip to content

Commit 804e27f

Browse files
committedMar 4, 2024··
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
1 parent c158097 commit 804e27f

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed
 

‎pywebpush/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -424,9 +424,10 @@ def send(self, *args, **kwargs) -> Union[Response, str]:
424424
**params,
425425
)
426426
self.verb(
427-
"\nResponse:\n\tcode: {}\n\tbody: {}\n",
427+
"\nResponse:\n\tcode: {}\n\tbody: {}\n\theaders: {}",
428428
resp.status_code,
429429
resp.text or "Empty",
430+
resp.headers or "None"
430431
)
431432
return resp
432433

‎pywebpush/__main__.py

+15
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
import json
44
import logging
5+
import math
56

67
from requests import JSONDecodeError
78

@@ -15,6 +16,11 @@ def get_config():
1516
parser.add_argument("--head", help="Header Info JSON file")
1617
parser.add_argument("--claims", help="Vapid claim file")
1718
parser.add_argument("--key", help="Vapid private key file path")
19+
parser.add_argument(
20+
"--wns",
21+
help="Include WNS cache header based on TTL",
22+
default=False,
23+
action="store_true")
1824
parser.add_argument(
1925
"--curl",
2026
help="Don't send, display as curl command",
@@ -53,6 +59,15 @@ def get_config():
5359
args.head = json.loads(r.read())
5460
except JSONDecodeError as e:
5561
raise WebPushException("Could not read the header arguments: {}", e)
62+
# Set the default "TTL"
63+
args.head["ttl"] = args.head.get("ttl", "0")
64+
if args.wns:
65+
# NOTE: Microsoft also requires `X-WNS-Type` as
66+
# `tile`, `toast`, `badge` or `raw`. This is not provided by this code.
67+
if int(args.head.get("ttl", "0")) > 0:
68+
args.head["x-wns-cache-policy"] = "cache"
69+
else:
70+
args.head["x-wns-cache-policy"] = "no-cache"
5671
if args.claims:
5772
if not args.key:
5873
raise WebPushException("No private --key specified for claims")

0 commit comments

Comments
 (0)
Please sign in to comment.