working commit

This commit is contained in:
2026-02-11 13:08:51 +02:00
parent 6fe7f7c15e
commit 75bd5a952b
4 changed files with 202 additions and 103 deletions
+54 -36
View File
@@ -10,18 +10,13 @@
package client
import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"mstore/app/handler"
"mstore/app/operator"
"mstore/pkg/auxhttp"
)
func (cli *Client) CreateAccount(ctx context.Context, hosturi, username, password string) error {
@@ -76,7 +71,7 @@ func (cli *Client) GetAccount(ctx context.Context, hosturi, id, username string)
return err
}
operRes := handler.NewResponse[operator.CreateAccountResult]()
operRes := handler.NewResponse[operator.GetAccountResult]()
err = json.Unmarshal(respBytes, operRes)
if err != nil {
return err
@@ -88,44 +83,67 @@ func (cli *Client) GetAccount(ctx context.Context, hosturi, id, username string)
return err
}
func doHTTPCall(ctx context.Context, apiuri string, reqBytes []byte) ([]byte, error) {
func (cli *Client) UpdateAccount(ctx context.Context, hosturi, id, username, newUsername, newPassword string) error {
var err error
respBytes := make([]byte, 0)
apiuri, username, password, err := repackServiceURI(apiuri)
apipath, err := url.JoinPath(hosturi, "/v3/api/account/update")
if err != nil {
return respBytes, err
return err
}
reqBuffer := bytes.NewBuffer(reqBytes)
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, apiuri, reqBuffer)
operParams := operator.UpdateAccountParams{
Username: username,
AccountID: id,
NewUsername: newUsername,
NewPassword: newPassword,
}
paramsJson, err := json.Marshal(operParams)
if err != nil {
return respBytes, err
return err
}
httpReq.Header.Set("Content-Type", "application/json")
if username != "" && password != "" {
basicHeader := auxhttp.EncodeBasicAuth(username, password)
httpReq.Header.Add("Authorization", basicHeader)
}
transport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
httpClient := &http.Client{
Transport: transport,
}
httpResp, err := httpClient.Do(httpReq)
respBytes, err := doHTTPCall(ctx, apipath, paramsJson)
if err != nil {
return respBytes, err
return err
}
defer httpResp.Body.Close()
if httpResp.StatusCode != http.StatusOK {
err := fmt.Errorf("Wrong StatusCode header: %s", httpResp.Status)
return respBytes, err
}
respBuffer := bytes.NewBuffer(nil)
_, err = io.Copy(respBuffer, httpResp.Body)
operRes := handler.NewResponse[operator.UpdateAccountResult]()
err = json.Unmarshal(respBytes, operRes)
if err != nil {
return respBytes, err
return err
}
respBytes = respBuffer.Bytes()
return respBytes, 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
}