Skip to content

Commit

Permalink
feat: add headers to claude response
Browse files Browse the repository at this point in the history
  • Loading branch information
liushuangls committed May 15, 2024
1 parent 58e0bfa commit 365f9df
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 8 deletions.
25 changes: 19 additions & 6 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,42 @@ type Client struct {
config ClientConfig
}

type Response interface {
SetHeader(http.Header)
}

type httpHeader http.Header

func (h *httpHeader) SetHeader(header http.Header) {
*h = httpHeader(header)
}

func (h *httpHeader) Header() http.Header {
return http.Header(*h)
}

// NewClient create new Anthropic API client
func NewClient(apikey string, opts ...ClientOption) *Client {
return &Client{
config: newConfig(apikey, opts...),
}
}

func (c *Client) sendRequest(req *http.Request, v any) error {
func (c *Client) sendRequest(req *http.Request, v Response) error {
res, err := c.config.HTTPClient.Do(req)
if err != nil {
return err
}

defer res.Body.Close()

v.SetHeader(res.Header)

if err := c.handlerRequestError(res); err != nil {
return err
}

if v != nil {
if err = json.NewDecoder(res.Body).Decode(v); err != nil {
return err
}
if err = json.NewDecoder(res.Body).Decode(v); err != nil {
return err
}

return nil
Expand Down
2 changes: 2 additions & 0 deletions complete.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ func (c *CompleteRequest) SetTopK(k int) {
}

type CompleteResponse struct {
httpHeader

Type string `json:"type"`
ID string `json:"id"`
Completion string `json:"completion"`
Expand Down
5 changes: 4 additions & 1 deletion complete_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,13 @@ func (c *Client) CreateCompleteStream(ctx context.Context, request CompleteStrea
if err != nil {
return
}
defer resp.Body.Close()

response.SetHeader(resp.Header)

if err := c.handlerRequestError(resp); err != nil {
return response, err
}
defer resp.Body.Close()

reader := bufio.NewReader(resp.Body)
var (
Expand Down
2 changes: 2 additions & 0 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ type MessageContentToolUse struct {
}

type MessagesResponse struct {
httpHeader

ID string `json:"id"`
Type MessagesResponseType `json:"type"`
Role string `json:"role"`
Expand Down
5 changes: 4 additions & 1 deletion message_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,13 @@ func (c *Client) CreateMessagesStream(ctx context.Context, request MessagesStrea
if err != nil {
return
}
defer resp.Body.Close()

response.SetHeader(resp.Header)

if err := c.handlerRequestError(resp); err != nil {
return response, err
}
defer resp.Body.Close()

reader := bufio.NewReader(resp.Body)
var (
Expand Down

0 comments on commit 365f9df

Please sign in to comment.