150 lines
3.2 KiB
Go
150 lines
3.2 KiB
Go
/*
|
|
* 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) CreateAccount(ctx context.Context, hosturi, username, password string) error {
|
|
var err error
|
|
|
|
apiuri, err := url.JoinPath(hosturi, "/v3/api/account/create")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
operParams := operator.CreateAccountParams{
|
|
Username: username,
|
|
Password: password,
|
|
}
|
|
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.CreateAccountResult]()
|
|
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) GetAccount(ctx context.Context, hosturi, id, username string) error {
|
|
var err error
|
|
|
|
apipath, err := url.JoinPath(hosturi, "/v3/api/account/get")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
operParams := operator.GetAccountParams{
|
|
Username: username,
|
|
AccountID: 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.GetAccountResult]()
|
|
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) UpdateAccount(ctx context.Context, hosturi, id, username, newUsername, newPassword string) error {
|
|
var err error
|
|
|
|
apipath, err := url.JoinPath(hosturi, "/v3/api/account/update")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
operParams := operator.UpdateAccountParams{
|
|
Username: username,
|
|
AccountID: id,
|
|
NewUsername: newUsername,
|
|
NewPassword: newPassword,
|
|
}
|
|
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.UpdateAccountResult]()
|
|
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) DeleteAccount(ctx context.Context, hosturi, id, username string) error {
|
|
var err error
|
|
|
|
apipath, err := url.JoinPath(hosturi, "/v3/api/account/delete")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
operParams := operator.DeleteAccountParams{
|
|
Username: username,
|
|
AccountID: 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.DeleteAccountResult]()
|
|
err = json.Unmarshal(respBytes, operRes)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !operRes.Error {
|
|
err = fmt.Errorf("%s", operRes.Message)
|
|
return err
|
|
}
|
|
return err
|
|
}
|