Skip to content

Commit afb5b18

Browse files
Add support for HTTP GET, PUT, DELETE in APIClient.go (#16)
1 parent a7a6d5a commit afb5b18

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

APIFiles/APIClient.go

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -314,19 +314,28 @@ waitForTask: determines the behavior when the API server responds with a "task-i
314314
when wait_for_task=False, it is up to the user to call the "show-task" API and check
315315
the status of the command.
316316
useProxy: Determines if the user wants to use the proxy server and port provider.
317+
method: HTTP request method - POST by default
317318
return: APIResponse object
318319
side-effects: updates the class's uid and server variables
319320
320321
*/
321-
func (c *ApiClient) ApiCall(command string, payload map[string]interface{}, sid string, waitForTask bool, useProxy bool) (APIResponse, error) {
322-
return c.apiCall(command,payload,sid,waitForTask,useProxy,false)
322+
func (c *ApiClient) ApiCall(command string, payload map[string]interface{}, sid string, waitForTask bool, useProxy bool, method ...string) (APIResponse, error) {
323+
return c.apiCall(command, payload, sid, waitForTask, useProxy, false, method...)
323324
}
324325

325326
func (c *ApiClient) ApiCallSimple(command string, payload map[string]interface{}) (APIResponse, error) {
326-
return c.apiCall(command, payload, c.sid,true, c.IsProxyUsed(),false)
327+
return c.apiCall(command, payload, c.sid, true, c.IsProxyUsed(), false)
327328
}
328329

329-
func (c *ApiClient) apiCall(command string, payload map[string]interface{}, sid string, waitForTask bool, useProxy bool, internal bool) (APIResponse, error) {
330+
func (c *ApiClient) apiCall(command string, payload map[string]interface{}, sid string, waitForTask bool, useProxy bool, internal bool, method ...string) (APIResponse, error) {
331+
reqMethod := "POST"
332+
if len(method) > 0 {
333+
providedMethod := method[0]
334+
if !isValidHTTPMethod(providedMethod) {
335+
return APIResponse{}, fmt.Errorf("invalid HTTP method: %s", providedMethod)
336+
}
337+
reqMethod = providedMethod
338+
}
330339
fp, errFP := getFingerprint(c.server, c.port)
331340
if errFP != nil {
332341
return APIResponse{}, errFP
@@ -387,7 +396,7 @@ func (c *ApiClient) apiCall(command string, payload map[string]interface{}, sid
387396

388397
spotReader := bytes.NewReader(_data)
389398

390-
req, err := http.NewRequest("POST", url, spotReader)
399+
req, err := http.NewRequest(reqMethod, url, spotReader)
391400
if err != nil {
392401
return APIResponse{}, err
393402
}
@@ -1023,3 +1032,15 @@ func (c *ApiClient) askYesOrNoQuestion(question string) bool {
10231032
_, _ = fmt.Scanln(&answer)
10241033
return strings.ToLower(answer) == "y" || strings.ToLower(answer) == "yes"
10251034
}
1035+
1036+
func isValidHTTPMethod(method string) bool {
1037+
validMethods := []string{
1038+
"GET", "POST", "PUT", "DELETE",
1039+
}
1040+
for _, validMethod := range validMethods {
1041+
if method == validMethod {
1042+
return true
1043+
}
1044+
}
1045+
return false
1046+
}

0 commit comments

Comments
 (0)