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
}
-67
View File
@@ -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
+81
View File
@@ -0,0 +1,81 @@
/*
* 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 (
"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
}
+67
View File
@@ -0,0 +1,67 @@
/*
* 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 (
"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
}