comparison src/requests.tp @ 26:18a043613dae

added guess response.
author William Morgan <bill@mrgn.org>
date Sat, 10 Aug 2013 18:39:45 -0700
parents bb80f86c5048
children a4bffcd381cd
comparison
equal deleted inserted replaced
25:bb80f86c5048 26:18a043613dae
18 } 18 }
19 19
20 println <- :str { 20 println <- :str {
21 print: str . "\n" 21 print: str . "\n"
22 } 22 }
23
24
25
26 23
27 evalId <- :id args { 24 evalId <- :id args {
28 #{ 25 #{
29 string <- { 26 string <- {
30 idStr <- (quote: "id") . ":" . (quote: id) 27 idStr <- (quote: "id") . ":" . (quote: id)
71 decoded <- json decode: bod 68 decoded <- json decode: bod
72 _status <- "error" 69 _status <- "error"
73 if: _code = 200 { 70 if: _code = 200 {
74 _status <- decoded get: "status" withDefault: "error" 71 _status <- decoded get: "status" withDefault: "error"
75 } else: { 72 } else: {
76 print: "bad status" 73 print: "http response gave error!, code was: " . _code
77 } 74 }
78 if: _status = "ok" { 75 if: _status = "ok" {
79 _outputs <- (decoded get: "outputs" withDefault: #[]) map: :num { 76 _outputs <- (decoded get: "outputs" withDefault: #[]) map: :num {
80 (num from: 2) parseHex64 77 (num from: 2) parseHex64
81 } 78 }
106 } 103 }
107 } 104 }
108 } 105 }
109 } 106 }
110 107
108 guessResponse <- :httpResp {
109 _code <- httpResp statusCode
110 bod <- httpResp body
111 print: "Response code: " . (string: _code) . "\n"
112 print: bod . "\n"
113 decoded <- json decode: bod
114 _status <- "error"
115 if: _code = 200 {
116 _status <- decoded get: "status" withDefault: "error"
117 } else: {
118 print: "http response gave error!, code was: " . _code
119 }
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 }
161
111 main <- :args { 162 main <- :args {
112 print: ((evalId: "someId" #[1u64 2u64 3u64]) string) . "\n" 163 print: ((evalId: "someId" #[1u64 2u64 3u64]) string) . "\n"
113 print: ((guessRequest: "someId" "someProg") string) . "\n" 164 print: ((guessRequest: "someId" "someProg") string) . "\n"
114 165
115 if: (args length) > 1 { 166 if: (args length) > 1 {