added !unraid and restructure

This commit is contained in:
2023-05-27 16:32:59 -05:00
parent 21ce2d0609
commit b2b50d303b
6 changed files with 278 additions and 179 deletions

75
handlers/shared.go Normal file
View File

@@ -0,0 +1,75 @@
package handlers
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"gitlab.com/DeveloperDurp/durpot/model"
)
func CallDurpAPI(url string, accesstoken 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)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error sending request:", err)
return nil
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading response:", err)
return nil
}
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 := ioutil.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
}