client request options
This commit is contained in:
parent
b0618a466e
commit
291bb97e3d
1 changed files with 53 additions and 29 deletions
|
@ -25,19 +25,35 @@ func NewClient(hosts ...string) *Client {
|
|||
}
|
||||
}
|
||||
|
||||
func (c *Client) stream(ctx context.Context, method string, path string, reqData any, fn func(bts []byte) error) error {
|
||||
var reqBody io.Reader
|
||||
var data []byte
|
||||
var err error
|
||||
if reqData != nil {
|
||||
data, err = json.Marshal(reqData)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
reqBody = bytes.NewReader(data)
|
||||
type options struct {
|
||||
requestBody io.Reader
|
||||
responseFunc func(bts []byte) error
|
||||
}
|
||||
|
||||
request, err := http.NewRequestWithContext(ctx, method, c.base.JoinPath(path).String(), reqBody)
|
||||
func OptionRequestBody(data any) func(*options) {
|
||||
bts, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return func(opts *options) {
|
||||
opts.requestBody = bytes.NewReader(bts)
|
||||
}
|
||||
}
|
||||
|
||||
func OptionResponseFunc(fn func([]byte) error) func(*options) {
|
||||
return func(opts *options) {
|
||||
opts.responseFunc = fn
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) stream(ctx context.Context, method, path string, fns ...func(*options)) error {
|
||||
var opts options
|
||||
for _, fn := range fns {
|
||||
fn(&opts)
|
||||
}
|
||||
|
||||
request, err := http.NewRequestWithContext(ctx, method, c.base.JoinPath(path).String(), opts.requestBody)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -51,12 +67,14 @@ func (c *Client) stream(ctx context.Context, method string, path string, reqData
|
|||
}
|
||||
defer response.Body.Close()
|
||||
|
||||
if opts.responseFunc != nil {
|
||||
scanner := bufio.NewScanner(response.Body)
|
||||
for scanner.Scan() {
|
||||
if err := fn(scanner.Bytes()); err != nil {
|
||||
if err := opts.responseFunc(scanner.Bytes()); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -64,25 +82,31 @@ func (c *Client) stream(ctx context.Context, method string, path string, reqData
|
|||
type GenerateResponseFunc func(GenerateResponse) error
|
||||
|
||||
func (c *Client) Generate(ctx context.Context, req *GenerateRequest, fn GenerateResponseFunc) error {
|
||||
return c.stream(ctx, http.MethodPost, "/api/generate", req, func(bts []byte) error {
|
||||
return c.stream(ctx, http.MethodPost, "/api/generate",
|
||||
OptionRequestBody(req),
|
||||
OptionResponseFunc(func(bts []byte) error {
|
||||
var resp GenerateResponse
|
||||
if err := json.Unmarshal(bts, &resp); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return fn(resp)
|
||||
})
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
type PullProgressFunc func(PullProgress) error
|
||||
|
||||
func (c *Client) Pull(ctx context.Context, req *PullRequest, fn PullProgressFunc) error {
|
||||
return c.stream(ctx, http.MethodPost, "/api/pull", req, func(bts []byte) error {
|
||||
return c.stream(ctx, http.MethodPost, "/api/pull",
|
||||
OptionRequestBody(req),
|
||||
OptionResponseFunc(func(bts []byte) error {
|
||||
var resp PullProgress
|
||||
if err := json.Unmarshal(bts, &resp); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return fn(resp)
|
||||
})
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue