Skip to content

Commit

Permalink
Add support for HTTP GET, PUT, DELETE in APIClient.go (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
chkp-nimrodgab authored Jan 9, 2024
1 parent a7a6d5a commit afb5b18
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions APIFiles/APIClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,19 +314,28 @@ waitForTask: determines the behavior when the API server responds with a "task-i
when wait_for_task=False, it is up to the user to call the "show-task" API and check
the status of the command.
useProxy: Determines if the user wants to use the proxy server and port provider.
method: HTTP request method - POST by default
return: APIResponse object
side-effects: updates the class's uid and server variables
*/
func (c *ApiClient) ApiCall(command string, payload map[string]interface{}, sid string, waitForTask bool, useProxy bool) (APIResponse, error) {
return c.apiCall(command,payload,sid,waitForTask,useProxy,false)
func (c *ApiClient) ApiCall(command string, payload map[string]interface{}, sid string, waitForTask bool, useProxy bool, method ...string) (APIResponse, error) {
return c.apiCall(command, payload, sid, waitForTask, useProxy, false, method...)
}

func (c *ApiClient) ApiCallSimple(command string, payload map[string]interface{}) (APIResponse, error) {
return c.apiCall(command, payload, c.sid,true, c.IsProxyUsed(),false)
return c.apiCall(command, payload, c.sid, true, c.IsProxyUsed(), false)
}

func (c *ApiClient) apiCall(command string, payload map[string]interface{}, sid string, waitForTask bool, useProxy bool, internal bool) (APIResponse, error) {
func (c *ApiClient) apiCall(command string, payload map[string]interface{}, sid string, waitForTask bool, useProxy bool, internal bool, method ...string) (APIResponse, error) {
reqMethod := "POST"
if len(method) > 0 {
providedMethod := method[0]
if !isValidHTTPMethod(providedMethod) {
return APIResponse{}, fmt.Errorf("invalid HTTP method: %s", providedMethod)
}
reqMethod = providedMethod
}
fp, errFP := getFingerprint(c.server, c.port)
if errFP != nil {
return APIResponse{}, errFP
Expand Down Expand Up @@ -387,7 +396,7 @@ func (c *ApiClient) apiCall(command string, payload map[string]interface{}, sid

spotReader := bytes.NewReader(_data)

req, err := http.NewRequest("POST", url, spotReader)
req, err := http.NewRequest(reqMethod, url, spotReader)
if err != nil {
return APIResponse{}, err
}
Expand Down Expand Up @@ -1023,3 +1032,15 @@ func (c *ApiClient) askYesOrNoQuestion(question string) bool {
_, _ = fmt.Scanln(&answer)
return strings.ToLower(answer) == "y" || strings.ToLower(answer) == "yes"
}

func isValidHTTPMethod(method string) bool {
validMethods := []string{
"GET", "POST", "PUT", "DELETE",
}
for _, validMethod := range validMethods {
if method == validMethod {
return true
}
}
return false
}

0 comments on commit afb5b18

Please sign in to comment.