/
chrome-tools.factor
62 lines (54 loc) · 2.11 KB
/
chrome-tools.factor
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
! Copyright (C) 2023 Doug Coleman.
! See https://factorcode.org/license.txt for BSD license.
USING: accessors ascii assocs combinators http http.client json
kernel sequences splitting urls ;
IN: chrome-tools
: trim-squotes ( string -- string' ) [ CHAR: ' = ] trim ;
: trim-dquotes ( string -- string' ) [ CHAR: " = ] trim ;
: parse-key-value ( string -- value key ) trim-squotes ": " split1 swap ;
: trim-curl-command ( string -- string' )
string-lines
[ [ "\\ " member? ] trim-tail ] map
[ [ " " member? ] trim-head ] map ;
: parse-curl-command ( string method -- request )
[ trim-curl-command ]
[ <request> swap >>method ] bi* swap
[
" " split1 swap
{
{ [ dup "curl" = ] [ drop trim-squotes >url '[ _ >>url ] ] }
{ [ dup "-H" = ] [ drop parse-key-value dup "if-modified-since" = [ 2drop f ] [ '[ _ _ set-header ] ] if ] }
{ [ dup "--compressed" = ] [ 2drop f ] }
{ [ dup "--data-raw" = ] [ drop trim-squotes '[ _ >>post-data ] ] }
[ 2drop f ]
} cond
] map [ ] concat-as curry call( -- request ) ;
: curl-get-request ( string -- response data )
"GET" parse-curl-command http-request ;
: curl-post-request ( string -- response data )
"POST" parse-curl-command http-request ;
: parse-fetch-command ( string -- request )
[ blank? ] trim
"fetch(" ?head drop
", " split1 [ <request> swap trim-dquotes >url >>url ] dip
");" ?tail drop json>
[
swap
{
{ "method" [ '[ _ >>method ] ] }
{ "headers" [
[
! Remove this header so we get the data
swap dup "if-modified-since" = [
2drop f
] [
'[ _ _ set-header ]
] if
] { } assoc>map [ ] concat-as
] }
{ "body" [ '[ _ dup json-null = [ drop ] [ >>post-data ] if ] ] }
[ 2drop f ]
} case
] { } assoc>map [ ] concat-as curry call( -- request ) ;
: fetch-request ( string -- response data )
parse-fetch-command http-request ;