diff --git a/README.md b/README.md index b2f45427..e44ea841 100644 --- a/README.md +++ b/README.md @@ -96,9 +96,10 @@ These are standalone projects, so you will need to follow the instructions in th ## Remote Files ```go -dg := deepgram.NewClient("DEEPGRAM_API_KEY") +dg := prerecorded.NewClient("DEEPGRAM_API_KEY") +prClient := api.New(dg) -res, err := dg.PreRecordedFromURL(deepgram.UrlSource{Url: "https://static.deepgram.com/examples/Bueller-Life-moves-pretty-fast.wav"}, +res, err := prClient.PreRecordedFromURL(deepgram.UrlSource{Url: "https://static.deepgram.com/examples/Bueller-Life-moves-pretty-fast.wav"}, deepgram.PreRecordedTranscriptionOptions{Punctuate: true, Utterances: true}) ``` @@ -113,15 +114,17 @@ deepgram.PreRecordedTranscriptionOptions{Punctuate: true, Utterances: true}) ## Local files ```go -dg := deepgram.NewClient("DEEPGRAM_API_KEY") +dg := prerecorded.NewClient("DEEPGRAM_API_KEY") +prClient := api.New(dg) + file, err := os.Open("PATH_TO_LOCAL_FILE") if err != nil { fmt.Printf("error opening file %s:", file.Name()) } -source := deepgram.ReadStreamSource{Stream: file, Mimetype: "MIMETYPE_OF_YOUR_FILE"} -res, err := dg.PreRecordedFromStream(source, deepgram.PreRecordedTranscriptionOptions{Punctuate: true}) +source := api.ReadStreamSource{Stream: file, Mimetype: "MIMETYPE_OF_YOUR_FILE"} +res, err := prClient.PreRecordedFromStream(source, deepgram.PreRecordedTranscriptionOptions{Punctuate: true}) if err != nil { fmt.Println("ERROR", err) return @@ -169,7 +172,7 @@ if err != nil { ```go // The request can be from a local file stream or a URL // Turn on utterances with {Utterances: true} for captions to work -res, err := dg.PreRecordedFromStream(source, deepgram.PreRecordedTranscriptionOptions{Punctuate: true, Utterances:true}) +res, err := prClient.PreRecordedFromStream(source, deepgram.PreRecordedTranscriptionOptions{Punctuate: true, Utterances:true}) // Convert the results to WebVTT format vtt, err := res.ToWebVTT() @@ -181,12 +184,11 @@ stt, err := res.ToSRT() ## Live Audio ```go -dg := *deepgram.NewClient("DEEPGRAM_API_KEY") options := deepgram.LiveTranscriptionOptions{ Language: "en-US", Punctuate: true, } -ws, _, err := dg.LiveTranscription(options) +dg, _, err := live.New(options, "DEEPGRAM_API_KEY") ``` #### LiveTranscriptionOptions @@ -202,8 +204,9 @@ See [API Reference](https://developers.deepgram.com/reference/streaming) Returns all projects accessible by the API key. ```go -dg := deepgram.NewClient("YOUR_API_KEY") -res, err := dg.ListProjects() +dg := manage.New("YOUR_API_KEY") +mgClient := api.New(dg) +res, err := mgClient.ListProjects() ``` [See our API reference for more info](https://developers.deepgram.com/reference/get-projects). @@ -213,8 +216,9 @@ res, err := dg.ListProjects() Retrieves a specific project based on the provided projectId. ```go -dg := deepgram.NewClient("YOUR_API_KEY") -res, err := dg.GetProject(projectId) +dg := manage.New("YOUR_API_KEY") +mgClient := api.New(dg) +res, err := mgClient.GetProject(projectId) ``` [See our API reference for more info](https://developers.deepgram.com/reference/get-project). @@ -224,14 +228,16 @@ res, err := dg.GetProject(projectId) Update a project. ```go -dg := deepgram.NewClient("YOUR_API_KEY") +dg := manage.New("YOUR_API_KEY") +mgClient := api.New(dg) + projectID := "YOUR_PROJECT_ID" options := deepgram.ProjectUpdateOptions{ Name: "NAME_OF_PROJECT", Company:"COMPANY", } -res, err := dg.UpdateProject(projectID, options) +res, err := mgClient.UpdateProject(projectID, options) ``` **Project Type** @@ -249,10 +255,12 @@ res, err := dg.UpdateProject(projectID, options) Delete a project. ```go -dg := deepgram.NewClient("YOUR_API_KEY") +dg := manage.New("YOUR_API_KEY") +mgClient := api.New(dg) + projectID := "YOUR_PROJECT_ID" -res, err := dg.DeleteProject(projectID) +res, err := mgClient.DeleteProject(projectID) ``` [See our API reference for more info](https://developers.deepgram.com/reference/delete-project). @@ -266,10 +274,12 @@ res, err := dg.DeleteProject(projectID) Retrieves all keys associated with the provided project_id. ```go -dg := deepgram.NewClient("YOUR_API_KEY") +dg := manage.New("YOUR_API_KEY") +mgClient := api.New(dg) + projectID := "YOUR_PROJECT_ID" -res, err := dg.ListKeys(projectID) +res, err := mgClient.ListKeys(projectID) ``` [See our API reference for more info](https://developers.deepgram.com/reference/list-keys). @@ -279,11 +289,13 @@ res, err := dg.ListKeys(projectID) Retrieves a specific key associated with the provided project_id. ```go -dg := deepgram.NewClient("YOUR_API_KEY") +dg := manage.New("YOUR_API_KEY") +mgClient := api.New(dg) + projectID := "YOUR_PROJECT_ID" keyID := "YOUR_KEY_ID" -res, err := dg.GetKey(projectID, keyID) +res, err := mgClient.GetKey(projectID, keyID) ``` [See our API reference for more info](https://developers.deepgram.com/reference/get-key). @@ -293,7 +305,9 @@ res, err := dg.GetKey(projectID, keyID) Creates an API key with the provided scopes. ```go -dg := deepgram.NewClient("YOUR_API_KEY") +dg := manage.New("YOUR_API_KEY") +mgClient := api.New(dg) + projectID := "YOUR_PROJECT_ID" comment := "A comment" scopes := []string{"admin", "member"} @@ -303,7 +317,7 @@ options := deepgram.CreateKeyOptions{ Tags: []string{"tag1", "tag2"}, } -res, err := dg.CreateKey(projectID, comment, scopes, options) +res, err := mgClient.CreateKey(projectID, comment, scopes, options) ``` @@ -314,11 +328,13 @@ res, err := dg.CreateKey(projectID, comment, scopes, options) Deletes a specific key associated with the provided project_id. ```go -dg := deepgram.NewClient("YOUR_API_KEY") +dg := manage.New("YOUR_API_KEY") +mgClient := api.New(dg) + projectID := "YOUR_PROJECT_ID" keyID := "YOUR_KEY_ID" -res, err := dg.DeleteKey(projectID, keyID) +res, err := mgClient.DeleteKey(projectID, keyID) ``` [See our API reference for more info](https://developers.deepgram.com/reference/delete-key). @@ -332,10 +348,12 @@ res, err := dg.DeleteKey(projectID, keyID) Retrieves account objects for all of the accounts in the specified project_id. ```go -dg := deepgram.NewClient("YOUR_API_KEY") +dg := manage.New("YOUR_API_KEY") +mgClient := api.New(dg) + projectID := "YOUR_PROJECT_ID" -res, err := dg.ListMembers(projectID) +res, err := mgClient.ListMembers(projectID) ``` @@ -346,11 +364,13 @@ res, err := dg.ListMembers(projectID) Removes member account for specified member_id. ```go -dg := deepgram.NewClient("YOUR_API_KEY") +dg := manage.New("YOUR_API_KEY") +mgClient := api.New(dg) + projectID := "YOUR_PROJECT_ID" memberID := "YOUR_MEMBER_ID" -res, err := dg.RemoveMember(projectID, memberID) +res, err := mgClient.RemoveMember(projectID, memberID) ``` [See our API reference for more info](https://developers.deepgram.com/reference/remove-member). @@ -364,11 +384,13 @@ res, err := dg.RemoveMember(projectID, memberID) Retrieves scopes of the specified member in the specified project. ```go -dg := deepgram.NewClient("YOUR_API_KEY") +dg := manage.New("YOUR_API_KEY") +mgClient := api.New(dg) + projectID := "YOUR_PROJECT_ID" memberID := "YOUR_MEMBER_ID" -res, err := dg.GetMemberScopes(projectID, memberID) +res, err := mgClient.GetMemberScopes(projectID, memberID) ``` [See our API reference for more info](https://developers.deepgram.com/reference/get-member-scopes). @@ -378,12 +400,14 @@ res, err := dg.GetMemberScopes(projectID, memberID) Updates the scope for the specified member in the specified project. ```go -dg := deepgram.NewClient("YOUR_API_KEY") +dg := manage.New("YOUR_API_KEY") +mgClient := api.New(dg) + projectID := "YOUR_PROJECT_ID" memberID := "THEIR_MEMBER_ID" scope := "SCOPE_TO_ASSIGN" -res, err := dg.UpdateMemberScopes(projectID, memberID, scope) +res, err := mgClient.UpdateMemberScopes(projectID, memberID, scope) ``` [See our API reference for more info](https://developers.deepgram.com/reference/update-scope). @@ -395,10 +419,12 @@ res, err := dg.UpdateMemberScopes(projectID, memberID, scope) Retrieves all invitations associated with the provided project_id. ```go -dg := deepgram.NewClient("YOUR_API_KEY") +dg := manage.New("YOUR_API_KEY") +mgClient := api.New(dg) + projectID := "YOUR_PROJECT_ID" -res, err := dg.ListInvitations(projectID) +res, err := mgClient.ListInvitations(projectID) ``` [See our API reference for more info](https://developers.deepgram.com/reference/list-invites). @@ -408,14 +434,16 @@ res, err := dg.ListInvitations(projectID) Sends an invitation to the provided email address. ```go -dg := deepgram.NewClient("YOUR_API_KEY") +dg := manage.New("YOUR_API_KEY") +mgClient := api.New(dg) + projectID := "YOUR_PROJECT_ID" invitationOptions := deepgram.InvitationOptions{ Email: "", Scope: "", } -res, err := dg.SendInvitation(projectID, invitationOptions) +res, err := mgClient.SendInvitation(projectID, invitationOptions) ``` [See our API reference for more info](https://developers.deepgram.com/reference/send-invites). @@ -425,11 +453,13 @@ res, err := dg.SendInvitation(projectID, invitationOptions) Removes the specified invitation from the project. ```go -dg := deepgram.NewClient("YOUR_API_KEY") +dg := manage.New("YOUR_API_KEY") +mgClient := api.New(dg) + projectID := "YOUR_PROJECT_ID" email := "" -res, err := dg.DeleteInvitation(projectID, email) +res, err := mgClient.DeleteInvitation(projectID, email) ``` [See our API reference for more info](https://developers.deepgram.com/reference/delete-invite). @@ -439,10 +469,12 @@ res, err := dg.DeleteInvitation(projectID, email) Removes the authenticated user from the project. ```go -dg := deepgram.NewClient("YOUR_API_KEY") +dg := manage.New("YOUR_API_KEY") +mgClient := api.New(dg) + projectID := "YOUR_PROJECT_ID" -res, err := dg.LeaveProject(projectID) +res, err := mgClient.LeaveProject(projectID) ``` [See our API reference for more info](https://developers.deepgram.com/reference/leave-project). @@ -456,7 +488,9 @@ res, err := dg.LeaveProject(projectID) Retrieves all requests associated with the provided projectId based on the provided options. ```go -dg := deepgram.NewClient("YOUR_API_KEY") +dg := manage.New("YOUR_API_KEY") +mgClient := api.New(dg) + projectID := "YOUR_PROJECT_ID" options := deepgram.UsageRequestListOptions{ Start: "2009-11-10", @@ -466,7 +500,7 @@ options := deepgram.UsageRequestListOptions{ Status: "failed", } -res, err := dg.ListRequests(projectID, options) +res, err := mgClient.ListRequests(projectID, options) ``` #### UsageRequestListOptions @@ -486,10 +520,12 @@ res, err := dg.ListRequests(projectID, options) Retrieves a specific request associated with the provided projectId. ```go -dg := deepgram.NewClient("YOUR_API_KEY") +dg := manage.New("YOUR_API_KEY") +mgClient := api.New(dg) + projectID := "YOUR_PROJECT_ID" requestID := "REQUEST_ID" -res, err := dg.GetRequest(projectID, requestID) +res, err := mgClient.GetRequest(projectID, requestID) ``` [See our API reference for more info](https://developers.deepgram.com/reference/get-request). @@ -499,14 +535,16 @@ res, err := dg.GetRequest(projectID, requestID) Retrieves usage associated with the provided project_id based on the provided options. ```go -dg := deepgram.NewClient("YOUR_API_KEY") +dg := manage.New("YOUR_API_KEY") +mgClient := api.New(dg) + projectID := "YOUR_PROJECT_ID" options := deepgram.UsageOptions{ Start: "2009-11-10", End: "2029-11-10", } -res, err := dg.GetUsage(projectID, options) +res, err := mgClient.GetUsage(projectID, options) ``` #### UsageOptions @@ -523,13 +561,15 @@ res, err := dg.GetUsage(projectID, options) Lists the features, models, tags, languages, and processing method used for requests in the specified project. ```go -dg := deepgram.NewClient("YOUR_API_KEY") +dg := manage.New("YOUR_API_KEY") +mgClient := api.New(dg) + projectID := "YOUR_PROJECT_ID" options := deepgram.UsageRequestListOptions{ Start: "2009-11-10", End: "2029-11-10", } -res, err := dg.GetFields(projectID, options) +res, err := mgClient.GetFields(projectID, options) ``` #### GetUsageFieldsOptions diff --git a/docs.go b/docs.go new file mode 100644 index 00000000..f4e4986f --- /dev/null +++ b/docs.go @@ -0,0 +1,6 @@ +package sdk + +import ( + _ "github.com/deepgram-devs/deepgram-go-sdk/pkg/client/live" + _ "github.com/deepgram-devs/deepgram-go-sdk/pkg/client/prerecorded" +) diff --git a/examples/README.md b/examples/README.md index ad702798..93e16f6c 100644 --- a/examples/README.md +++ b/examples/README.md @@ -18,12 +18,12 @@ If the project requires third-party dependencies and not just standard library d go mod tidy ``` -### Edit the API key, the file, and the mimetype (as needed) +### Set your API Key as an Environment Variable named "DEEPGRAM_API_KEY" -Replace the API key where it says "YOUR_DEEPGRAM_API_KEY" +If using bash, this could be done in your `.bash_profile` or this could also be done by a simple export seen below. -```go -DEEPGRAM_API_KEY = "YOUR_DEEPGRAM_API_KEY" +```bash +export DEEPGRAM_API_KEY = "YOUR_DEEPGRAM_API_KEY" ``` ### Run the project diff --git a/examples/prerecorded/main.go b/examples/prerecorded/main.go index cfb7c650..d8dd6fa6 100644 --- a/examples/prerecorded/main.go +++ b/examples/prerecorded/main.go @@ -7,21 +7,32 @@ import ( "os" "strings" - "github.com/deepgram-devs/deepgram-go-sdk/deepgram" + api "github.com/deepgram-devs/deepgram-go-sdk/pkg/api/prerecorded" + client "github.com/deepgram-devs/deepgram-go-sdk/pkg/client/prerecorded" ) func main() { - credentials := "DEEPGRAM_API_KEY" - dg := deepgram.NewClient(credentials) + var deepgramApiKey string + if v := os.Getenv("DEEPGRAM_API_KEY"); v != "" { + log.Println("DEEPGRAM_API_KEY found") + deepgramApiKey = v + } else { + log.Fatal("DEEPGRAM_API_KEY not found") + os.Exit(1) + } + + dg := client.New(deepgramApiKey) + + prClient := api.New(dg) filePath := "https://static.deepgram.com/examples/Bueller-Life-moves-pretty-fast.wav" var res interface{} var err error if isURL(filePath) { - res, err = dg.PreRecordedFromURL( - deepgram.UrlSource{Url: filePath}, - deepgram.PreRecordedTranscriptionOptions{ + res, err = prClient.PreRecordedFromURL( + api.UrlSource{Url: filePath}, + api.PreRecordedTranscriptionOptions{ Punctuate: true, Diarize: true, Language: "en-US", @@ -39,11 +50,11 @@ func main() { } defer file.Close() - source := deepgram.ReadStreamSource{Stream: file, Mimetype: "YOUR_FILE_MIME_TYPE"} + source := api.ReadStreamSource{Stream: file, Mimetype: "YOUR_FILE_MIME_TYPE"} - res, err = dg.PreRecordedFromStream( + res, err = prClient.PreRecordedFromStream( source, - deepgram.PreRecordedTranscriptionOptions{ + api.PreRecordedTranscriptionOptions{ Punctuate: true, Diarize: true, Language: "en-US", diff --git a/examples/projects/main.go b/examples/projects/main.go new file mode 100644 index 00000000..e75ab6a6 --- /dev/null +++ b/examples/projects/main.go @@ -0,0 +1,34 @@ +package main + +import ( + "log" + "os" + + api "github.com/deepgram-devs/deepgram-go-sdk/pkg/api/manage" + client "github.com/deepgram-devs/deepgram-go-sdk/pkg/client/prerecorded" +) + +func main() { + var deepgramApiKey string + if v := os.Getenv("DEEPGRAM_API_KEY"); v != "" { + log.Println("DEEPGRAM_API_KEY found") + deepgramApiKey = v + } else { + log.Fatal("DEEPGRAM_API_KEY not found") + os.Exit(1) + } + + dg := client.New(deepgramApiKey) + + mgClient := api.New(dg) + + resp, err := mgClient.ListProjects() + if err != nil { + log.Printf("ListProjects failed. Err: %v\n", err) + os.Exit(1) + } + + for _, project := range resp.Projects { + log.Printf("Name: %s\n", project.Name) + } +} diff --git a/examples/streaming/main.go b/examples/streaming/main.go index c3b5233b..5ea094cb 100644 --- a/examples/streaming/main.go +++ b/examples/streaming/main.go @@ -5,27 +5,36 @@ import ( "fmt" "log" "net/http" + "os" "reflect" "time" - "github.com/Jeffail/gabs/v2" - "github.com/deepgram-devs/deepgram-go-sdk/deepgram" - "github.com/gorilla/websocket" + gabs "github.com/Jeffail/gabs/v2" + "github.com/dvonthenen/websocket" + + client "github.com/deepgram-devs/deepgram-go-sdk/pkg/client/live" ) const ( - DEEPGRAM_API_KEY = "DEEPGRAM_API_KEY" STREAM_URL = "http://stream.live.vc.bbcmedia.co.uk/bbc_world_service" CHUNK_SIZE = 1024 * 2 TEN_MILLISECONDS_SLEEP = 10 * time.Millisecond ) func main() { - client := new(http.Client) + var deepgramApiKey string + if v := os.Getenv("DEEPGRAM_API_KEY"); v != "" { + log.Println("DEEPGRAM_API_KEY found") + deepgramApiKey = v + } else { + log.Fatal("DEEPGRAM_API_KEY not found") + os.Exit(1) + } - dg := *deepgram.NewClient(DEEPGRAM_API_KEY) + // HTTP client + httpClient := new(http.Client) - res, err := client.Get(STREAM_URL) + res, err := httpClient.Get(STREAM_URL) if err != nil { log.Println("ERROR getting stream", err) return @@ -36,20 +45,21 @@ func main() { reader := bufio.NewReader(res.Body) - liveTranscriptionOptions := deepgram.LiveTranscriptionOptions{ + // live transcription + liveTranscriptionOptions := client.LiveTranscriptionOptions{ Language: "en-US", Punctuate: true, } - dgConn, _, err := dg.LiveTranscription(liveTranscriptionOptions) + dgConn, _, err := client.New(deepgramApiKey, liveTranscriptionOptions) if err != nil { log.Println("ERROR creating LiveTranscription connection:", err) return } defer dgConn.Close() + // process messages chunk := make([]byte, CHUNK_SIZE) - go func() { for { _, message, err := dgConn.ReadMessage() diff --git a/go.mod b/go.mod index facb49c6..f8c1bf6c 100644 --- a/go.mod +++ b/go.mod @@ -14,4 +14,4 @@ require ( golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect ) -require github.com/google/go-cmp v0.5.9 // indirect +require github.com/dvonthenen/websocket v1.5.1-dyv.2 // indirect diff --git a/go.sum b/go.sum index 6928e882..70232da2 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,9 @@ +github.com/Jeffail/gabs/v2 v2.6.1 h1:wwbE6nTQTwIMsMxzi6XFQQYRZ6wDc1mSdxoAN+9U4Gk= +github.com/Jeffail/gabs/v2 v2.6.1/go.mod h1:xCn81vdHKxFUuWWAaD5jCTQDNPBMh5pPs9IJ+NcziBI= github.com/Jeffail/gabs/v2 v2.7.0 h1:Y2edYaTcE8ZpRsR2AtmPu5xQdFDIthFG0jYhu5PY8kg= github.com/Jeffail/gabs/v2 v2.7.0/go.mod h1:dp5ocw1FvBBQYssgHsG7I1WYsiLRtkUaB1FEtSwvNUw= +github.com/dvonthenen/websocket v1.5.1-dyv.2 h1:OXlWJJkeHt8k4+MEI0Y8SQjY2ihHYD2z/tI7sZZfsnA= +github.com/dvonthenen/websocket v1.5.1-dyv.2/go.mod h1:q2GbopbpFJvBP4iqVvqwwahVmvu2HnCfdqCWDoQVKMM= github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= @@ -10,3 +14,5 @@ github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/ad github.com/jarcoal/httpmock v1.3.0 h1:2RJ8GP0IIaWwcC9Fp2BmVi8Kog3v2Hn7VXM3fTd+nuc= github.com/jarcoal/httpmock v1.3.0/go.mod h1:3yb8rc4BI7TCBhFY8ng0gjuLKJNquuDNiPaZjnENuYg= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 h1:H2TDz8ibqkAF6YGhCdN3jS9O0/s90v0rJh3X/OLHEUk= +golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8= diff --git a/deepgram/billing.go b/pkg/api/manage/billing.go similarity index 75% rename from deepgram/billing.go rename to pkg/api/manage/billing.go index aa82e3aa..03c6f77c 100644 --- a/deepgram/billing.go +++ b/pkg/api/manage/billing.go @@ -1,4 +1,4 @@ -package deepgram +package manage import ( "fmt" @@ -19,10 +19,10 @@ type BalanceList struct { Balances []Balance `json:"balances"` } -func (dg *Client) ListBalances(projectId string) (BalanceList, error) { +func (dg *ManageClient) ListBalances(projectId string) (BalanceList, error) { client := new(http.Client) - path := fmt.Sprintf("%s/%s/balances", dg.Path, projectId) - u := url.URL{Scheme: "https", Host: dg.Host, Path: path} + path := fmt.Sprintf("%s/%s/balances", dg.Client.Path, projectId) + u := url.URL{Scheme: "https", Host: dg.Client.Host, Path: path} req, err := http.NewRequest("GET", u.String(), nil) if err != nil { //Handle Error @@ -30,7 +30,7 @@ func (dg *Client) ListBalances(projectId string) (BalanceList, error) { } req.Header = http.Header{ - "Host": []string{dg.Host}, + "Host": []string{dg.Client.Host}, "Content-Type": []string{"application/json"}, "Authorization": []string{"token " + dg.ApiKey}, "User-Agent": []string{dgAgent}, @@ -55,10 +55,10 @@ func (dg *Client) ListBalances(projectId string) (BalanceList, error) { } } -func (dg *Client) GetBalance(projectId string, balanceId string) (Balance, error) { +func (dg *ManageClient) GetBalance(projectId string, balanceId string) (Balance, error) { client := new(http.Client) - path := fmt.Sprintf("%s/%s/balances/%s", dg.Path, projectId, balanceId) - u := url.URL{Scheme: "https", Host: dg.Host, Path: path} + path := fmt.Sprintf("%s/%s/balances/%s", dg.Client.Path, projectId, balanceId) + u := url.URL{Scheme: "https", Host: dg.Client.Host, Path: path} req, err := http.NewRequest("GET", u.String(), nil) if err != nil { //Handle Error @@ -66,7 +66,7 @@ func (dg *Client) GetBalance(projectId string, balanceId string) (Balance, error } req.Header = http.Header{ - "Host": []string{dg.Host}, + "Host": []string{dg.Client.Host}, "Content-Type": []string{"application/json"}, "Authorization": []string{"token " + dg.ApiKey}, "User-Agent": []string{dgAgent}, diff --git a/deepgram/invitations.go b/pkg/api/manage/invitations.go similarity index 68% rename from deepgram/invitations.go rename to pkg/api/manage/invitations.go index 667b7f78..38853bb1 100644 --- a/deepgram/invitations.go +++ b/pkg/api/manage/invitations.go @@ -1,4 +1,4 @@ -package deepgram +package manage import ( "bytes" @@ -9,10 +9,10 @@ import ( "net/url" ) -func (dg *Client) ListInvitations(projectId string) (InvitationList, error) { +func (dg *ManageClient) ListInvitations(projectId string) (InvitationList, error) { client := new(http.Client) - path := fmt.Sprintf("%s/%s/invites", dg.Path, projectId) - u := url.URL{Scheme: "https", Host: dg.Host, Path: path} + path := fmt.Sprintf("%s/%s/invites", dg.Client.Path, projectId) + u := url.URL{Scheme: "https", Host: dg.Client.Host, Path: path} req, err := http.NewRequest("GET", u.String(), nil) if err != nil { @@ -21,7 +21,7 @@ func (dg *Client) ListInvitations(projectId string) (InvitationList, error) { } req.Header = http.Header{ - "Host": []string{dg.Host}, + "Host": []string{dg.Client.Host}, "Content-Type": []string{"application/json"}, "Authorization": []string{"token " + dg.ApiKey}, "User-Agent": []string{dgAgent}, @@ -41,10 +41,10 @@ func (dg *Client) ListInvitations(projectId string) (InvitationList, error) { } } -func (dg *Client) SendInvitation(projectId string, options InvitationOptions) (Message, error) { +func (dg *ManageClient) SendInvitation(projectId string, options InvitationOptions) (Message, error) { client := new(http.Client) - path := fmt.Sprintf("%s/%s/invites", dg.Path, projectId) - u := url.URL{Scheme: "https", Host: dg.Host, Path: path} + path := fmt.Sprintf("%s/%s/invites", dg.Client.Path, projectId) + u := url.URL{Scheme: "https", Host: dg.Client.Host, Path: path} jsonStr, err := json.Marshal(options) if err != nil { log.Panic(err) @@ -57,7 +57,7 @@ func (dg *Client) SendInvitation(projectId string, options InvitationOptions) (M } req.Header = http.Header{ - "Host": []string{dg.Host}, + "Host": []string{dg.Client.Host}, "Content-Type": []string{"application/json"}, "Authorization": []string{"token " + dg.ApiKey}, "User-Agent": []string{dgAgent}, @@ -80,11 +80,11 @@ func (dg *Client) SendInvitation(projectId string, options InvitationOptions) (M } } -func (dg *Client) DeleteInvitation(projectId string, email string) (Message, error) { +func (dg *ManageClient) DeleteInvitation(projectId string, email string) (Message, error) { client := new(http.Client) - // url := fmt.Sprintf("%s%s/%s/invites/%s", dg.Host, dg.Path, projectId, email) - path := fmt.Sprintf("%s/%s/invites/%s", dg.Path, projectId, email) - u := url.URL{Scheme: "https", Host: dg.Host, Path: path} + // url := fmt.Sprintf("%s%s/%s/invites/%s", dg.Client.Host, dg.Client.Path, projectId, email) + path := fmt.Sprintf("%s/%s/invites/%s", dg.Client.Path, projectId, email) + u := url.URL{Scheme: "https", Host: dg.Client.Host, Path: path} req, err := http.NewRequest("DELETE", u.String(), nil) if err != nil { //Handle Error @@ -92,7 +92,7 @@ func (dg *Client) DeleteInvitation(projectId string, email string) (Message, err } req.Header = http.Header{ - "Host": []string{dg.Host}, + "Host": []string{dg.Client.Host}, "Content-Type": []string{"application/json"}, "Authorization": []string{"token " + dg.ApiKey}, "User-Agent": []string{dgAgent}, diff --git a/deepgram/keys.go b/pkg/api/manage/keys.go similarity index 78% rename from deepgram/keys.go rename to pkg/api/manage/keys.go index 5405e767..b1cb3d3f 100644 --- a/deepgram/keys.go +++ b/pkg/api/manage/keys.go @@ -1,4 +1,4 @@ -package deepgram +package manage import ( "bytes" @@ -41,10 +41,10 @@ type CreateKeyRequest struct { TimeToLive int `json:"time_to_live,omitempty"` } -func (dg *Client) ListKeys(projectId string) (KeyResponse, error) { +func (dg *ManageClient) ListKeys(projectId string) (KeyResponse, error) { client := new(http.Client) - path := fmt.Sprintf("%s/%s/keys", dg.Path, projectId) - u := url.URL{Scheme: "https", Host: dg.Host, Path: path} + path := fmt.Sprintf("%s/%s/keys", dg.Client.Path, projectId) + u := url.URL{Scheme: "https", Host: dg.Client.Host, Path: path} req, err := http.NewRequest("GET", u.String(), nil) if err != nil { //Handle Error @@ -52,7 +52,7 @@ func (dg *Client) ListKeys(projectId string) (KeyResponse, error) { } req.Header = http.Header{ - "Host": []string{dg.Host}, + "Host": []string{dg.Client.Host}, "Content-Type": []string{"application/json"}, "Authorization": []string{"token " + dg.ApiKey}, "User-Agent": []string{dgAgent}, @@ -77,10 +77,10 @@ func (dg *Client) ListKeys(projectId string) (KeyResponse, error) { } } -func (dg *Client) GetKey(projectId string, keyId string) (KeyResponseObj, error) { +func (dg *ManageClient) GetKey(projectId string, keyId string) (KeyResponseObj, error) { client := new(http.Client) - path := fmt.Sprintf("%s/%s/keys/%s", dg.Path, projectId, keyId) - u := url.URL{Scheme: "https", Host: dg.Host, Path: path} + path := fmt.Sprintf("%s/%s/keys/%s", dg.Client.Path, projectId, keyId) + u := url.URL{Scheme: "https", Host: dg.Client.Host, Path: path} req, err := http.NewRequest("GET", u.String(), nil) if err != nil { //Handle Error @@ -88,7 +88,7 @@ func (dg *Client) GetKey(projectId string, keyId string) (KeyResponseObj, error) } req.Header = http.Header{ - "Host": []string{dg.Host}, + "Host": []string{dg.Client.Host}, "Content-Type": []string{"application/json"}, "Authorization": []string{"token " + dg.ApiKey}, "User-Agent": []string{dgAgent}, @@ -114,7 +114,7 @@ func (dg *Client) GetKey(projectId string, keyId string) (KeyResponseObj, error) } } -func (dg *Client) CreateKey(projectId string, comment string, scopes []string, options CreateKeyOptions) (Key, error) { +func (dg *ManageClient) CreateKey(projectId string, comment string, scopes []string, options CreateKeyOptions) (Key, error) { var expirationDate string if options.ExpirationDate.IsZero() { expirationDate = "" @@ -133,8 +133,8 @@ func (dg *Client) CreateKey(projectId string, comment string, scopes []string, o log.Panic(err) } client := new(http.Client) - path := fmt.Sprintf("%s/%s/keys", dg.Path, projectId) - u := url.URL{Scheme: "https", Host: dg.Host, Path: path} + path := fmt.Sprintf("%s/%s/keys", dg.Client.Path, projectId) + u := url.URL{Scheme: "https", Host: dg.Client.Host, Path: path} req, err := http.NewRequest("POST", u.String(), buf) if err != nil { //Handle Error @@ -142,7 +142,7 @@ func (dg *Client) CreateKey(projectId string, comment string, scopes []string, o } req.Header = http.Header{ - "Host": []string{dg.Host}, + "Host": []string{dg.Client.Host}, "Content-Type": []string{"application/json"}, "Authorization": []string{"token " + dg.ApiKey}, "User-Agent": []string{dgAgent}, @@ -168,10 +168,10 @@ func (dg *Client) CreateKey(projectId string, comment string, scopes []string, o } } -func (dg *Client) DeleteKey(projectId string, keyId string) (Message, error) { +func (dg *ManageClient) DeleteKey(projectId string, keyId string) (Message, error) { client := new(http.Client) - path := fmt.Sprintf("%s/%s/keys/%s", dg.Path, projectId, keyId) - u := url.URL{Scheme: "https", Host: dg.Host, Path: path} + path := fmt.Sprintf("%s/%s/keys/%s", dg.Client.Path, projectId, keyId) + u := url.URL{Scheme: "https", Host: dg.Client.Host, Path: path} req, err := http.NewRequest("DELETE", u.String(), nil) if err != nil { //Handle Error @@ -179,7 +179,7 @@ func (dg *Client) DeleteKey(projectId string, keyId string) (Message, error) { } req.Header = http.Header{ - "Host": []string{dg.Host}, + "Host": []string{dg.Client.Host}, "Content-Type": []string{"application/json"}, "Authorization": []string{"token " + dg.ApiKey}, "User-Agent": []string{dgAgent}, diff --git a/pkg/api/manage/manage.go b/pkg/api/manage/manage.go new file mode 100644 index 00000000..59e354f0 --- /dev/null +++ b/pkg/api/manage/manage.go @@ -0,0 +1,35 @@ +package manage + +import ( + "encoding/json" + "net/http" + "runtime" + "strings" + + client "github.com/deepgram-devs/deepgram-go-sdk/pkg/client/prerecorded" +) + +var sdkVersion string = "0.10.0" +var dgAgent string = "@deepgram/sdk/" + sdkVersion + " go/" + goVersion() + +func goVersion() string { + version := runtime.Version() + if strings.HasPrefix(version, "go") { + return version[2:] + } + return version +} + +type ManageClient struct { + *client.Client +} + +func New(client *client.Client) *ManageClient { + return &ManageClient{client} +} + +func GetJson(resp *http.Response, target interface{}) error { + defer resp.Body.Close() + + return json.NewDecoder(resp.Body).Decode(target) +} diff --git a/deepgram/members.go b/pkg/api/manage/members.go similarity index 73% rename from deepgram/members.go rename to pkg/api/manage/members.go index 4f21e584..faa98260 100644 --- a/deepgram/members.go +++ b/pkg/api/manage/members.go @@ -1,4 +1,4 @@ -package deepgram +package manage import ( "fmt" @@ -25,10 +25,10 @@ type ScopeList struct { Scopes []string `json:"scopes"` } -func (dg *Client) ListMembers(projectId string) (MemberList, error) { +func (dg *ManageClient) ListMembers(projectId string) (MemberList, error) { client := new(http.Client) - path := fmt.Sprintf("%s/%s/members", dg.Path, projectId) - u := url.URL{Scheme: "https", Host: dg.Host, Path: path} + path := fmt.Sprintf("%s/%s/members", dg.Client.Path, projectId) + u := url.URL{Scheme: "https", Host: dg.Client.Host, Path: path} req, err := http.NewRequest("GET", u.String(), nil) if err != nil { //Handle Error @@ -36,7 +36,7 @@ func (dg *Client) ListMembers(projectId string) (MemberList, error) { } req.Header = http.Header{ - "Host": []string{dg.Host}, + "Host": []string{dg.Client.Host}, "Content-Type": []string{"application/json"}, "Authorization": []string{"token " + dg.ApiKey}, "User-Agent": []string{dgAgent}, @@ -61,10 +61,10 @@ func (dg *Client) ListMembers(projectId string) (MemberList, error) { } } -func (dg *Client) RemoveMember(projectId string, memberId string) (Message, error) { +func (dg *ManageClient) RemoveMember(projectId string, memberId string) (Message, error) { client := new(http.Client) - path := fmt.Sprintf("%s/%s/members/%s", dg.Path, projectId, memberId) - u := url.URL{Scheme: "https", Host: dg.Host, Path: path} + path := fmt.Sprintf("%s/%s/members/%s", dg.Client.Path, projectId, memberId) + u := url.URL{Scheme: "https", Host: dg.Client.Host, Path: path} req, err := http.NewRequest("DELETE", u.String(), nil) if err != nil { //Handle Error @@ -72,7 +72,7 @@ func (dg *Client) RemoveMember(projectId string, memberId string) (Message, erro } req.Header = http.Header{ - "Host": []string{dg.Host}, + "Host": []string{dg.Client.Host}, "Content-Type": []string{"application/json"}, "Authorization": []string{"token " + dg.ApiKey}, "User-Agent": []string{dgAgent}, @@ -97,10 +97,10 @@ func (dg *Client) RemoveMember(projectId string, memberId string) (Message, erro } } -func (dg *Client) GetMemberScopes(projectId string, memberId string) (ScopeList, error) { +func (dg *ManageClient) GetMemberScopes(projectId string, memberId string) (ScopeList, error) { client := new(http.Client) - path := fmt.Sprintf("%s/%s/members/%s/scopes", dg.Path, projectId, memberId) - u := url.URL{Scheme: "https", Host: dg.Host, Path: path} + path := fmt.Sprintf("%s/%s/members/%s/scopes", dg.Client.Path, projectId, memberId) + u := url.URL{Scheme: "https", Host: dg.Client.Host, Path: path} req, err := http.NewRequest("GET", u.String(), nil) if err != nil { //Handle Error @@ -108,7 +108,7 @@ func (dg *Client) GetMemberScopes(projectId string, memberId string) (ScopeList, } req.Header = http.Header{ - "Host": []string{dg.Host}, + "Host": []string{dg.Client.Host}, "Content-Type": []string{"application/json"}, "Authorization": []string{"token " + dg.ApiKey}, "User-Agent": []string{dgAgent}, @@ -137,11 +137,11 @@ type MemberScope struct { Scope string `json:"scope"` } -func (dg *Client) UpdateMemberScopes(projectId string, memberId string, scope string) (Message, error) { +func (dg *ManageClient) UpdateMemberScopes(projectId string, memberId string, scope string) (Message, error) { newScope := fmt.Sprintf(`{"scope":"%s"}`, scope) client := new(http.Client) - path := fmt.Sprintf("%s/%s/members/%s/scopes", dg.Path, projectId, memberId) - u := url.URL{Scheme: "https", Host: dg.Host, Path: path} + path := fmt.Sprintf("%s/%s/members/%s/scopes", dg.Client.Path, projectId, memberId) + u := url.URL{Scheme: "https", Host: dg.Client.Host, Path: path} req, err := http.NewRequest("PUT", u.String(), strings.NewReader(newScope)) if err != nil { //Handle Error @@ -149,7 +149,7 @@ func (dg *Client) UpdateMemberScopes(projectId string, memberId string, scope st } req.Header = http.Header{ - "Host": []string{dg.Host}, + "Host": []string{dg.Client.Host}, "Content-Type": []string{"application/json"}, "Authorization": []string{"token " + dg.ApiKey}, "User-Agent": []string{dgAgent}, @@ -175,10 +175,10 @@ func (dg *Client) UpdateMemberScopes(projectId string, memberId string, scope st } } -func (dg *Client) LeaveProject(projectId string) (Message, error) { +func (dg *ManageClient) LeaveProject(projectId string) (Message, error) { client := new(http.Client) - path := fmt.Sprintf("%s/%s/leave", dg.Path, projectId) - u := url.URL{Scheme: "https", Host: dg.Host, Path: path} + path := fmt.Sprintf("%s/%s/leave", dg.Client.Path, projectId) + u := url.URL{Scheme: "https", Host: dg.Client.Host, Path: path} req, err := http.NewRequest("DELETE", u.String(), nil) if err != nil { //Handle Error @@ -186,7 +186,7 @@ func (dg *Client) LeaveProject(projectId string) (Message, error) { } req.Header = http.Header{ - "Host": []string{dg.Host}, + "Host": []string{dg.Client.Host}, "Content-Type": []string{"application/json"}, "Authorization": []string{"token " + dg.ApiKey}, "User-Agent": []string{dgAgent}, diff --git a/deepgram/projects.go b/pkg/api/manage/projects.go similarity index 76% rename from deepgram/projects.go rename to pkg/api/manage/projects.go index b7b0f649..9e7c779b 100644 --- a/deepgram/projects.go +++ b/pkg/api/manage/projects.go @@ -1,4 +1,4 @@ -package deepgram +package manage import ( "bytes" @@ -25,10 +25,10 @@ type ProjectUpdateOptions struct { Company string `json:"company,omitempty"` } -func (dg *Client) ListProjects() (ProjectResponse, error) { +func (dg *ManageClient) ListProjects() (ProjectResponse, error) { client := new(http.Client) - // path := fmt.Sprintf("%s", dg.Path) - u := url.URL{Scheme: "https", Host: dg.Host, Path: dg.Path} + // path := fmt.Sprintf("%s", dg.Client.Path) + u := url.URL{Scheme: "https", Host: dg.Client.Host, Path: dg.Client.Path} req, err := http.NewRequest("GET", u.String(), nil) if err != nil { //Handle Error @@ -36,7 +36,7 @@ func (dg *Client) ListProjects() (ProjectResponse, error) { } req.Header = http.Header{ - "Host": []string{dg.Host}, + "Host": []string{dg.Client.Host}, "Content-Type": []string{"application/json"}, "Authorization": []string{"token " + dg.ApiKey}, "User-Agent": []string{dgAgent}, @@ -61,10 +61,10 @@ func (dg *Client) ListProjects() (ProjectResponse, error) { } } -func (dg *Client) GetProject(projectId string) (Project, error) { +func (dg *ManageClient) GetProject(projectId string) (Project, error) { client := new(http.Client) - path := fmt.Sprintf("%s/%s", dg.Path, projectId) - u := url.URL{Scheme: "https", Host: dg.Host, Path: path} + path := fmt.Sprintf("%s/%s", dg.Client.Path, projectId) + u := url.URL{Scheme: "https", Host: dg.Client.Host, Path: path} req, err := http.NewRequest("GET", u.String(), nil) if err != nil { //Handle Error @@ -72,7 +72,7 @@ func (dg *Client) GetProject(projectId string) (Project, error) { } req.Header = http.Header{ - "Host": []string{dg.Host}, + "Host": []string{dg.Client.Host}, "Content-Type": []string{"application/json"}, "Authorization": []string{"token " + dg.ApiKey}, "User-Agent": []string{dgAgent}, @@ -97,10 +97,10 @@ func (dg *Client) GetProject(projectId string) (Project, error) { } } -func (dg *Client) UpdateProject(projectId string, options ProjectUpdateOptions) (Message, error) { +func (dg *ManageClient) UpdateProject(projectId string, options ProjectUpdateOptions) (Message, error) { client := new(http.Client) - path := fmt.Sprintf("%s/%s", dg.Path, projectId) - u := url.URL{Scheme: "https", Host: dg.Host, Path: path} + path := fmt.Sprintf("%s/%s", dg.Client.Path, projectId) + u := url.URL{Scheme: "https", Host: dg.Client.Host, Path: path} jsonStr, err := json.Marshal(options) if err != nil { log.Panic(err) @@ -113,7 +113,7 @@ func (dg *Client) UpdateProject(projectId string, options ProjectUpdateOptions) } req.Header = http.Header{ - "Host": []string{dg.Host}, + "Host": []string{dg.Client.Host}, "Content-Type": []string{"application/json"}, "Authorization": []string{"token " + dg.ApiKey}, "User-Agent": []string{dgAgent}, @@ -138,10 +138,10 @@ func (dg *Client) UpdateProject(projectId string, options ProjectUpdateOptions) } } -func (dg *Client) DeleteProject(projectId string) (Message, error) { +func (dg *ManageClient) DeleteProject(projectId string) (Message, error) { client := new(http.Client) - path := fmt.Sprintf("%s/%s", dg.Path, projectId) - u := url.URL{Scheme: "https", Host: dg.Host, Path: path} + path := fmt.Sprintf("%s/%s", dg.Client.Path, projectId) + u := url.URL{Scheme: "https", Host: dg.Client.Host, Path: path} req, err := http.NewRequest("DELETE", u.String(), nil) if err != nil { @@ -150,7 +150,7 @@ func (dg *Client) DeleteProject(projectId string) (Message, error) { } req.Header = http.Header{ - "Host": []string{dg.Host}, + "Host": []string{dg.Client.Host}, "Content-Type": []string{"application/json"}, "Authorization": []string{"token " + dg.ApiKey}, "User-Agent": []string{dgAgent}, diff --git a/deepgram/types.go b/pkg/api/manage/types.go similarity index 97% rename from deepgram/types.go rename to pkg/api/manage/types.go index 4fc3db19..5aa1ec4d 100644 --- a/deepgram/types.go +++ b/pkg/api/manage/types.go @@ -1,4 +1,4 @@ -package deepgram +package manage import ( "bytes" diff --git a/deepgram/usage.go b/pkg/api/manage/usage.go similarity index 84% rename from deepgram/usage.go rename to pkg/api/manage/usage.go index 1e7c1591..52a2f8b0 100644 --- a/deepgram/usage.go +++ b/pkg/api/manage/usage.go @@ -1,4 +1,4 @@ -package deepgram +package manage import ( "fmt" @@ -81,11 +81,11 @@ type UsageRequest struct { Callback interface{} `json:"callback" url:"callback,omitempty"` } -func (dg *Client) ListRequests(projectId string, options UsageRequestListOptions) (UsageRequestList, error) { +func (dg *ManageClient) ListRequests(projectId string, options UsageRequestListOptions) (UsageRequestList, error) { query, _ := query.Values(options) client := new(http.Client) - path := fmt.Sprintf("%s/%s/requests", dg.Path, projectId) - u := url.URL{Scheme: "https", Host: dg.Host, Path: path, RawQuery: query.Encode()} + path := fmt.Sprintf("%s/%s/requests", dg.Client.Path, projectId) + u := url.URL{Scheme: "https", Host: dg.Client.Host, Path: path, RawQuery: query.Encode()} req, err := http.NewRequest("GET", u.String(), nil) if err != nil { @@ -94,7 +94,7 @@ func (dg *Client) ListRequests(projectId string, options UsageRequestListOptions } req.Header = http.Header{ - "Host": []string{dg.Host}, + "Host": []string{dg.Client.Host}, "Content-Type": []string{"application/json"}, "Authorization": []string{"token " + dg.ApiKey}, "User-Agent": []string{dgAgent}, @@ -120,10 +120,10 @@ func (dg *Client) ListRequests(projectId string, options UsageRequestListOptions } -func (dg *Client) GetRequest(projectId string, requestId string) (UsageRequest, error) { +func (dg *ManageClient) GetRequest(projectId string, requestId string) (UsageRequest, error) { client := new(http.Client) - path := fmt.Sprintf("%s/%s/requests/%s", dg.Path, projectId, requestId) - u := url.URL{Scheme: "https", Host: dg.Host, Path: path} + path := fmt.Sprintf("%s/%s/requests/%s", dg.Client.Path, projectId, requestId) + u := url.URL{Scheme: "https", Host: dg.Client.Host, Path: path} req, err := http.NewRequest("GET", u.String(), nil) if err != nil { //Handle Error @@ -131,7 +131,7 @@ func (dg *Client) GetRequest(projectId string, requestId string) (UsageRequest, } req.Header = http.Header{ - "Host": []string{dg.Host}, + "Host": []string{dg.Client.Host}, "Content-Type": []string{"application/json"}, "Authorization": []string{"token " + dg.ApiKey}, "User-Agent": []string{dgAgent}, @@ -156,11 +156,11 @@ func (dg *Client) GetRequest(projectId string, requestId string) (UsageRequest, } } -func (dg *Client) GetFields(projectId string, options UsageRequestListOptions) (interface{}, error) { +func (dg *ManageClient) GetFields(projectId string, options UsageRequestListOptions) (interface{}, error) { query, _ := query.Values(options) client := new(http.Client) - path := fmt.Sprintf("%s/%s/usage/fields", dg.Path, projectId) - u := url.URL{Scheme: "https", Host: dg.Host, Path: path, RawQuery: query.Encode()} + path := fmt.Sprintf("%s/%s/usage/fields", dg.Client.Path, projectId) + u := url.URL{Scheme: "https", Host: dg.Client.Host, Path: path, RawQuery: query.Encode()} req, err := http.NewRequest("GET", u.String(), nil) if err != nil { //Handle Error @@ -168,7 +168,7 @@ func (dg *Client) GetFields(projectId string, options UsageRequestListOptions) ( } req.Header = http.Header{ - "Host": []string{dg.Host}, + "Host": []string{dg.Client.Host}, "Content-Type": []string{"application/json"}, "Authorization": []string{"token " + dg.ApiKey}, "User-Agent": []string{dgAgent}, @@ -193,11 +193,11 @@ func (dg *Client) GetFields(projectId string, options UsageRequestListOptions) ( } } -func (dg *Client) GetUsage(projectId string, options UsageOptions) (UsageSummary, error) { +func (dg *ManageClient) GetUsage(projectId string, options UsageOptions) (UsageSummary, error) { query, _ := query.Values(options) client := new(http.Client) - path := fmt.Sprintf("%s/%s/usage", dg.Path, projectId) - u := url.URL{Scheme: "https", Host: dg.Host, Path: path, RawQuery: query.Encode()} + path := fmt.Sprintf("%s/%s/usage", dg.Client.Path, projectId) + u := url.URL{Scheme: "https", Host: dg.Client.Host, Path: path, RawQuery: query.Encode()} req, err := http.NewRequest("GET", u.String(), nil) if err != nil { //Handle Error @@ -205,7 +205,7 @@ func (dg *Client) GetUsage(projectId string, options UsageOptions) (UsageSummary } req.Header = http.Header{ - "Host": []string{dg.Host}, + "Host": []string{dg.Client.Host}, "Content-Type": []string{"application/json"}, "Authorization": []string{"token " + dg.ApiKey}, "User-Agent": []string{dgAgent}, diff --git a/deepgram/prerecorded.go b/pkg/api/prerecorded/prerecorded.go similarity index 88% rename from deepgram/prerecorded.go rename to pkg/api/prerecorded/prerecorded.go index 017ab573..5b41f5d4 100644 --- a/deepgram/prerecorded.go +++ b/pkg/api/prerecorded/prerecorded.go @@ -1,4 +1,4 @@ -package deepgram +package prerecorded import ( "bytes" @@ -9,11 +9,33 @@ import ( "log" "net/http" "net/url" + "runtime" "strings" "github.com/google/go-querystring/query" + + client "github.com/deepgram-devs/deepgram-go-sdk/pkg/client/prerecorded" ) +var sdkVersion string = "0.10.0" +var dgAgent string = "@deepgram/sdk/" + sdkVersion + " go/" + goVersion() + +func goVersion() string { + version := runtime.Version() + if strings.HasPrefix(version, "go") { + return version[2:] + } + return version +} + +type PrerecordedClient struct { + *client.Client +} + +func New(client *client.Client) *PrerecordedClient { + return &PrerecordedClient{client} +} + type PreRecordedTranscriptionOptions struct { Alternatives int `json:"alternatives" url:"alternatives,omitempty" ` AnalyzeSentiment bool `json:"analyze_sentiment" url:"analyze_sentiment,omitempty" ` @@ -187,10 +209,10 @@ type SummaryV2 struct { Result string `json:"result"` } -func (dg *Client) PreRecordedFromStream(source ReadStreamSource, options PreRecordedTranscriptionOptions) (*PreRecordedResponse, error) { +func (dg *PrerecordedClient) PreRecordedFromStream(source ReadStreamSource, options PreRecordedTranscriptionOptions) (*PreRecordedResponse, error) { client := &http.Client{} query, _ := query.Values(options) - u := url.URL{Scheme: "https", Host: dg.Host, Path: dg.TranscriptionPath, RawQuery: query.Encode()} + u := url.URL{Scheme: "https", Host: dg.Client.Host, Path: dg.Client.TranscriptionPath, RawQuery: query.Encode()} // TODO: accept file path as string build io.Reader here req, err := http.NewRequest("POST", u.String(), source.Stream) @@ -200,9 +222,9 @@ func (dg *Client) PreRecordedFromStream(source ReadStreamSource, options PreReco } req.Header = http.Header{ - "Host": []string{dg.Host}, + "Host": []string{dg.Client.Host}, "Content-Type": []string{source.Mimetype}, - "Authorization": []string{"token " + dg.ApiKey}, + "Authorization": []string{"token " + dg.Client.ApiKey}, "User-Agent": []string{dgAgent}, } @@ -226,10 +248,10 @@ func (dg *Client) PreRecordedFromStream(source ReadStreamSource, options PreReco return &result, nil } -func (dg *Client) PreRecordedFromURL(source UrlSource, options PreRecordedTranscriptionOptions) (PreRecordedResponse, error) { +func (dg *PrerecordedClient) PreRecordedFromURL(source UrlSource, options PreRecordedTranscriptionOptions) (PreRecordedResponse, error) { client := new(http.Client) query, _ := query.Values(options) - u := url.URL{Scheme: "https", Host: dg.Host, Path: dg.TranscriptionPath, RawQuery: query.Encode()} + u := url.URL{Scheme: "https", Host: dg.Client.Host, Path: dg.Client.TranscriptionPath, RawQuery: query.Encode()} jsonStr, err := json.Marshal(source) if err != nil { log.Panic(err) @@ -243,9 +265,9 @@ func (dg *Client) PreRecordedFromURL(source UrlSource, options PreRecordedTransc } req.Header = http.Header{ - "Host": []string{dg.Host}, + "Host": []string{dg.Client.Host}, "Content-Type": []string{"application/json"}, - "Authorization": []string{"token " + dg.ApiKey}, + "Authorization": []string{"token " + dg.Client.ApiKey}, "User-Agent": []string{dgAgent}, } @@ -311,3 +333,9 @@ func SecondsToTimestamp(seconds float64) string { seconds = seconds - float64(hours*3600) - float64(minutes*60) return fmt.Sprintf("%02d:%02d:%02.3f", hours, minutes, seconds) } + +func GetJson(resp *http.Response, target interface{}) error { + defer resp.Body.Close() + + return json.NewDecoder(resp.Body).Decode(target) +} diff --git a/pkg/api/prerecorded/types.go b/pkg/api/prerecorded/types.go new file mode 100644 index 00000000..efa1de26 --- /dev/null +++ b/pkg/api/prerecorded/types.go @@ -0,0 +1,37 @@ +package prerecorded + +import ( + "bytes" + "io" +) + +type InvitationOptions struct { + Email string `json:"email"` + Scope string `json:"scope"` +} + +type InvitationList struct { + Invites []InvitationOptions `json:"invites"` +} + +type Message struct { + Message string `json:"message"` +} + +type TranscriptionSource interface { + ReadStreamSource | UrlSource | BufferSource +} + +type ReadStreamSource struct { + Stream io.Reader `json:"stream"` + Mimetype string `json:"mimetype"` +} + +type UrlSource struct { + Url string `json:"url"` +} + +type BufferSource struct { + Buffer bytes.Buffer `json:"buffer"` + Mimetype string `json:"mimetype"` +} diff --git a/deepgram/transcriptions.go b/pkg/client/live/client.go similarity index 82% rename from deepgram/transcriptions.go rename to pkg/client/live/client.go index c9c8725f..d20c5fed 100644 --- a/deepgram/transcriptions.go +++ b/pkg/client/live/client.go @@ -1,15 +1,18 @@ -package deepgram +package live import ( "log" "net/http" "net/url" + "runtime" + "strings" + "github.com/dvonthenen/websocket" "github.com/google/go-querystring/query" - "github.com/gorilla/websocket" ) type LiveTranscriptionOptions struct { + Host string Alternatives int `json:"alternatives" url:"alternatives,omitempty" ` Callback string `json:"callback" url:"callback,omitempty" ` Channels int `json:"channels" url:"channels,omitempty" ` @@ -44,14 +47,29 @@ type LiveTranscriptionOptions struct { FillerWords string `json:"filler_words" url:"filler_words,omitempty" ` } -func (dg *Client) LiveTranscription(options LiveTranscriptionOptions) (*websocket.Conn, *http.Response, error) { +var sdkVersion string = "0.10.0" +var dgAgent string = "@deepgram/sdk/" + sdkVersion + " go/" + goVersion() + +func goVersion() string { + version := runtime.Version() + if strings.HasPrefix(version, "go") { + return version[2:] + } + return version +} + +func New(apiKey string, options LiveTranscriptionOptions) (*websocket.Conn, *http.Response, error) { + if options.Host == "" { + options.Host = "api.deepgram.com" + } + query, _ := query.Values(options) - u := url.URL{Scheme: "wss", Host: dg.Host, Path: "/v1/listen", RawQuery: query.Encode()} + u := url.URL{Scheme: "wss", Host: options.Host, Path: "/v1/listen", RawQuery: query.Encode()} log.Printf("connecting to %s", u.String()) header := http.Header{ - "Host": []string{dg.Host}, - "Authorization": []string{"token " + dg.ApiKey}, + "Host": []string{options.Host}, + "Authorization": []string{"token " + apiKey}, "User-Agent": []string{dgAgent}, } diff --git a/deepgram/deepgram.go b/pkg/client/prerecorded/client.go similarity index 51% rename from deepgram/deepgram.go rename to pkg/client/prerecorded/client.go index 8c51be59..fa26fb09 100644 --- a/deepgram/deepgram.go +++ b/pkg/client/prerecorded/client.go @@ -1,15 +1,4 @@ -package deepgram - -import ( - "encoding/json" - "net/http" - "runtime" - "strings" -) - -var sdkVersion string = "0.10.0" - -var dgAgent string = "@deepgram/sdk/" + sdkVersion + " go/" + goVersion() +package prerecorded type Client struct { ApiKey string @@ -18,7 +7,7 @@ type Client struct { TranscriptionPath string } -func NewClient(apiKey string) *Client { +func New(apiKey string) *Client { return &Client{ ApiKey: apiKey, Host: "api.deepgram.com", @@ -41,17 +30,3 @@ func (c *Client) WithTranscriptionPath(path string) *Client { c.TranscriptionPath = path return c } - -func GetJson(resp *http.Response, target interface{}) error { - defer resp.Body.Close() - - return json.NewDecoder(resp.Body).Decode(target) -} - -func goVersion() string { - version := runtime.Version() - if strings.HasPrefix(version, "go") { - return version[2:] - } - return version -} diff --git a/tests/prerecorded_test.go b/tests/prerecorded_test.go index e405c3c2..81295b5a 100644 --- a/tests/prerecorded_test.go +++ b/tests/prerecorded_test.go @@ -6,8 +6,10 @@ import ( "net/http" "testing" - "github.com/deepgram-devs/deepgram-go-sdk/deepgram" "github.com/jarcoal/httpmock" + + api "github.com/deepgram-devs/deepgram-go-sdk/pkg/api/prerecorded" + client "github.com/deepgram-devs/deepgram-go-sdk/pkg/client/prerecorded" ) func TestPrerecordedFromURL(t *testing.T) { @@ -116,10 +118,11 @@ func TestPrerecordedFromURL(t *testing.T) { httpmock.RegisterResponder("POST", betaEndPoint, preRecordedFromURLHandler) t.Run("Test Basic PreRecordedFromURL", func(t *testing.T) { - dg := deepgram.NewClient(MockAPIKey) - _, err := dg.PreRecordedFromURL( - deepgram.UrlSource{Url: MockAudioURL}, - deepgram.PreRecordedTranscriptionOptions{}) + dg := client.New(MockAPIKey) + prClient := api.New(dg) + _, err := prClient.PreRecordedFromURL( + api.UrlSource{Url: MockAudioURL}, + api.PreRecordedTranscriptionOptions{}) if err != nil { t.Errorf("should succeed, but got %s", err) @@ -127,10 +130,11 @@ func TestPrerecordedFromURL(t *testing.T) { }) t.Run("Test PreRecordedFromURL with summarize v1", func(t *testing.T) { - dg := deepgram.NewClient(MockAPIKey) - _, err := dg.PreRecordedFromURL( - deepgram.UrlSource{Url: MockAudioURL}, - deepgram.PreRecordedTranscriptionOptions{ + dg := client.New(MockAPIKey) + prClient := api.New(dg) + _, err := prClient.PreRecordedFromURL( + api.UrlSource{Url: MockAudioURL}, + api.PreRecordedTranscriptionOptions{ Summarize: true, }) @@ -140,10 +144,11 @@ func TestPrerecordedFromURL(t *testing.T) { }) t.Run("Test PreRecordedFromURL with summarize v2", func(t *testing.T) { - dg := deepgram.NewClient(MockAPIKey).WithHost(betaHost) - _, err := dg.PreRecordedFromURL( - deepgram.UrlSource{Url: MockAudioURL}, - deepgram.PreRecordedTranscriptionOptions{ + dg := client.New(MockAPIKey).WithHost(betaHost) + prClient := api.New(dg) + _, err := prClient.PreRecordedFromURL( + api.UrlSource{Url: MockAudioURL}, + api.PreRecordedTranscriptionOptions{ Summarize: "v2", })