-
Notifications
You must be signed in to change notification settings - Fork 3
/
hooks.py
115 lines (77 loc) · 3.55 KB
/
hooks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# Standard Library Imports
from typing import Any, Callable
# Third Party Imports
from google.cloud import scheduler_v1, tasks_v2
from google.protobuf import duration_pb2
DelayedTaskHook = Callable[[tasks_v2.CreateTaskRequest], tasks_v2.CreateTaskRequest]
ScheduledHook = Callable[[scheduler_v1.CreateJobRequest], scheduler_v1.CreateJobRequest]
def noop_hook(request: Any) -> Any:
"""Inspired by https://github.com/kelseyhightower/nocode."""
return request
def chained_hook(*hooks: Callable[[Any], Any]) -> Callable[[Any], Any]:
"""Call all hooks sequentially with the result from the previous hook."""
def chain(request: Any) -> Any:
for hook in hooks:
request = hook(request)
return request
return chain
def oidc_scheduled_hook(token: scheduler_v1.OidcToken) -> ScheduledHook:
"""
Returns a hook for ScheduledRouteBuilder to add OIDC token to all requests.
https://cloud.google.com/scheduler/docs/reference/rpc/google.cloud.scheduler.v1#google.cloud.scheduler.v1.HttpTarget
"""
def add_token(
request: scheduler_v1.CreateJobRequest,
) -> scheduler_v1.CreateJobRequest:
request.job.http_target.oidc_token = token
return request
return add_token
def oidc_delayed_hook(token: tasks_v2.OidcToken) -> DelayedTaskHook:
"""
Returns a hook for DelayedRouteBuilder to add OIDC token to all requests.
https://cloud.google.com/tasks/docs/reference/rpc/google.cloud.tasks.v2#google.cloud.tasks.v2.HttpRequest
"""
def add_token(request: tasks_v2.CreateTaskRequest) -> tasks_v2.CreateTaskRequest:
request.task.http_request.oidc_token = token
return request
return add_token
def oauth_scheduled_hook(token: scheduler_v1.OAuthToken) -> ScheduledHook:
"""
Returns a hook for ScheduledRouteBuilder to add OAuth token to all requests.
https://cloud.google.com/scheduler/docs/reference/rpc/google.cloud.scheduler.v1#google.cloud.scheduler.v1.HttpTarget
"""
def add_token(
request: scheduler_v1.CreateJobRequest,
) -> scheduler_v1.CreateJobRequest:
request.job.http_target.oauth_token = token
return request
return add_token
def oauth_delayed_hook(token: tasks_v2.OAuthToken) -> DelayedTaskHook:
"""
Returns a hook for DelayedRouteBuilder to add OAuth token to all requests.
https://cloud.google.com/tasks/docs/reference/rpc/google.cloud.tasks.v2#google.cloud.tasks.v2.HttpRequest
"""
def add_token(request: tasks_v2.CreateTaskRequest) -> tasks_v2.CreateTaskRequest:
request.task.http_request.oauth_token = token
return request
return add_token
def deadline_scheduled_hook(duration: duration_pb2.Duration) -> ScheduledHook:
"""
Returns a hook for ScheduledRouteBuilder to set Deadline for job execution.
https://cloud.google.com/scheduler/docs/reference/rpc/google.cloud.scheduler.v1#google.cloud.scheduler.v1.Job
"""
def deadline(
request: scheduler_v1.CreateJobRequest,
) -> scheduler_v1.CreateJobRequest:
request.job.attempt_deadline = duration
return request
return deadline
def deadline_delayed_hook(duration: duration_pb2.Duration) -> DelayedTaskHook:
"""
Returns a hook for DelayedRouteBuilder to set Deadline for task execution.
https://cloud.google.com/tasks/docs/reference/rpc/google.cloud.tasks.v2#google.cloud.tasks.v2.Task
"""
def deadline(request: tasks_v2.CreateTaskRequest) -> tasks_v2.CreateTaskRequest:
request.task.dispatch_deadline = duration
return request
return deadline