diff --git a/pkg/client/account.go b/pkg/client/account.go index 17002f1..41f09da 100644 --- a/pkg/client/account.go +++ b/pkg/client/account.go @@ -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 } diff --git a/pkg/client/file.go b/pkg/client/file.go index 405ebf6..5e63c65 100644 --- a/pkg/client/file.go +++ b/pkg/client/file.go @@ -11,83 +11,16 @@ package client import ( "context" - "crypto/tls" "fmt" "io" "net/http" - "net/url" "os" - "path" "path/filepath" "strconv" - "strings" "mstore/pkg/auxhttp" ) -func makeHTTPClient() *http.Client { - transport := &http.Transport{ - TLSClientConfig: &tls.Config{ - InsecureSkipVerify: true, - }, - } - client := &http.Client{ - Transport: transport, - } - return client -} - -func convertFileURI(fileuri string) (string, error) { - var err error - var res string - uri, err := url.Parse(fileuri) - if err != nil { - return res, err - } - const fileAPI = "/v3/api/file/" - uri.Path, err = url.JoinPath(fileAPI, uri.Path) - if err != nil { - return res, err - } - - res = uri.String() - return res, err -} - -func convertFilesURI(fileuri string) (string, error) { - var err error - var res string - uri, err := url.Parse(fileuri) - const filesAPI = "/v3/api/files/" - uri.Path, err = url.JoinPath(filesAPI, uri.Path) - if err != nil { - return res, err - } - res = uri.String() - return res, err -} - -func repackServiceURI(fileuri string) (string, string, string, error) { - var err error - var res, username, password string - if !strings.Contains(fileuri, "://") { - fileuri = "https://" + fileuri - } - uri, err := url.Parse(fileuri) - if err != nil { - return res, username, password, err - } - uri.Path = path.Clean(uri.Path) - if uri.User != nil { - username = uri.User.Username() - password, _ = uri.User.Password() - } - uri.User = nil - uri.Scheme = "https" - res = uri.String() - return res, username, password, err -} - func (cli *Client) FileExists(ctx context.Context, fileuri string) (bool, error) { var res bool var err error diff --git a/pkg/client/fileaux.go b/pkg/client/fileaux.go new file mode 100644 index 0000000..a923f41 --- /dev/null +++ b/pkg/client/fileaux.go @@ -0,0 +1,81 @@ +/* + * Copyright 2026 Oleg Borodin + * + * 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 ( + "crypto/tls" + "net/http" + "net/url" + "path" + "strings" +) + +func makeHTTPClient() *http.Client { + transport := &http.Transport{ + TLSClientConfig: &tls.Config{ + InsecureSkipVerify: true, + }, + } + client := &http.Client{ + Transport: transport, + } + return client +} + +func convertFileURI(fileuri string) (string, error) { + var err error + var res string + uri, err := url.Parse(fileuri) + if err != nil { + return res, err + } + const fileAPI = "/v3/api/file/" + uri.Path, err = url.JoinPath(fileAPI, uri.Path) + if err != nil { + return res, err + } + + res = uri.String() + return res, err +} + +func convertFilesURI(fileuri string) (string, error) { + var err error + var res string + uri, err := url.Parse(fileuri) + const filesAPI = "/v3/api/files/" + uri.Path, err = url.JoinPath(filesAPI, uri.Path) + if err != nil { + return res, err + } + res = uri.String() + return res, err +} + +func repackServiceURI(fileuri string) (string, string, string, error) { + var err error + var res, username, password string + if !strings.Contains(fileuri, "://") { + fileuri = "https://" + fileuri + } + uri, err := url.Parse(fileuri) + if err != nil { + return res, username, password, err + } + uri.Path = path.Clean(uri.Path) + if uri.User != nil { + username = uri.User.Username() + password, _ = uri.User.Password() + } + uri.User = nil + uri.Scheme = "https" + res = uri.String() + return res, username, password, err +} diff --git a/pkg/client/httpcall.go b/pkg/client/httpcall.go new file mode 100644 index 0000000..dff6447 --- /dev/null +++ b/pkg/client/httpcall.go @@ -0,0 +1,67 @@ +/* + * Copyright 2026 Oleg Borodin + * + * 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 ( + "bytes" + "context" + "crypto/tls" + //"encoding/json" + "fmt" + "io" + "net/http" + //"net/url" + + //"mstore/app/handler" + //"mstore/app/operator" + "mstore/pkg/auxhttp" +) + +func doHTTPCall(ctx context.Context, apiuri string, reqBytes []byte) ([]byte, error) { + var err error + respBytes := make([]byte, 0) + + apiuri, username, password, err := repackServiceURI(apiuri) + if err != nil { + return respBytes, err + } + reqBuffer := bytes.NewBuffer(reqBytes) + httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, apiuri, reqBuffer) + if err != nil { + return respBytes, 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) + if err != nil { + return respBytes, 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) + if err != nil { + return respBytes, err + } + respBytes = respBuffer.Bytes() + return respBytes, err +}