Skip to content

Commit

Permalink
fix: remove log/slog and unsupported method (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
northes committed Apr 22, 2024
1 parent 8b7e62d commit 6f61681
Show file tree
Hide file tree
Showing 10 changed files with 16 additions and 93 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## Go Moonshot

[![Go Report Card](https://goreportcard.com/badge/github.com/northes/go-moonshot)](https://goreportcard.com/report/github.com/northes/go-moonshot)
![Go Version](https://img.shields.io/badge/Go-%3E%3D%201.18-%23007d9c)
[![tag](https://img.shields.io/github/tag/northes/go-moonshot.svg)](https://github.com/northes/go-moonshot/releases)
[![Go Reference](https://pkg.go.dev/badge/github.com/northes/go-moonshot.svg)](https://pkg.go.dev/github.com/northes/go-moonshot)
[![codecov](https://codecov.io/gh/northes/go-moonshot/graph/badge.svg?token=81O85CA9KL)](https://codecov.io/gh/northes/go-moonshot)
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fnorthes%2Fgo-moonshot.svg?type=shield&issueType=license)](https://app.fossa.com/projects/git%2Bgithub.com%2Fnorthes%2Fgo-moonshot?ref=badge_shield&issueType=license)
Expand Down
4 changes: 2 additions & 2 deletions api_chat_completions.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (c *chat) Completions(ctx context.Context, req *ChatCompletionsRequest) (*C
const path = "/v1/chat/completions"
req.Stream = false
chatCompletionsResp := new(ChatCompletionsResponse)
resp, err := c.client.HTTPClient().AddPath(path).SetBody(req).Post()
resp, err := c.client.HTTPClient().SetPath(path).SetBody(req).Post()
if err != nil {
return chatCompletionsResp, err
}
Expand Down Expand Up @@ -100,7 +100,7 @@ func (c *chat) CompletionsStream(ctx context.Context, req *ChatCompletionsReques

req.Stream = true

resp, err := c.client.HTTPClient().AddPath(path).SetBody(req).Post()
resp, err := c.client.HTTPClient().SetPath(path).SetBody(req).Post()
if err != nil {
return nil, err
}
Expand Down
12 changes: 6 additions & 6 deletions api_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (f *files) Upload(req *FilesUploadRequest) (*FilesUploadResponse, error) {
}

resp, err := f.client.HTTPClient().
AddPath(path).
SetPath(path).
SetBody(&b).
SetContentType(builder.FormDataContentType()).
Post()
Expand Down Expand Up @@ -119,7 +119,7 @@ func (f *files) UploadBytes(req *FilesUploadBytesRequest) (*FilesUploadBytesResp
}

resp, err := f.client.HTTPClient().
AddPath(path).
SetPath(path).
SetBody(&b).
SetContentType(builder.FormDataContentType()).
Post()
Expand Down Expand Up @@ -159,7 +159,7 @@ type FilesListResponseData struct {

func (f *files) Lists() (*FilesListResponse, error) {
const path = "/v1/files"
resp, err := f.client.HTTPClient().AddPath(path).Get()
resp, err := f.client.HTTPClient().SetPath(path).Get()
if err != nil {
return nil, err
}
Expand All @@ -184,7 +184,7 @@ type FilesDeleteResponse struct {
func (f *files) Delete(fileID string) (*FilesDeleteResponse, error) {
const path = "/v1/files/%s"
fullPath := fmt.Sprintf(path, fileID)
resp, err := f.client.HTTPClient().AddPath(fullPath).Delete()
resp, err := f.client.HTTPClient().SetPath(fullPath).Delete()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -236,7 +236,7 @@ type FilesInfoResponse struct {
func (f *files) Info(fileID string) (*FilesInfoResponse, error) {
const path = "/v1/files/%s"
fullPath := fmt.Sprintf(path, fileID)
resp, err := f.client.HTTPClient().AddPath(fullPath).Get()
resp, err := f.client.HTTPClient().SetPath(fullPath).Get()
if err != nil {
return nil, err
}
Expand All @@ -262,7 +262,7 @@ type FileContentResponse struct {
func (f *files) Content(fileID string) (*FileContentResponse, error) {
const path = "/v1/files/%s/content"
fullPath := fmt.Sprintf(path, fileID)
resp, err := f.client.HTTPClient().AddPath(fullPath).Get()
resp, err := f.client.HTTPClient().SetPath(fullPath).Get()
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion api_models.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type ListModelsResponseDataPermission struct {

func (m *models) List(ctx context.Context) (*ListModelsResponse, error) {
const path = "/v1/models"
resp, err := m.client.HTTPClient().AddPath(path).Get()
resp, err := m.client.HTTPClient().SetPath(path).Get()
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion api_tokenizers_estimate_token_count.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type TokenizersEstimateTokenCountResponseData struct {
func (t *tokenizersEstimateTokenCount) EstimateTokenCount(ctx context.Context, req *TokenizersEstimateTokenCountRequest) (*TokenizersEstimateTokenCountResponse, error) {
const path = "/v1/tokenizers/estimate-token-count"
estimateTokenCountResp := new(TokenizersEstimateTokenCountResponse)
resp, err := t.client.HTTPClient().AddPath(path).SetBody(req).Post()
resp, err := t.client.HTTPClient().SetPath(path).SetBody(req).Post()
if err != nil {
return nil, err
}
Expand Down
4 changes: 1 addition & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,5 @@ func NewClientWithConfig(cfg *Config) (*Client, error) {
}

func (c *Client) HTTPClient() *httpx.Client {
return httpx.NewClient(c.cfg.Host,
httpx.WithDebug(c.cfg.Debug),
).AddHeader(tools.AuthorizationHeaderKey, tools.ToBearToken(c.cfg.APIKey))
return httpx.NewClient(c.cfg.Host).AddHeader(tools.AuthorizationHeaderKey, tools.ToBearToken(c.cfg.APIKey))
}
6 changes: 0 additions & 6 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,3 @@ func WithAPIKey(key string) Option {
c.APIKey = key
}
}

func WithDebugMod() Option {
return func(c *Config) {
c.Debug = true
}
}
51 changes: 3 additions & 48 deletions internal/httpx/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"errors"
"fmt"
"io"
"log/slog"
"net/http"
"net/url"
"time"
Expand All @@ -23,14 +22,11 @@ type Client struct {
contentType string
timeout time.Duration
response http.Response
logger Logger
debug bool
error error
}

type Response struct {
response *http.Response
logger Logger
}

func NewClient(rawURL string, opts ...Option) *Client {
Expand All @@ -47,9 +43,7 @@ func NewClient(rawURL string, opts ...Option) *Client {
opt(cli)
}
}
if cli.logger == nil {
cli.logger = slog.Default()
}

return cli
}

Expand All @@ -75,9 +69,8 @@ func (c *Client) AddHeaders(kv map[string]string) *Client {
return c
}

func (c *Client) AddPath(paths ...string) *Client {
c.url = c.url.JoinPath(paths...)
//c.url.JoinPath(paths...)
func (c *Client) SetPath(path string) *Client {
c.url.Path = path
return c
}

Expand Down Expand Up @@ -169,60 +162,22 @@ func (c *Client) do(ctxs ...context.Context) (*Response, error) {
req.Header.Set(tools.ContentTypeHeaderKey, c.contentType)
}

if c.debug {
c.logger.Info("request",
slog.String("url", c.url.String()),
slog.String("body", fmt.Sprintf("%+v", c.body)),
slog.String("header", fmt.Sprintf("%+v", req.Header)),
)
}

client := http.Client{
Timeout: c.timeout,
}

if c.debug {
c.logger.Info("client",
slog.Duration("timeout", client.Timeout),
)
}

resp, err := client.Do(req.WithContext(ctx))
if err != nil {
return nil, errors.New(fmt.Sprintf("client Do(): %v", err))
}

response := &Response{
response: resp,
logger: c.logger,
}

if c.debug {
slog.Info("response",
slog.String("resp", response.String()),
)
}

return response, nil
}

func (r *Response) String() string {
b := r.response.Body
if b == nil {
return ""
}
defer func() {
_ = r.response.Body.Close()
}()
body, err := io.ReadAll(b)
if err != nil {
r.logger.Error(err.Error())
return ""
}
r.response.Body = io.NopCloser(bytes.NewBuffer(body))
return fmt.Sprintf("Status: %s\n Body: %s", r.response.Status, string(body))
}

func (r *Response) Unmarshal(body any) error {
if body == nil || r.Raw() == nil {
return errors.New(fmt.Sprintf("response is nil or input body id nil"))
Expand Down
14 changes: 0 additions & 14 deletions internal/httpx/logger.go

This file was deleted.

12 changes: 0 additions & 12 deletions internal/httpx/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,3 @@ func WithTimeout(d time.Duration) Option {
c.timeout = d
}
}

func WithDebug(b bool) Option {
return func(c *Client) {
c.debug = b
}
}

func WithLogger(logger Logger) Option {
return func(c *Client) {
c.logger = logger
}
}

0 comments on commit 6f61681

Please sign in to comment.