-
Notifications
You must be signed in to change notification settings - Fork 0
/
gomat.go
67 lines (63 loc) · 1.39 KB
/
gomat.go
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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
)
func RecursiveDataProcess(d map[string]interface{}) {
for k, v := range d {
switch dd := v.(type) {
case []interface{}:
for _, u := range dd {
uu, _ := u.(map[string]interface{})
RecursiveDataProcess(uu)
}
case map[string]interface{}:
fmt.Println(k, " :")
for l, m := range dd {
fmt.Println(" ", l, " :", m)
}
default:
fmt.Println(k, " :", dd)
}
}
}
func main() {
preamble := "https://www.materialsproject.org/rest/v1"
request_type := "materials" //"materials", "battery", "reaction", "mpquery" and "api_check"
identifier := os.Args[1]
data_type := "vasp"
mapi_key := os.Getenv("MAPI_KEY")
url := fmt.Sprintf(
"%s/%s/%s/%s?API_KEY=%s",
preamble,
request_type,
identifier,
data_type,
mapi_key)
client := &http.Client{}
req, httperr := http.NewRequest("GET", url, nil)
if httperr != nil {
fmt.Errorf("gomat: %s", httperr)
}
resp, reqerr := client.Do(req)
if reqerr != nil {
fmt.Errorf("gomat: %s", reqerr)
}
defer resp.Body.Close()
body, dataerr := ioutil.ReadAll(resp.Body)
//fmt.Printf(string(body))
if dataerr != nil {
fmt.Errorf("gomat: %s", dataerr)
}
var data interface{}
jsonerr := json.Unmarshal(body, &data)
if jsonerr != nil {
fmt.Errorf("gomat: %s", jsonerr)
}
d := data.(map[string]interface{})
RecursiveDataProcess(d)
fmt.Println("Done")
}