Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encapsulate the Manage Object to Hide Internal Details #107

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions examples/balances/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2023 Deepgram SDK contributors. All Rights Reserved.
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
// SPDX-License-Identifier: MIT

package main

import (
"context"
"log"
"os"

api "github.com/deepgram-devs/deepgram-go-sdk/pkg/api/manage/v1"
client "github.com/deepgram-devs/deepgram-go-sdk/pkg/client/prerecorded"
)

func main() {
// context
ctx := context.Background()

//client
dg := client.NewWithDefaults()
mgClient := api.New(dg)

// list projects
respProject, err := mgClient.ListProjects(ctx)
if err != nil {
log.Printf("ListProjects failed. Err: %v\n", err)
os.Exit(1)
}

var projectId string
for _, item := range respProject.Projects {
projectId = item.ProjectId
name := item.Name
log.Printf("ListProjects() - Name: %s, ID: %s\n", name, projectId)
break
}

// list balances
respList, err := mgClient.ListBalances(ctx, projectId)
if err != nil {
log.Printf("ListBalances failed. Err: %v\n", err)
os.Exit(1)
}

var amount float64
var id string
for _, item := range respList.Balances {
id = item.BalanceId
amount = item.Amount
log.Printf("ListBalances() - ID: %s, Amount: %f\n", id, amount)
}

// get first balance
respGet, err := mgClient.GetBalance(ctx, projectId, id)
if err != nil {
log.Printf("GetBalance failed. Err: %v\n", err)
os.Exit(1)
}
log.Printf("GetBalance() - ID: %s, Amount: %f\n", id, respGet.Amount)
}
106 changes: 106 additions & 0 deletions examples/invitations/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Copyright 2023 Deepgram SDK contributors. All Rights Reserved.
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
// SPDX-License-Identifier: MIT

package main

import (
"context"
"log"
"os"

api "github.com/deepgram-devs/deepgram-go-sdk/pkg/api/manage/v1"
interfaces "github.com/deepgram-devs/deepgram-go-sdk/pkg/api/manage/v1/interfaces"
client "github.com/deepgram-devs/deepgram-go-sdk/pkg/client/prerecorded"
)

func main() {
// context
ctx := context.Background()

//client
dg := client.NewWithDefaults()
mgClient := api.New(dg)

// list projects
respList, err := mgClient.ListProjects(ctx)
if err != nil {
log.Printf("ListProjects failed. Err: %v\n", err)
os.Exit(1)
}

var projectId string
for _, item := range respList.Projects {
projectId = item.ProjectId
name := item.Name
log.Printf("ListProjects() - Name: %s, ID: %s\n", name, projectId)
break
}

// list invitations
respGet, err := mgClient.ListInvitations(ctx, projectId)
if err != nil {
log.Printf("ListInvitations failed. Err: %v\n", err)
os.Exit(1)
}

for _, item := range respGet.Invites {
id := item.Email
scope := item.Scope
log.Printf("ListInvitations() - ID: %s, Scope: %s\n", id, scope)
}

// send invite
respMessage, err := mgClient.SendInvitation(ctx, projectId, &interfaces.InvitationRequest{
Email: "[email protected]",
Scope: "member",
})
if err != nil {
log.Printf("SendInvitation failed. Err: %v\n", err)
os.Exit(1)
}
log.Printf("SendInvitation() - Name: %s\n", respMessage.Message)

// list invitations
respGet, err = mgClient.ListInvitations(ctx, projectId)
if err != nil {
log.Printf("ListInvitations failed. Err: %v\n", err)
os.Exit(1)
}

for _, item := range respGet.Invites {
id := item.Email
scope := item.Scope
log.Printf("ListInvitations() - ID: %s, Scope: %s\n", id, scope)
}

// delete project
respMessage, err = mgClient.DeleteInvitation(ctx, projectId, "[email protected]")
if err != nil {
log.Printf("DeleteInvitation failed. Err: %v\n", err)
os.Exit(1)
}
log.Printf("DeleteInvitation() - Name: %s\n", respMessage.Message)

// list invitations
respGet, err = mgClient.ListInvitations(ctx, projectId)
if err != nil {
log.Printf("ListInvitations failed. Err: %v\n", err)
os.Exit(1)
}

for _, item := range respGet.Invites {
id := item.Email
scope := item.Scope
log.Printf("ListInvitations() - ID: %s, Scope: %s\n", id, scope)
}

// TODO: There isnt an API call to add a member to a project. So will leave this commented out as an example
// Leave Project
// respMessage, err = mgClient.LeaveProject(ctx, projectId)
// if err != nil {
// log.Printf("LeaveProject failed. Err: %v\n", err)
// os.Exit(1)
// }
// log.Printf("LeaveProject() - Name: %s\n", respMessage.Message)
}
93 changes: 93 additions & 0 deletions examples/keys/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright 2023 Deepgram SDK contributors. All Rights Reserved.
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
// SPDX-License-Identifier: MIT

package main

import (
"context"
"log"
"os"

api "github.com/deepgram-devs/deepgram-go-sdk/pkg/api/manage/v1"
interfaces "github.com/deepgram-devs/deepgram-go-sdk/pkg/api/manage/v1/interfaces"
client "github.com/deepgram-devs/deepgram-go-sdk/pkg/client/prerecorded"
)

func main() {
// context
ctx := context.Background()

//client
dg := client.NewWithDefaults()
mgClient := api.New(dg)

// list projects
respList, err := mgClient.ListProjects(ctx)
if err != nil {
log.Printf("ListProjects failed. Err: %v\n", err)
os.Exit(1)
}

var projectId string
for _, item := range respList.Projects {
projectId = item.ProjectId
name := item.Name
log.Printf("ListProjects() - Name: %s, ID: %s\n", name, projectId)
break
}

// list keys
respGet, err := mgClient.ListKeys(ctx, projectId)
if err != nil {
log.Printf("ListKeys failed. Err: %v\n", err)
os.Exit(1)
}

for _, item := range respGet.ApiKeys {
id := item.ApiKeyId
comment := item.Comment
log.Printf("ListKeys() - ID: %s, Comment: %s\n", id, comment)
break
}

// create key
respCreate, err := mgClient.CreateKey(ctx, projectId, &interfaces.KeyCreateRequest{
Comment: "My Test",
Scopes: []string{"onprem:products"},
})
if err != nil {
log.Printf("CreateKey failed. Err: %v\n", err)
os.Exit(1)
}
log.Printf("CreateKey() - Name: %s\n", respCreate.Comment)

// get key
respKey, err := mgClient.GetKey(ctx, projectId, respCreate.ApiKeyId)
if err != nil {
log.Printf("GetKey failed. Err: %v\n", err)
os.Exit(1)
}
log.Printf("GetKey() - ID: %s, Comment: %s\n", respKey.Key.ApiKeyId, respKey.Key.Comment)

// delete project
respMessage, err := mgClient.DeleteKey(ctx, projectId, respKey.Key.ApiKeyId)
if err != nil {
log.Printf("DeleteKey failed. Err: %v\n", err)
os.Exit(1)
}
log.Printf("DeleteKey() - Name: %s\n", respMessage.Message)

// list invitations
respGet, err = mgClient.ListKeys(ctx, projectId)
if err != nil {
log.Printf("ListKeys failed. Err: %v\n", err)
os.Exit(1)
}

for _, item := range respGet.ApiKeys {
id := item.ApiKeyId
comment := item.Comment
log.Printf("ListKeys() - ID: %s, Comment: %s\n", id, comment)
}
}
91 changes: 91 additions & 0 deletions examples/members/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright 2023 Deepgram SDK contributors. All Rights Reserved.
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
// SPDX-License-Identifier: MIT

package main

import (
"bufio"
"context"
"fmt"
"log"
"os"
"strings"

api "github.com/deepgram-devs/deepgram-go-sdk/pkg/api/manage/v1"
client "github.com/deepgram-devs/deepgram-go-sdk/pkg/client/prerecorded"
)

func main() {
fmt.Print("This example requires a member who already exists where \"@spam.com\" is in the email.\n")
fmt.Printf("If that use already exists, press ENTER to continue!\n")
fmt.Printf("Otherwise, Control+C to EXIT!\n\n")
input := bufio.NewScanner(os.Stdin)
input.Scan()

// context
ctx := context.Background()

//client
dg := client.NewWithDefaults()
mgClient := api.New(dg)

// list projects
respList, err := mgClient.ListProjects(ctx)
if err != nil {
log.Printf("ListProjects failed. Err: %v\n", err)
os.Exit(1)
}

var projectId string
for _, item := range respList.Projects {
projectId = item.ProjectId
name := item.Name
log.Printf("ListProjects() - Name: %s, ID: %s\n", name, projectId)
break
}

// list invitations
respGet, err := mgClient.ListMembers(ctx, projectId)
if err != nil {
log.Printf("ListMembers failed. Err: %v\n", err)
os.Exit(1)
}

var delMemberId string
for _, item := range respGet.Members {
memberId := item.MemberId
email := item.Email
log.Printf("ListMembers() - ID: %s, Scope: %s\n", memberId, email)

if strings.Contains(email, "@spam.com") {
delMemberId = memberId
}
}

if delMemberId == "" {
fmt.Printf("Unable to find member with email containing \"@spam.com\". Exiting!\n")
os.Exit(0)
}

// remove member
respMessage, err := mgClient.RemoveMember(ctx, projectId, delMemberId)
if err != nil {
log.Printf("RemoveMember failed. Err: %v\n", err)
os.Exit(1)
}
log.Printf("RemoveMember() - Name: %s\n", respMessage.Message)

// list invitations
respGet, err = mgClient.ListMembers(ctx, projectId)
if err != nil {
log.Printf("ListMembers failed. Err: %v\n", err)
os.Exit(1)
}

for _, item := range respGet.Members {
memberId := item.MemberId
email := item.Email
log.Printf("ListMembers() - ID: %s, Scope: %s\n", memberId, email)
}
}
13 changes: 3 additions & 10 deletions examples/prerecorded/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,18 @@ import (
)

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)
}

// context
ctx := context.Background()

c := client.New(deepgramApiKey)
//client
c := client.NewWithDefaults()
dg := prerecorded.New(c)

filePath := "https://static.deepgram.com/examples/Bueller-Life-moves-pretty-fast.wav"
var res interface{}
var err error

// send stream
if isURL(filePath) {
res, err = dg.FromURL(
ctx,
Expand Down
Loading