Mercurial > repos > icfp2013
comparison src/requests.tp @ 30:a4bffcd381cd
eval(id) request fixed. Fail gracefully on non-200 http response.
author | William Morgan <bill@mrgn.org> |
---|---|
date | Sat, 10 Aug 2013 20:30:05 -0700 |
parents | 18a043613dae |
children | 8a2925ab0d4a |
comparison
equal
deleted
inserted
replaced
29:3690601c8c70 | 30:a4bffcd381cd |
---|---|
19 | 19 |
20 println <- :str { | 20 println <- :str { |
21 print: str . "\n" | 21 print: str . "\n" |
22 } | 22 } |
23 | 23 |
24 fixArgs <- :args { | |
25 args map: :el { | |
26 "\"0x" . (hex: el) . "\"" | |
27 } | |
28 } | |
29 | |
30 | |
24 evalId <- :id args { | 31 evalId <- :id args { |
32 args <- fixArgs: args | |
25 #{ | 33 #{ |
26 string <- { | 34 string <- { |
27 idStr <- (quote: "id") . ":" . (quote: id) | 35 idStr <- (quote: "id") . ":" . (quote: id) |
28 argsStr <- (quote: "arguments") . ":" . (quote: (strJoin: "," args)) | 36 argsStr <- (quote: "arguments") . ":[" . (strJoin: "," args) . "]" |
29 "{" . idStr . "," . argsStr . "}" | 37 "{" . idStr . "," . argsStr . "}" |
38 } | |
39 sendWithKey <- :key { | |
40 print: "Sending: " . string . "\n" | |
41 cli <- http client: "icfpc2013.cloudapp.net" | |
42 resp <- cli post: string toPath: "/eval?auth=" . key withType: "application/json" | |
43 evalResponse: resp | |
30 } | 44 } |
31 } | 45 } |
32 } | 46 } |
33 | 47 |
34 evalProgram <- :program args { | 48 evalProgram <- :program args { |
35 args <- args map: :el { | 49 args <- fixArgs: args |
36 "\"0x" . (hex: el) . "\"" | |
37 } | |
38 #{ | 50 #{ |
39 string <- { | 51 string <- { |
40 progStr <- (quote: "program") . ":" . (quote: program) | 52 progStr <- (quote: "program") . ":" . (quote: program) |
41 argsStr <- (quote: "arguments") . ":[" . (strJoin: "," args) . "]" | 53 argsStr <- (quote: "arguments") . ":[" . (strJoin: "," args) . "]" |
42 "{" . progStr . "," . argsStr . "}" | 54 "{" . progStr . "," . argsStr . "}" |
67 print: bod . "\n" | 79 print: bod . "\n" |
68 decoded <- json decode: bod | 80 decoded <- json decode: bod |
69 _status <- "error" | 81 _status <- "error" |
70 if: _code = 200 { | 82 if: _code = 200 { |
71 _status <- decoded get: "status" withDefault: "error" | 83 _status <- decoded get: "status" withDefault: "error" |
72 } else: { | 84 if: _status = "ok" { |
73 print: "http response gave error!, code was: " . _code | 85 _outputs <- (decoded get: "outputs" withDefault: #[]) map: :num { |
74 } | 86 (num from: 2) parseHex64 |
75 if: _status = "ok" { | 87 } |
76 _outputs <- (decoded get: "outputs" withDefault: #[]) map: :num { | 88 #{ |
77 (num from: 2) parseHex64 | 89 status <- { "ok" } |
78 } | 90 outputs <- { _outputs } |
79 #{ | 91 string <- { |
80 status <- { "ok" } | 92 str <- "OK:" |
81 outputs <- { _outputs } | 93 foreach: _outputs :idx val { |
82 string <- { | 94 str <- str . "\n" . (string: idx) . ": 0x" . (hex: val) |
83 str <- "OK:" | 95 } |
84 foreach: _outputs :idx val { | 96 str |
85 str <- str . "\n" . (string: idx) . ": 0x" . (hex: val) | |
86 } | 97 } |
87 str | 98 print <- { |
99 print: string . "\n" | |
100 } | |
88 } | 101 } |
89 print <- { | 102 } else: { |
90 print: string . "\n" | 103 _message <- decoded get: "message" withDefault: "" |
104 #{ | |
105 status <- { "error" } | |
106 message <- { _message } | |
107 string <- { | |
108 "Error: " . _message | |
109 } | |
110 print <- { | |
111 print: string . "\n" | |
112 } | |
91 } | 113 } |
92 } | 114 } |
93 } else: { | 115 } else: { |
94 _message <- decoded get: "message" withDefault: "" | 116 #{print <- print: "HTTP response gave error! code was: " . _code . "\n"} |
95 #{ | 117 |
96 status <- { "error" } | |
97 message <- { _message } | |
98 string <- { | |
99 "Error: " . _message | |
100 } | |
101 print <- { | |
102 print: string . "\n" | |
103 } | |
104 } | |
105 } | 118 } |
106 } | 119 } |
107 | 120 |
108 guessResponse <- :httpResp { | 121 guessResponse <- :httpResp { |
109 _code <- httpResp statusCode | 122 _code <- httpResp statusCode |
112 print: bod . "\n" | 125 print: bod . "\n" |
113 decoded <- json decode: bod | 126 decoded <- json decode: bod |
114 _status <- "error" | 127 _status <- "error" |
115 if: _code = 200 { | 128 if: _code = 200 { |
116 _status <- decoded get: "status" withDefault: "error" | 129 _status <- decoded get: "status" withDefault: "error" |
130 if: _status = "win" { | |
131 #{ | |
132 status <- { "win" } | |
133 string <- { "OK: win" } | |
134 print <- { | |
135 print: string . "\n" | |
136 } | |
137 } | |
138 } else: { if: _status = "mismatch" { | |
139 | |
140 _values <- (decoded get: "values" withDefault: #[]) map: :num { | |
141 (num from: 2) parseHex64 | |
142 } | |
143 #{ | |
144 status <- { "mismatch" } | |
145 values <- { _values } | |
146 string <- { | |
147 str <- "OK:" | |
148 foreach: _values :idx val { | |
149 str <- str . "\n" . (string: idx) . ": 0x" . (hex: val) | |
150 } | |
151 str | |
152 } | |
153 print <- { | |
154 print: string . "\n" | |
155 } | |
156 } | |
157 } else: { | |
158 _message <- decoded get: "message" withDefault: "" | |
159 #{ | |
160 status <- { "error" } | |
161 message <- { _message } | |
162 string <- { | |
163 "Error: " . _message | |
164 } | |
165 print <- { | |
166 print: string . "\n" | |
167 } | |
168 } | |
169 }} // end if | |
117 } else: { | 170 } else: { |
118 print: "http response gave error!, code was: " . _code | 171 #{print <- print: "HTTP response gave error! code was: " . _code . "\n"} |
119 } | 172 } |
120 if: _status = "win" { | |
121 #{ | |
122 status <- { "win" } | |
123 string <- { "OK: win" } | |
124 print <- { | |
125 print: string . "\n" | |
126 } | |
127 } | |
128 } else: { if: _status = "mismatch" { | |
129 | |
130 _values <- (decoded get: "values" withDefault: #[]) map: :num { | |
131 (num from: 2) parseHex64 | |
132 } | |
133 #{ | |
134 status <- { "mismatch" } | |
135 values <- { _values } | |
136 string <- { | |
137 str <- "OK:" | |
138 foreach: _values :idx val { | |
139 str <- str . "\n" . (string: idx) . ": 0x" . (hex: val) | |
140 } | |
141 str | |
142 } | |
143 print <- { | |
144 print: string . "\n" | |
145 } | |
146 } | |
147 } else: { | |
148 _message <- decoded get: "message" withDefault: "" | |
149 #{ | |
150 status <- { "error" } | |
151 message <- { _message } | |
152 string <- { | |
153 "Error: " . _message | |
154 } | |
155 print <- { | |
156 print: string . "\n" | |
157 } | |
158 } | |
159 }} // end if | |
160 } | 173 } |
161 | 174 |
162 main <- :args { | 175 main <- :args { |
163 print: ((evalId: "someId" #[1u64 2u64 3u64]) string) . "\n" | 176 print: ((evalId: "someId" #[1u64 2u64 3u64]) string) . "\n" |
164 print: ((guessRequest: "someId" "someProg") string) . "\n" | 177 print: ((guessRequest: "someId" "someProg") string) . "\n" |
165 | 178 |
166 if: (args length) > 1 { | 179 if: (args length) > 1 { |
167 key <- args get: 1 | 180 key <- args get: 1 |
168 print: ((evalProgram: "(lambda (input) (shl1 input))" #[1u64 0xEFFFFFFFFFFFFFu64]) sendWithKey: key) | 181 //print: ((evalProgram: "(lambda (input) (shl1 input))" #[1u64 0xEFFFFFFFFFFFFFu64]) sendWithKey: key) |
182 print: ((evalId: "QwhG7ZpaVsfXiLRvbJfIfxl8" #[1u64 0xEFFFFFFFFFFFFFu64]) sendWithKey: key) | |
169 } | 183 } |
170 } | 184 } |
171 } | 185 } |