-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi_methods.go
144 lines (120 loc) · 5.1 KB
/
api_methods.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// api_methods.go
package routerosv7_restfull_api
import (
"context"
"fmt"
)
// AuthConfig is the configuration for the AuthDevice function
type AuthConfig struct {
Host string // Host for the request to Mikrotik Router
Username string // Username for the request to Mikrotik Router
Password string // Password for the request to Mikrotik Router
}
// APIRequest represents a request to the API.
type APIRequest struct {
Host string // Host for the request to Mikrotik Router
Username string // Username for the request to Mikrotik Router
Password string // Password for the request to Mikrotik Router
Command string // Command for the request to Mikrotik Router
Payload []byte // Payload for the request to Mikrotik Router
Method string // Method for the request to Mikrotik Router
}
// URL constructs the request URL based on the method and path.
func (r *APIRequest) URL() string {
protocol := determineProtocolFromURL(r.Host) // Determine the protocol from the URL
path := r.Command // Set the path to the command
return fmt.Sprintf("%s://%s/rest/%s", protocol, r.Host, path) // Return the URL
}
// createAndExecuteRequest creates a request configuration and executes the request.
func createAndExecuteRequest(ctx context.Context, request *APIRequest) (interface{}, error) {
// Create a request configuration
config := requestConfig{
URL: request.URL(), // Set the URL
Method: request.Method, // Set the method
Username: request.Username, // Set the username
Password: request.Password, // Set the password
Payload: request.Payload, // Set the payload
}
// Execute the request and return the result and error
return makeRequest(ctx, config)
}
// Auth function now uses createAndExecuteRequest with MethodGet
func Auth(ctx context.Context, config AuthConfig) (interface{}, error) {
// Use the createAndExecuteRequest function with MethodGet
return createAndExecuteRequest(ctx, &APIRequest{
Host: config.Host, // Adjust the host as needed
Username: config.Username, // Adjust the username as needed
Password: config.Password, // Adjust the password as needed
Command: "system/resource", // Adjust the command as needed
Method: MethodGet, // Use MethodGet
Payload: nil, // No payload needed
})
}
// Print creates a new GET request and executes it, returning the result and error.
func Print(ctx context.Context, host, username, password, command string) (interface{}, error) {
// Create a new APIRequest
request := &APIRequest{
Host: host, // Set the host
Username: username, // Set the username
Password: password, // Set the password
Command: command, // Set the command
Method: MethodGet, // Set the method
}
// Return the result and error
return createAndExecuteRequest(ctx, request)
}
// Add creates a new PUT request and executes it, returning the result and error.
func Add(ctx context.Context, host, username, password, command string, payload []byte) (interface{}, error) {
// Create a new APIRequest
request := &APIRequest{
Host: host, // Set the host
Username: username, // Set the username
Password: password, // Set the password
Command: command, // Set the command
Payload: payload, // Set the payload
Method: MethodPut, // Set the method
}
// Return the result and error
return createAndExecuteRequest(ctx, request)
}
// Set creates a new PATCH request and executes it, returning the result and error.
func Set(ctx context.Context, host, username, password, command string, payload []byte) (interface{}, error) {
// Create a new APIRequest
request := &APIRequest{
Host: host, // Set the host
Username: username, // Set the username
Password: password, // Set the password
Command: command, // Set the command
Payload: payload, // Set the payload
Method: MethodPatch, // Set the method
}
// Return the result and error
return createAndExecuteRequest(ctx, request)
}
// Remove creates a new DELETE request and executes it, returning the result and error.
func Remove(ctx context.Context, host, username, password, command string) (interface{}, error) {
// Create a new APIRequest
request := &APIRequest{
Host: host, // Set the host
Username: username, // Set the username
Password: password, // Set the password
Command: command, // Set the command
Method: MethodDelete, // Set the method
}
// Return the result and error
return createAndExecuteRequest(ctx, request)
}
// Run creates a new POST request and executes it, returning the result and error.
func Run(ctx context.Context, host, username, password, command string, payload []byte) (interface{}, error) {
// Create a new APIRequest
request := &APIRequest{
Host: host, // Set the host
Username: username, // Set the username
Password: password, // Set the password
Command: command, // Set the command
Payload: payload, // Set the payload
Method: MethodPost, // Set the method
}
// Return the result and error
return createAndExecuteRequest(ctx, request)
}