working commit
This commit is contained in:
+54
-36
@@ -10,18 +10,13 @@
|
|||||||
package client
|
package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
"net/url"
|
"net/url"
|
||||||
|
|
||||||
"mstore/app/handler"
|
"mstore/app/handler"
|
||||||
"mstore/app/operator"
|
"mstore/app/operator"
|
||||||
"mstore/pkg/auxhttp"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func (cli *Client) CreateAccount(ctx context.Context, hosturi, username, password string) error {
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
operRes := handler.NewResponse[operator.CreateAccountResult]()
|
operRes := handler.NewResponse[operator.GetAccountResult]()
|
||||||
err = json.Unmarshal(respBytes, operRes)
|
err = json.Unmarshal(respBytes, operRes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -88,44 +83,67 @@ func (cli *Client) GetAccount(ctx context.Context, hosturi, id, username string)
|
|||||||
return err
|
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
|
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 {
|
if err != nil {
|
||||||
return respBytes, err
|
return err
|
||||||
}
|
}
|
||||||
reqBuffer := bytes.NewBuffer(reqBytes)
|
operParams := operator.UpdateAccountParams{
|
||||||
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, apiuri, reqBuffer)
|
Username: username,
|
||||||
|
AccountID: id,
|
||||||
|
NewUsername: newUsername,
|
||||||
|
NewPassword: newPassword,
|
||||||
|
}
|
||||||
|
paramsJson, err := json.Marshal(operParams)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return respBytes, err
|
return err
|
||||||
}
|
}
|
||||||
httpReq.Header.Set("Content-Type", "application/json")
|
respBytes, err := doHTTPCall(ctx, apipath, paramsJson)
|
||||||
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 {
|
if err != nil {
|
||||||
return respBytes, err
|
return err
|
||||||
}
|
}
|
||||||
defer httpResp.Body.Close()
|
operRes := handler.NewResponse[operator.UpdateAccountResult]()
|
||||||
if httpResp.StatusCode != http.StatusOK {
|
err = json.Unmarshal(respBytes, operRes)
|
||||||
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 {
|
if err != nil {
|
||||||
return respBytes, err
|
return err
|
||||||
}
|
}
|
||||||
respBytes = respBuffer.Bytes()
|
if !operRes.Error {
|
||||||
return respBytes, err
|
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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,83 +11,16 @@ package client
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
|
||||||
"os"
|
"os"
|
||||||
"path"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"mstore/pkg/auxhttp"
|
"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) {
|
func (cli *Client) FileExists(ctx context.Context, fileuri string) (bool, error) {
|
||||||
var res bool
|
var res bool
|
||||||
var err error
|
var err error
|
||||||
|
|||||||
@@ -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
|
||||||
|
}
|
||||||
@@ -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
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user