Skip to content

Commit

Permalink
create BasicGet in api library
Browse files Browse the repository at this point in the history
  • Loading branch information
abdfnx committed Dec 9, 2021
1 parent 0545a9f commit d0fb07d
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions core/api/basic_get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package api

import (
"fmt"
"net/http"

httpClient "github.com/abdfnx/resto/client"
"github.com/abdfnx/resto/validation"
)

// BasicGet sends a simple GET request to the url with any potential parameters like `Tokens` or `Basic Auth`
func BasicGet(
httpURL,
method,
authType,
bearerToken,
basicAuthUsername,
basicAuthPassword string,
) (string, string, string, error) {
var url, err = validation.CheckURL(httpURL)

if err != nil {
return "", "", "", err
}

req, err := http.NewRequest(method, url, nil)

if err != nil {
return "", "", "", fmt.Errorf("Error creating request: %s", err.Error())
}

if authType == "bearer" {
req.Header.Set("Authorization", "Bearer " + bearerToken)
} else if authType == "basic" {
req.Header.Set("Authorization", "Basic " + basicAuth(basicAuthUsername, basicAuthPassword))
}

client := httpClient.HttpClient()
res, err := client.Do(req)

if err != nil {
return "", "", "", fmt.Errorf("Error sending request: %s", err.Error())
}

defer res.Body.Close()

return formatResponse(res)
}

0 comments on commit d0fb07d

Please sign in to comment.