Skip to content

Commit

Permalink
feat: Add --wns option to handle WNS header
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jrconlin committed Mar 4, 2024
1 parent c158097 commit 804e27f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pywebpush/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
15 changes: 15 additions & 0 deletions pywebpush/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import json
import logging
import math

from requests import JSONDecodeError

Expand All @@ -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",
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 804e27f

Please sign in to comment.