working commit
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
|
||||
*/
|
||||
package servcli
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
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