-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapicalls.go
96 lines (91 loc) · 2.63 KB
/
apicalls.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
)
func apiGetPrinterInfo() PrinterInfoJSON {
// Call API for job information
apiurl := "http://" + c.Octopi + "/api/printer?exclude=state"
// Create Request with correct headers for authorization
client := &http.Client{}
req, err := http.NewRequest("GET", apiurl, nil)
req.Header.Add("X-Api-Key", c.Apikey)
resp, err := client.Do(req)
// Check for errors
if err != nil {
log.Fatalln(err)
}
body, err := ioutil.ReadAll(resp.Body)
// Create Struct to store json and parse the response
var infos PrinterInfoJSON
json.Unmarshal(body, &infos)
return infos
}
//PrinterInfoJSON represents the JSON Response from this Octoprint API Endpoint
type PrinterInfoJSON struct {
Sd struct {
Ready bool `json:"ready"`
} `json:"sd"`
Temperature struct {
Bed struct {
Actual float64 `json:"actual"`
Offset int `json:"offset"`
Target float64 `json:"target"`
} `json:"bed"`
Tool0 struct {
Actual float64 `json:"actual"`
Offset int `json:"offset"`
Target float64 `json:"target"`
} `json:"tool0"`
} `json:"temperature"`
}
func apiGetJobInfo() JobInfoJSON {
// Call API for job information
apiurl := "http://" + c.Octopi + "/api/job"
// Create Request with correct headers for authorization
client := &http.Client{}
req, err := http.NewRequest("GET", apiurl, nil)
req.Header.Add("X-Api-Key", c.Apikey)
resp, err := client.Do(req)
// Check for errors
if err != nil {
log.Fatalln(err)
}
body, err := ioutil.ReadAll(resp.Body)
// Create Struct to store json and parse the response
var infos JobInfoJSON
json.Unmarshal(body, &infos)
return infos
}
//JobInfoJSON represents the JSON Response from this Octoprint API Endpoint
type JobInfoJSON struct {
Job struct {
AveragePrintTime interface{} `json:"averagePrintTime"`
EstimatedPrintTime float64 `json:"estimatedPrintTime"`
Filament struct {
Tool0 struct {
Length float64 `json:"length"`
Volume float64 `json:"volume"`
} `json:"tool0"`
} `json:"filament"`
File struct {
Date int `json:"date"`
Display string `json:"display"`
Name string `json:"name"`
Origin string `json:"origin"`
Path string `json:"path"`
Size int `json:"size"`
} `json:"file"`
LastPrintTime interface{} `json:"lastPrintTime"`
} `json:"job"`
Progress struct {
Completion float64 `json:"completion"`
Filepos int `json:"filepos"`
PrintTime int `json:"printTime"`
PrintTimeLeft int `json:"printTimeLeft"`
PrintTimeLeftOrigin string `json:"printTimeLeftOrigin"`
} `json:"progress"`
State string `json:"state"`
}