From 952bfe906ec55489dc506ab2e5a02c835177bc6c Mon Sep 17 00:00:00 2001 From: Tai Groot Date: Tue, 18 Apr 2023 13:26:28 -0700 Subject: [PATCH] use endpoint built into client --- client/client.go | 13 +++++++++++-- client/history.go | 13 ++++++------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/client/client.go b/client/client.go index 6e10d8d..c7732e5 100644 --- a/client/client.go +++ b/client/client.go @@ -5,9 +5,18 @@ const apiEndpoint = "https://api.elevenlabs.io" var ErrUnauthorized error type Client struct { - apiKey string + apiKey string + endpoint string } func New(apiKey string) Client { - return Client{apiKey: apiKey} + return Client{ + apiKey: apiKey, + endpoint: apiEndpoint, + } +} + +func (c Client) WithEndpoint(endpoint string) Client { + c.endpoint = endpoint + return c } diff --git a/client/history.go b/client/history.go index fdab811..d945330 100644 --- a/client/history.go +++ b/client/history.go @@ -12,8 +12,7 @@ import ( ) func (c Client) HistoryDelete(ctx context.Context, historyItemId string) (bool, error) { - // create path and map variables - url := fmt.Sprintf(apiEndpoint+"/v1/history/%s", historyItemId) + url := fmt.Sprintf(c.endpoint+"/v1/history/%s", historyItemId) client := &http.Client{} req, err := http.NewRequestWithContext(ctx, http.MethodDelete, url, nil) @@ -39,7 +38,7 @@ func (c Client) HistoryDelete(ctx context.Context, historyItemId string) (bool, } func (c Client) HistoryDownloadZipWriter(ctx context.Context, w io.Writer, id1, id2 string, additionalIDs ...string) error { - url := apiEndpoint + "/v1/history/download" + url := c.endpoint + "/v1/history/download" downloads := append(additionalIDs, id1, id2) toDownload := types.HistoryPost{ HistoryItemIds: downloads, @@ -59,7 +58,7 @@ func (c Client) HistoryDownloadZipWriter(ctx context.Context, w io.Writer, id1, } func (c Client) HistoryDownloadZip(ctx context.Context, id1, id2 string, additionalIDs ...string) ([]byte, error) { - url := apiEndpoint + "/v1/history/download" + url := c.endpoint + "/v1/history/download" downloads := append(additionalIDs, id1, id2) toDownload := types.HistoryPost{ HistoryItemIds: downloads, @@ -78,7 +77,7 @@ func (c Client) HistoryDownloadZip(ctx context.Context, id1, id2 string, additio } func (c Client) HistoryDownloadAudioWriter(ctx context.Context, w io.Writer, ID string) error { - url := fmt.Sprintf(apiEndpoint+"/v1/history/%s/audio", ID) + url := fmt.Sprintf(c.endpoint+"/v1/history/%s/audio", ID) client := &http.Client{} req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) if err != nil { @@ -91,7 +90,7 @@ func (c Client) HistoryDownloadAudioWriter(ctx context.Context, w io.Writer, ID } func (c Client) HistoryDownloadAudio(ctx context.Context, ID string) ([]byte, error) { - url := fmt.Sprintf(apiEndpoint+"/v1/history/%s/audio", ID) + url := fmt.Sprintf(c.endpoint+"/v1/history/%s/audio", ID) client := &http.Client{} req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) if err != nil { @@ -102,7 +101,7 @@ func (c Client) HistoryDownloadAudio(ctx context.Context, ID string) ([]byte, er } func (c Client) GetHistoryList(ctx context.Context) ([]string, error) { - url := apiEndpoint + "/v1/history" + url := c.endpoint + "/v1/history" client := &http.Client{} req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) if err != nil {