Welcome to mirror list, hosted at ThFree Co, Russian Federation.

system.go « octoprintApis - github.com/Z-Bolt/OctoScreen.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ef1da2f5b337c282be0db9677e25dc94a7ffc99c (plain)
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
package octoprintApis

import (
	"encoding/json"
	"fmt"

	"github.com/Z-Bolt/OctoScreen/logger"
	"github.com/Z-Bolt/OctoScreen/octoprintApis/dataModels"
)


const SystemCommandsApiUri = "/api/system/commands"


var ExecuteErrors = StatusMapping {
	404: "The command could not be found for source and action",
	500: "The command didn’t define a command to execute, the command returned a non-zero return code and ignore was not true or some other internal server error occurred",
}


// SystemCommandsRequest retrieves all configured system commands.
type SystemCommandsRequest struct{}

// Do sends an API request and returns the API response.
func (cmd *SystemCommandsRequest) Do(c *Client) (*dataModels.SystemCommandsResponse, error) {
	bytes, err := c.doJsonRequest("GET", SystemCommandsApiUri, nil, nil, true)
	if err != nil {
		return nil, err
	}

	response := &dataModels.SystemCommandsResponse{}
	if err := json.Unmarshal(bytes, response); err != nil {
		return nil, err
	}

	for i := range response.Core {
		commandDefinition := response.Core[i]
		err = json.Unmarshal(commandDefinition.RawConfirm, &commandDefinition.Confirm)
		if err != nil {
			logger.LogError("SystemCommandsRequest.Do()", "json.Unmarshal(Core)", err)
			commandDefinition.Confirm = ""
			return nil, err
		}
	}

	for i := range response.Custom {
		commandDefinition := response.Custom[i]
		err = json.Unmarshal(commandDefinition.RawConfirm, &commandDefinition.Confirm)
		if err != nil {
			logger.LogError("SystemCommandsRequest.Do()", "json.Unmarshal(Custom)", err)
			commandDefinition.Confirm = ""
			return nil, err
		}
	}

	return response, err
}

// SystemExecuteCommandRequest retrieves all configured system commands.
type SystemExecuteCommandRequest struct {
	// Source for which to list commands.
	Source dataModels.CommandSource `json:"source"`

	// Action is the identifier of the command, action from its definition.
	Action string `json:"action"`
}

// Do sends an API request and returns an error if any.
func (cmd *SystemExecuteCommandRequest) Do(c *Client) error {
	uri := fmt.Sprintf("%s/%s/%s", SystemCommandsApiUri, cmd.Source, cmd.Action)
	_, err := c.doJsonRequest("POST", uri, nil, ExecuteErrors, true)
	return err
}