MAC vendor lookup: Difference between revisions

Content added Content deleted
(Added Wren)
(→‎{{header|Wren}}: Tweaks to fit the task requirements more closely.)
Line 984: Line 984:
}
}


System.print(MAC.lookup("FC:FB:FB:01:FA:21"))</lang>
System.print(MAC.lookup("FC:FB:FB:01:FA:21"))
for (i in 1..1e8) {} // slow down request
System.print(MAC.lookup("23:45:67"))</lang>


which we embed in the following Go program and run it.
which we embed in the following Go program and run it.
Line 1,001: Line 1,003:
func macLookup(vm *wren.VM, parameters []any) (any, error) {
func macLookup(vm *wren.VM, parameters []any) (any, error) {
mac := parameters[1].(string)
mac := parameters[1].(string)
resp, _ := http.Get("http://api.macvendors.com/" + mac)
resp, err := http.Get("http://api.macvendors.com/" + mac)
if err != nil {
body, _ := ioutil.ReadAll(resp.Body)
return string(body), nil
return nil, nil
}
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, nil
}
var vendor = string(body)
if vendor == `{"errors":{"detail":"Not Found"}}` {
return "N/A", nil
}
return vendor, nil
}


func main() {
func main() {
Line 1,020: Line 1,032:
<pre>
<pre>
Cisco Systems, Inc
Cisco Systems, Inc
N/A
</pre>
</pre>