This commit is contained in:
2023-06-06 19:13:41 -04:00
parent b719dbd9fc
commit 618b4e290a
6 changed files with 127 additions and 156 deletions

View File

@@ -1,23 +1,24 @@
package handlers
import (
"encoding/json"
"encoding/base64"
"fmt"
"io"
"net/http"
"strings"
"gitlab.com/DeveloperDurp/durpot/model"
)
func CallDurpAPI(url string, accesstoken string) []byte {
func CallDurpAPI(url string, username string, password string) []byte {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
fmt.Println("Error creating request:", err)
return nil
}
req.Header.Set("Authorization", "Bearer "+accesstoken)
auth := username + ":" + password
basicAuth := "Basic " + base64.StdEncoding.EncodeToString([]byte(auth))
req.Header.Set("Authorization", basicAuth)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
@@ -35,41 +36,3 @@ func CallDurpAPI(url string, accesstoken string) []byte {
}
return body
}
func GenerateToken(clientID string, grantType string, url string, username string, password string) model.AccessTokenResponse {
formData := fmt.Sprintf("grant_type=%s&client_id=%s&username=%s&password=%s",
grantType, clientID, username, password)
client := &http.Client{}
req, err := http.NewRequest("POST", url, strings.NewReader(formData))
if err != nil {
fmt.Println("Error creating request:", err)
return model.AccessTokenResponse{}
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error sending request:", err)
return model.AccessTokenResponse{}
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading response:", err)
return model.AccessTokenResponse{}
}
var response model.AccessTokenResponse
err = json.Unmarshal(body, &response)
if err != nil {
fmt.Println("Error parsing response:", err)
return model.AccessTokenResponse{}
}
return response
}