working commit
This commit is contained in:
@@ -0,0 +1,146 @@
|
||||
/*
|
||||
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
|
||||
*
|
||||
* This work is published and licensed under a Creative Commons
|
||||
* Attribution-NonCommercial-NoDerivatives 4.0 International License.
|
||||
*
|
||||
* Distribution of this work is permitted, but commercial use and
|
||||
* modifications are strictly prohibited.
|
||||
*/
|
||||
package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/url"
|
||||
|
||||
"mstore/app/handler"
|
||||
"mstore/app/operator"
|
||||
)
|
||||
|
||||
func (cli *Client) CreateGrant(ctx context.Context, hosturi, accountID, operation, pattern string) error {
|
||||
var err error
|
||||
|
||||
apiuri, err := url.JoinPath(hosturi, "/v3/api/grant/create")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
operParams := operator.CreateGrantParams{
|
||||
AccountID: accountID,
|
||||
Operation: operation,
|
||||
Pattern: pattern,
|
||||
}
|
||||
paramsJson, err := json.Marshal(operParams)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
respBytes, err := doHTTPCall(ctx, apiuri, paramsJson)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
operRes := handler.NewResponse[operator.CreateGrantResult]()
|
||||
err = json.Unmarshal(respBytes, operRes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !operRes.Error {
|
||||
err = fmt.Errorf("%s", operRes.Message)
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (cli *Client) GetGrant(ctx context.Context, hosturi, id, username string) error {
|
||||
var err error
|
||||
|
||||
apipath, err := url.JoinPath(hosturi, "/v3/api/grant/get")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
operParams := operator.GetGrantParams{
|
||||
GrantID: id,
|
||||
}
|
||||
paramsJson, err := json.Marshal(operParams)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
respBytes, err := doHTTPCall(ctx, apipath, paramsJson)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
operRes := handler.NewResponse[operator.GetGrantResult]()
|
||||
err = json.Unmarshal(respBytes, operRes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !operRes.Error {
|
||||
err = fmt.Errorf("%s", operRes.Message)
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (cli *Client) UpdateGrant(ctx context.Context, hosturi, grantID, newPattern string) error {
|
||||
var err error
|
||||
|
||||
apipath, err := url.JoinPath(hosturi, "/v3/api/grant/update")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
operParams := operator.UpdateGrantParams{
|
||||
GrantID: grantID,
|
||||
NewPattern: newPattern,
|
||||
}
|
||||
paramsJson, err := json.Marshal(operParams)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
respBytes, err := doHTTPCall(ctx, apipath, paramsJson)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
operRes := handler.NewResponse[operator.UpdateGrantResult]()
|
||||
err = json.Unmarshal(respBytes, operRes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !operRes.Error {
|
||||
err = fmt.Errorf("%s", operRes.Message)
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (cli *Client) DeleteGrant(ctx context.Context, hosturi, grantID string) error {
|
||||
var err error
|
||||
|
||||
apipath, err := url.JoinPath(hosturi, "/v3/api/grant/delete")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
operParams := operator.DeleteGrantParams{
|
||||
GrantID: grantID,
|
||||
}
|
||||
paramsJson, err := json.Marshal(operParams)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
respBytes, err := doHTTPCall(ctx, apipath, paramsJson)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
operRes := handler.NewResponse[operator.DeleteGrantResult]()
|
||||
err = json.Unmarshal(respBytes, operRes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !operRes.Error {
|
||||
err = fmt.Errorf("%s", operRes.Message)
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user