-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcookie.v
98 lines (89 loc) · 2.51 KB
/
cookie.v
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
module cdv
import x.json2 as json
@[params]
pub struct Cookie {
pub mut:
name string
value string
url ?string
domain ?string
path ?string
secure ?bool
http_only ?bool @[json: 'httpOnly']
same_site ?string @[json: 'sameSite']
expires ?i64 @[json: 'expires']
same_party ?bool @[json: 'sameParty']
source_port ?int @[json: 'sourcePort']
partition_key ?map[string]json.Any @[json: 'partitionKey']
}
pub fn (mut page Page) set_cookie(name string, value string, params Cookie) {
cookie := page.struct_to_json_any(Cookie{ ...params, name: name, value: value }).as_map()
page.send_or_noop('Network.setCookie', params: cookie)
}
pub fn (mut page Page) delete_cookie(name string, params Cookie) {
cookie := page.struct_to_json_any(Cookie{ ...params, name: name }).as_map()
page.send_or_noop('Network.deleteCookies', params: cookie)
}
pub fn (mut page Page) clear_browser_cookie() {
page.send_or_noop('Network.clearBrowserCookies')
}
pub fn (mut page Page) set_cookies(cookies []Cookie) {
my_cookies := page.struct_to_json_any(cookies)
page.send_or_noop('Network.setCookies',
params: {
'cookies': my_cookies
}
)
}
@[params]
pub struct GetCookiesParams {
pub:
urls ?[]string
}
pub fn (mut page Page) get_cookies(opts GetCookiesParams) []Cookie {
mut obj := map[string]json.Any{}
if urls := opts.urls {
obj['urls'] = urls.map(json.Any(it))
}
arr := page.send_or_noop('Network.getCookies', params: obj).result['cookies'] or { json.Any{} }.arr()
mut cookies := []Cookie{}
for cookie in arr {
ck_map := cookie.as_map()
mut ck := Cookie{
name: ck_map['name'] or { '' }.str()
value: ck_map['value'] or { '' }.str()
}
if url := ck_map['url'] {
ck.url = url.str()
}
if domain := ck_map['domain'] {
ck.domain = domain.str()
}
if path := ck_map['path'] {
ck.path = path.str()
}
if secure := ck_map['secure'] {
ck.secure = secure.bool()
}
if http_only := ck_map['httpOnly'] {
ck.http_only = http_only.bool()
}
if same_site := ck_map['sameSite'] {
ck.same_site = same_site.str()
}
if expires := ck_map['expires'] {
ck.expires = expires.i64()
}
if same_party := ck_map['sameParty'] {
ck.same_party = same_party.bool()
}
if source_port := ck_map['sourcePort'] {
ck.source_port = source_port.int()
}
if partition_key := ck_map['partitionKey'] {
ck.partition_key = partition_key.as_map()
}
cookies << ck
}
return cookies
}