update psd integration
This commit is contained in:
40
utils/psd.go
40
utils/psd.go
@@ -32,9 +32,21 @@ func NewPSD(baseURL, login, password string, log *zerolog.Logger) *PSD {
|
||||
return &PSD{url: uri, login: login, password: password, log: log}
|
||||
}
|
||||
|
||||
func (p *PSD) Contains(identifier string) (bool, error) {
|
||||
func (p *PSD) Status(email string) string {
|
||||
psd, err := p.get(email)
|
||||
if err != nil {
|
||||
p.log.Error().Err(err).Str("email", email).Msg("error checking PSD")
|
||||
return ""
|
||||
}
|
||||
if len(psd) == 0 {
|
||||
return ""
|
||||
}
|
||||
return "👥" + psd[0].Labels["domain"] + " 👤"
|
||||
}
|
||||
|
||||
func (p *PSD) get(identifier string) ([]*PSDTarget, error) {
|
||||
if p.url == nil {
|
||||
return false, nil
|
||||
return nil, nil
|
||||
}
|
||||
cloned := *p.url
|
||||
uri := cloned.JoinPath("/node/" + identifier)
|
||||
@@ -43,39 +55,27 @@ func (p *PSD) Contains(identifier string) (bool, error) {
|
||||
defer cancel()
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri.String(), http.NoBody)
|
||||
if err != nil {
|
||||
return false, err
|
||||
return nil, err
|
||||
}
|
||||
req.SetBasicAuth(p.login, p.password)
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return false, err
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
err = fmt.Errorf("%s", resp.Status) //nolint:goerr113 // that's ok
|
||||
return false, err
|
||||
return nil, err
|
||||
}
|
||||
datab, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return false, err
|
||||
return nil, err
|
||||
}
|
||||
var psd []*PSDTarget
|
||||
err = json.Unmarshal(datab, &psd)
|
||||
if err != nil {
|
||||
return false, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return len(psd) > 0, nil
|
||||
}
|
||||
|
||||
func (p *PSD) Status(email string) string {
|
||||
ok, err := p.Contains(email)
|
||||
if err != nil {
|
||||
p.log.Error().Err(err).Str("email", email).Msg("error checking PSD")
|
||||
return ""
|
||||
}
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
return "👤"
|
||||
return psd, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user