This commit is contained in:
103
internal/idms/request.go
Normal file
103
internal/idms/request.go
Normal file
@@ -0,0 +1,103 @@
|
||||
package idms
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"clintonambulance.com/calculate_negative_points/internal/config"
|
||||
"resty.dev/v3"
|
||||
)
|
||||
|
||||
type Method string
|
||||
|
||||
const Get Method = "GET"
|
||||
const Post Method = "POST"
|
||||
const Put Method = "PUT"
|
||||
const Delete Method = "Delete"
|
||||
|
||||
type ResponsePagination struct {
|
||||
Count int `json:"count"`
|
||||
Current int `json:"current"`
|
||||
EndIndex int `json:"end_index"`
|
||||
Next int `json:"next"`
|
||||
Previous int `json:"previous"`
|
||||
StartIndex int `json:"start_index"`
|
||||
TotalPages int `json:"total_pages"`
|
||||
}
|
||||
|
||||
type IdmsResponse[T any] struct {
|
||||
Pagination ResponsePagination `json:"pagination"`
|
||||
Results []T `json:"results"`
|
||||
}
|
||||
|
||||
type TokenResponse struct {
|
||||
AccessToken string `json:"access_token"`
|
||||
}
|
||||
|
||||
type IdmsRequest[T any] struct {
|
||||
Method Method
|
||||
Path string
|
||||
ResponseDecoder *IdmsResponse[T]
|
||||
}
|
||||
|
||||
func newRestyRequest(url string, path string) *resty.Request {
|
||||
client := resty.New()
|
||||
formattedUrl := fmt.Sprintf("%s%s", url, path)
|
||||
req := client.R()
|
||||
req.SetURL(formattedUrl)
|
||||
defer client.Close()
|
||||
|
||||
return req
|
||||
}
|
||||
|
||||
func GetToken(config *config.ApplicationConfig) (string, error) {
|
||||
req := newRestyRequest(config.Idms.BaseUrl, "/application/o/token/")
|
||||
|
||||
req.SetResult(&TokenResponse{})
|
||||
req.SetMethod(string(Post))
|
||||
req.SetFormData(map[string]string{
|
||||
"client_id": config.Idms.Id.Value(),
|
||||
"grant_type": "client_credentials",
|
||||
"password": config.Idms.Password.Value(),
|
||||
"scope": "goauthentik.io/api",
|
||||
"username": "service",
|
||||
})
|
||||
|
||||
res, err := req.Send()
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
data := res.Result().(*TokenResponse).AccessToken
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func NewRequest[T any](path string, method Method) IdmsRequest[T] {
|
||||
return IdmsRequest[T]{
|
||||
Method: method,
|
||||
Path: path,
|
||||
ResponseDecoder: &IdmsResponse[T]{},
|
||||
}
|
||||
}
|
||||
|
||||
func (r IdmsRequest[T]) Perform(config *config.ApplicationConfig, bodyData interface{}, headers map[string]string) ([]T, error) {
|
||||
req := newRestyRequest(config.Idms.BaseUrl, r.Path)
|
||||
token, err := GetToken(config)
|
||||
|
||||
if err != nil {
|
||||
return []T{}, err
|
||||
}
|
||||
|
||||
req.SetResult(r.ResponseDecoder).SetAuthToken(token).SetContentType("application/json")
|
||||
req.SetMethod(string(r.Method))
|
||||
res, err := req.Send()
|
||||
|
||||
if err != nil {
|
||||
return []T{}, err
|
||||
}
|
||||
|
||||
data := res.Result().(*IdmsResponse[T]).Results
|
||||
|
||||
return data, nil
|
||||
}
|
||||
74
internal/idms/users.go
Normal file
74
internal/idms/users.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package idms
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"clintonambulance.com/calculate_negative_points/internal/config"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
// FlexibleString handles JSON values that can be either strings or numbers
|
||||
type FlexibleString string
|
||||
|
||||
func (f *FlexibleString) UnmarshalJSON(data []byte) error {
|
||||
var s string
|
||||
if err := json.Unmarshal(data, &s); err == nil {
|
||||
*f = FlexibleString(s)
|
||||
return nil
|
||||
}
|
||||
|
||||
var n json.Number
|
||||
if err := json.Unmarshal(data, &n); err == nil {
|
||||
*f = FlexibleString(n.String())
|
||||
return nil
|
||||
}
|
||||
|
||||
return fmt.Errorf("cannot unmarshal %s into FlexibleString", data)
|
||||
}
|
||||
|
||||
type IdmsUserAttributes struct {
|
||||
Cars bool `json:"cars"`
|
||||
EmployeeId FlexibleString `json:"employee_id"`
|
||||
}
|
||||
|
||||
type IdmsUser struct {
|
||||
Attributes IdmsUserAttributes `json:"attributes"`
|
||||
Groups []string `json:"groups"`
|
||||
Id string `json:"uid"`
|
||||
Name string `json:"name"`
|
||||
Username string `json:"username"`
|
||||
}
|
||||
|
||||
func (u IdmsUser) FirstName() string {
|
||||
parts := strings.Fields(u.Name)
|
||||
if len(parts) == 0 {
|
||||
return ""
|
||||
}
|
||||
return parts[0]
|
||||
}
|
||||
|
||||
func (u IdmsUser) LastName() string {
|
||||
parts := strings.Fields(u.Name)
|
||||
if len(parts) == 0 {
|
||||
return ""
|
||||
}
|
||||
return parts[len(parts)-1]
|
||||
}
|
||||
|
||||
func GetCarsMembers(config *config.ApplicationConfig) []IdmsUser {
|
||||
req := NewRequest[IdmsUser]("/api/v3/core/users/?is_active=true", Get)
|
||||
results, _ := req.Perform(config, map[string]string{}, map[string]string{})
|
||||
|
||||
filtered := lo.Filter(results, func(u IdmsUser, _ int) bool {
|
||||
return lo.Contains(u.Groups, config.Idms.CarsGroupId)
|
||||
})
|
||||
|
||||
sort.Slice(filtered, func(i, j int) bool {
|
||||
return filtered[i].LastName() < filtered[j].LastName()
|
||||
})
|
||||
|
||||
return filtered
|
||||
}
|
||||
Reference in New Issue
Block a user