add accntcli
This commit is contained in:
@@ -0,0 +1,235 @@
|
||||
/*
|
||||
* 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 accntcli
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"mstore/app/accoper"
|
||||
"mstore/app/handler"
|
||||
"mstore/pkg/descr"
|
||||
)
|
||||
|
||||
func (cli *Client) CreateAccount(ctx context.Context, host, user, pass string) (string, error) {
|
||||
var err error
|
||||
var res string
|
||||
params := accoper.CreateAccountParams{
|
||||
Username: user,
|
||||
Password: pass,
|
||||
}
|
||||
reqdata, err := json.Marshal(params)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
respdata, err := cli.doHTTPCall(ctx, host, "accont", "create", reqdata)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
response := handler.NewResponse[accoper.CreateAccountResult]()
|
||||
err = json.Unmarshal(respdata, response)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
if response.Error {
|
||||
err = errors.New(response.Message)
|
||||
return res, err
|
||||
}
|
||||
res = response.Result.AccountID
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (cli *Client) GetAccountByID(ctx context.Context, host, accountID string) (*descr.AccountShort, error) {
|
||||
var err error
|
||||
res := &descr.AccountShort{}
|
||||
|
||||
params := accoper.GetAccountParams{
|
||||
AccountID: accountID,
|
||||
}
|
||||
reqdata, err := json.Marshal(params)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
resdata, err := cli.doHTTPCall(ctx, host, "account", "get", reqdata)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
response := handler.NewResponse[accoper.GetAccountResult]()
|
||||
err = json.Unmarshal(resdata, response)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
if response.Error {
|
||||
err = errors.New(response.Message)
|
||||
return res, err
|
||||
}
|
||||
res = response.Result.Account
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (cli *Client) GetAccountByName(ctx context.Context, host, username string) (*descr.AccountShort, error) {
|
||||
var err error
|
||||
res := &descr.AccountShort{}
|
||||
params := accoper.GetAccountParams{
|
||||
Username: username,
|
||||
}
|
||||
reqdata, err := json.Marshal(params)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
resdata, err := cli.doHTTPCall(ctx, host, "account", "get", reqdata)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
response := handler.NewResponse[accoper.GetAccountResult]()
|
||||
err = json.Unmarshal(resdata, response)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
if response.Error {
|
||||
err = errors.New(response.Message)
|
||||
return res, err
|
||||
}
|
||||
res = response.Result.Account
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (cli *Client) UpdateAccountByID(ctx context.Context, host string, accountID, newUsername, newPassword string) error {
|
||||
var err error
|
||||
params := accoper.UpdateAccountParams{
|
||||
AccountID: accountID,
|
||||
NewUsername: newUsername,
|
||||
NewPassword: newPassword,
|
||||
}
|
||||
reqdata, err := json.Marshal(params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
resdata, err := cli.doHTTPCall(ctx, host, "account", "update", reqdata)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
response := handler.NewResponse[accoper.UpdateAccountResult]()
|
||||
err = json.Unmarshal(resdata, response)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if response.Error {
|
||||
err = errors.New(response.Message)
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (cli *Client) UpdateAccountByName(ctx context.Context, host, username, newUsername, newPassword string) error {
|
||||
var err error
|
||||
params := accoper.UpdateAccountParams{
|
||||
Username: username,
|
||||
NewUsername: newUsername,
|
||||
NewPassword: newPassword,
|
||||
}
|
||||
reqdata, err := json.Marshal(params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
resdata, err := cli.doHTTPCall(ctx, host, "account", "update", reqdata)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
response := handler.NewResponse[accoper.UpdateAccountResult]()
|
||||
err = json.Unmarshal(resdata, response)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if response.Error {
|
||||
err = errors.New(response.Message)
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (cli *Client) DeleteAccountByName(ctx context.Context, host, username string) error {
|
||||
var err error
|
||||
|
||||
params := accoper.DeleteAccountParams{
|
||||
Username: username,
|
||||
}
|
||||
reqdata, err := json.Marshal(params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
resdata, err := cli.doHTTPCall(ctx, host, "account", "delete", reqdata)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
response := handler.NewResponse[accoper.DeleteAccountResult]()
|
||||
err = json.Unmarshal(resdata, response)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if response.Error {
|
||||
err = errors.New(response.Message)
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (cli *Client) DeleteAccountByID(ctx context.Context, host, accountID string) error {
|
||||
var err error
|
||||
params := accoper.DeleteAccountParams{
|
||||
AccountID: accountID,
|
||||
}
|
||||
reqdata, err := json.Marshal(params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
resdata, err := cli.doHTTPCall(ctx, host, "account", "delete", reqdata)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
response := handler.NewResponse[accoper.DeleteAccountResult]()
|
||||
err = json.Unmarshal(resdata, response)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if response.Error {
|
||||
err = errors.New(response.Message)
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (cli *Client) ListAccounts(ctx context.Context, host string) ([]descr.AccountShort, error) {
|
||||
var err error
|
||||
res := make([]descr.AccountShort, 0)
|
||||
|
||||
params := accoper.ListAccountsParams{}
|
||||
reqdata, err := json.Marshal(params)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
resdata, err := cli.doHTTPCall(ctx, host, "accounts", "list", reqdata)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
response := handler.NewResponse[accoper.ListAccountsResult]()
|
||||
err = json.Unmarshal(resdata, response)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
if response.Error {
|
||||
err = errors.New(response.Message)
|
||||
return res, err
|
||||
}
|
||||
res = response.Result.Accounts
|
||||
return res, err
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
package accntcli
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"encoding/base64"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
httpClient *http.Client
|
||||
userAgent string
|
||||
}
|
||||
|
||||
func NewClient(transport http.RoundTripper, mwFuncs ...MiddlewareFunc) *Client {
|
||||
if transport == nil {
|
||||
transport = NewDefaultTransport()
|
||||
}
|
||||
for _, mwFunc := range mwFuncs {
|
||||
transport = mwFunc(transport)
|
||||
}
|
||||
httpClient := &http.Client{
|
||||
Transport: transport,
|
||||
}
|
||||
|
||||
return &Client{
|
||||
httpClient: httpClient,
|
||||
userAgent: "ociClient/1.0",
|
||||
}
|
||||
}
|
||||
|
||||
func (cli *Client) SetTransport(transport http.RoundTripper) {
|
||||
cli.httpClient.Transport = transport
|
||||
}
|
||||
|
||||
type MiddlewareFunc func(next http.RoundTripper) http.RoundTripper
|
||||
|
||||
func (cli *Client) UseMiddleware(mwFunc MiddlewareFunc) {
|
||||
cli.httpClient.Transport = mwFunc(cli.httpClient.Transport)
|
||||
}
|
||||
|
||||
// BasicAuthMiddleware
|
||||
func NewBasicAuthMiddleware(user, pass string) MiddlewareFunc {
|
||||
return func(next http.RoundTripper) http.RoundTripper {
|
||||
return newBasicAuthMW(next, user, pass)
|
||||
}
|
||||
}
|
||||
|
||||
type BasicAuthMW struct {
|
||||
user, pass string
|
||||
next http.RoundTripper
|
||||
}
|
||||
|
||||
func newBasicAuthMW(next http.RoundTripper, user, pass string) *BasicAuthMW {
|
||||
return &BasicAuthMW{
|
||||
user: user,
|
||||
pass: pass,
|
||||
next: next,
|
||||
}
|
||||
}
|
||||
|
||||
func (tran BasicAuthMW) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
if tran.user != "" && tran.pass != "" {
|
||||
pair := base64.StdEncoding.EncodeToString([]byte(tran.user + ":" + tran.pass))
|
||||
req.Header.Set("Authorization", "Basic "+pair)
|
||||
}
|
||||
return tran.next.RoundTrip(req)
|
||||
}
|
||||
|
||||
// BearerAuthMiddleware
|
||||
func NewBearerAuthMiddleware(token string) MiddlewareFunc {
|
||||
return func(next http.RoundTripper) http.RoundTripper {
|
||||
return newBearerAuthMW(next, token)
|
||||
}
|
||||
}
|
||||
|
||||
type BearerAuthMW struct {
|
||||
token string
|
||||
next http.RoundTripper
|
||||
}
|
||||
|
||||
func newBearerAuthMW(next http.RoundTripper, token string) *BearerAuthMW {
|
||||
return &BearerAuthMW{
|
||||
token: token,
|
||||
next: next,
|
||||
}
|
||||
}
|
||||
|
||||
func (tran BearerAuthMW) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
req.Header.Set("Authorization", "Bearer "+tran.token)
|
||||
return tran.next.RoundTrip(req)
|
||||
}
|
||||
|
||||
// DefaultTransport
|
||||
type DefaultTransport struct {
|
||||
transport http.RoundTripper
|
||||
}
|
||||
|
||||
func NewDefaultTransport() *DefaultTransport {
|
||||
return &DefaultTransport{
|
||||
transport: &http.Transport{
|
||||
TLSClientConfig: &tls.Config{
|
||||
InsecureSkipVerify: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (wrap *DefaultTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
return wrap.transport.RoundTrip(req)
|
||||
}
|
||||
|
||||
// ExampleMiddleware
|
||||
func NewExampleMiddleware() MiddlewareFunc {
|
||||
return func(next http.RoundTripper) http.RoundTripper {
|
||||
return newExampleTransport(next)
|
||||
}
|
||||
}
|
||||
|
||||
type ExampleTransport struct {
|
||||
next http.RoundTripper
|
||||
}
|
||||
|
||||
func newExampleTransport(next http.RoundTripper) *ExampleTransport {
|
||||
return &ExampleTransport{
|
||||
next: next,
|
||||
}
|
||||
}
|
||||
|
||||
func (tran ExampleTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
return tran.next.RoundTrip(req)
|
||||
}
|
||||
@@ -0,0 +1,216 @@
|
||||
/*
|
||||
* 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 accntcli
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"mstore/app/accoper"
|
||||
"mstore/app/handler"
|
||||
"mstore/pkg/descr"
|
||||
)
|
||||
|
||||
func (cli *Client) CreateGrantByAccountID(ctx context.Context, host string, accountID, right, pattern string) (string, error) {
|
||||
var err error
|
||||
var res string
|
||||
|
||||
params := accoper.CreateGrantParams{
|
||||
AccountID: accountID,
|
||||
Right: right,
|
||||
Pattern: pattern,
|
||||
}
|
||||
reqdata, err := json.Marshal(params)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
respdata, err := cli.doHTTPCall(ctx, host, "grant", "create", reqdata)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
response := handler.NewResponse[accoper.CreateGrantResult]()
|
||||
err = json.Unmarshal(respdata, response)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
if response.Error {
|
||||
err = errors.New(response.Message)
|
||||
return res, err
|
||||
}
|
||||
res = response.Result.GrantID
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (cli *Client) CreateGrantByUsername(ctx context.Context, host, username string, right string, pattern string) (string, error) {
|
||||
var err error
|
||||
var res string
|
||||
params := accoper.CreateGrantParams{
|
||||
Username: username,
|
||||
Right: right,
|
||||
Pattern: pattern,
|
||||
}
|
||||
reqdata, err := json.Marshal(params)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
respdata, err := cli.doHTTPCall(ctx, host, "grant", "create", reqdata)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
response := handler.NewResponse[accoper.CreateGrantResult]()
|
||||
err = json.Unmarshal(respdata, response)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
if response.Error {
|
||||
err = errors.New(response.Message)
|
||||
return res, err
|
||||
}
|
||||
res = response.Result.GrantID
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (cli *Client) GetGrant(ctx context.Context, host, grantID string) (*descr.Grant, error) {
|
||||
var err error
|
||||
res := &descr.Grant{}
|
||||
params := accoper.GetGrantParams{
|
||||
GrantID: grantID,
|
||||
}
|
||||
reqdata, err := json.Marshal(params)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
respdata, err := cli.doHTTPCall(ctx, host, "grant", "get", reqdata)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
response := handler.NewResponse[accoper.GetGrantResult]()
|
||||
err = json.Unmarshal(respdata, response)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
if response.Error {
|
||||
err = errors.New(response.Message)
|
||||
return res, err
|
||||
}
|
||||
res = response.Result.Grant
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (cli *Client) UpdateGrant(ctx context.Context, host, grantID, newPattern string) error {
|
||||
var err error
|
||||
params := accoper.UpdateGrantParams{
|
||||
GrantID: grantID,
|
||||
NewPattern: newPattern,
|
||||
}
|
||||
reqdata, err := json.Marshal(params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
respdata, err := cli.doHTTPCall(ctx, host, "grant", "update", reqdata)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
response := handler.NewResponse[accoper.UpdateGrantResult]()
|
||||
err = json.Unmarshal(respdata, response)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if response.Error {
|
||||
err = errors.New(response.Message)
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (cli *Client) DeleteGrant(ctx context.Context, host, grantID string) error {
|
||||
var err error
|
||||
params := accoper.DeleteGrantParams{
|
||||
GrantID: grantID,
|
||||
}
|
||||
reqdata, err := json.Marshal(params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
respdata, err := cli.doHTTPCall(ctx, host, "grant", "delete", reqdata)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
response := handler.NewResponse[accoper.DeleteGrantResult]()
|
||||
err = json.Unmarshal(respdata, response)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if response.Error {
|
||||
err = errors.New(response.Message)
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (cli *Client) ListGrantsByAccountID(ctx context.Context, host, accountID string) ([]descr.Grant, error) {
|
||||
var err error
|
||||
res := make([]descr.Grant, 0)
|
||||
params := accoper.ListGrantsParams{
|
||||
AccountID: accountID,
|
||||
}
|
||||
reqdata, err := json.Marshal(params)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
respdata, err := cli.doHTTPCall(ctx, host, "grants", "list", reqdata)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
response := handler.NewResponse[accoper.ListGrantsResult]()
|
||||
err = json.Unmarshal(respdata, response)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
if response.Error {
|
||||
err = errors.New(response.Message)
|
||||
return res, err
|
||||
}
|
||||
res = response.Result.Grants
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (cli *Client) ListGrantsByUsername(ctx context.Context, host, username string) ([]descr.Grant, error) {
|
||||
var err error
|
||||
res := make([]descr.Grant, 0)
|
||||
params := accoper.ListGrantsParams{
|
||||
Username: username,
|
||||
}
|
||||
reqdata, err := json.Marshal(params)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
respdata, err := cli.doHTTPCall(ctx, host, "grants", "list", reqdata)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
response := handler.NewResponse[accoper.ListGrantsResult]()
|
||||
err = json.Unmarshal(respdata, response)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
if response.Error {
|
||||
err = errors.New(response.Message)
|
||||
return res, err
|
||||
}
|
||||
res = response.Result.Grants
|
||||
return res, err
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package accntcli
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"path"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Referer struct {
|
||||
urlobj *url.URL
|
||||
user, pass string
|
||||
obj, oper string
|
||||
}
|
||||
|
||||
func NewReferer(hostname, object, operation string) (*Referer, error) {
|
||||
ref := &Referer{
|
||||
obj: object,
|
||||
oper: operation,
|
||||
}
|
||||
if !strings.Contains(hostname, "://") {
|
||||
hostname = "https://" + hostname
|
||||
}
|
||||
urlobj, err := url.Parse(hostname)
|
||||
if err != nil {
|
||||
return ref, err
|
||||
}
|
||||
if urlobj.User != nil {
|
||||
ref.user = urlobj.User.Username()
|
||||
ref.pass, _ = urlobj.User.Password()
|
||||
urlobj.User = nil
|
||||
}
|
||||
urlobj.Path = "/"
|
||||
ref.urlobj = urlobj
|
||||
return ref, err
|
||||
}
|
||||
|
||||
func (ref *Referer) Raw() string {
|
||||
return path.Join(ref.urlobj.Host, "/v3/api/", ref.obj, ref.oper)
|
||||
}
|
||||
|
||||
func (ref *Referer) Point() string {
|
||||
curl := ref.urlobj.JoinPath("/v3/api/", ref.obj, ref.oper)
|
||||
return curl.String()
|
||||
}
|
||||
|
||||
func (ref *Referer) Userinfo() (string, string) {
|
||||
return ref.user, ref.pass
|
||||
}
|
||||
|
||||
func (ref *Referer) SetUserinfo(user, pass string) {
|
||||
if user != "" && pass != "" {
|
||||
ref.user, ref.pass = user, pass
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
* 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 accntcli
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"mstore/app/handler"
|
||||
"mstore/app/imageoper"
|
||||
)
|
||||
|
||||
func (cli *Client) ServiceHello(ctx context.Context, host string) (bool, error) {
|
||||
var res bool
|
||||
var err error
|
||||
|
||||
params := imageoper.SendHelloParams{}
|
||||
reqdata, err := json.Marshal(params)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
resdata, err := cli.doHTTPCall(ctx, host, "service", "hello", reqdata)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
response := handler.Response[imageoper.SendHelloResult]{}
|
||||
err = json.Unmarshal(resdata, &response)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
if response.Error {
|
||||
err = errors.New(response.Message)
|
||||
return res, err
|
||||
}
|
||||
res = response.Result.Alive
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (cli *Client) doHTTPCall(ctx context.Context, host, obj, oper string, req []byte) ([]byte, error) {
|
||||
var err error
|
||||
var res []byte
|
||||
|
||||
reader := bytes.NewReader(req)
|
||||
ref, err := NewReferer(host, obj, oper)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, ref.Point(), reader)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
httpReq.Header.Set("User-Agent", cli.userAgent)
|
||||
httpReq.Header.Set("Accept", "*/*")
|
||||
httpResp, err := cli.httpClient.Do(httpReq)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
defer httpResp.Body.Close()
|
||||
if httpResp.StatusCode != http.StatusOK {
|
||||
err := fmt.Errorf("Unexpected response code: %s", httpResp.Status)
|
||||
return res, err
|
||||
}
|
||||
contentLength := httpResp.Header.Get("Content-Length")
|
||||
if contentLength == "" {
|
||||
err := fmt.Errorf("Content-Length header is missing")
|
||||
return res, err
|
||||
}
|
||||
blobSize, err := strconv.ParseInt(contentLength, 10, 64)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
buffer := bytes.NewBuffer(nil)
|
||||
recSize, err := io.Copy(buffer, httpResp.Body)
|
||||
if blobSize != recSize {
|
||||
err := fmt.Errorf("Mismatch declared and actual body size, %d and %d", blobSize, recSize)
|
||||
return res, err
|
||||
}
|
||||
res = buffer.Bytes()
|
||||
return res, err
|
||||
}
|
||||
Reference in New Issue
Block a user