132 lines
3.0 KiB
Go
132 lines
3.0 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 (
|
|
"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 {
|
|
var err error
|
|
|
|
apiuri, err := url.JoinPath(hosturi, "/v3/api/account/get")
|
|
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.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 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
|
|
}
|