Skip to content

Fix: Voice response cannot be decoded #8

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

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
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
8 changes: 5 additions & 3 deletions seven/voice.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package seven
import (
"context"
"encoding/json"
"fmt"
)

type VoiceHangupParams struct {
CallIdentifier string
CallIdentifier int64
}

type VoiceHangup struct {
Expand All @@ -25,7 +26,7 @@ type Voice struct {
type VoiceMessage struct {
Error *string `json:"error"`
ErrorText *string `json:"error_text"`
Id *string `json:"id"`
Id *int64 `json:"id"`
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was unsure how large the value could be. If it is a database ID, I can imagine that it will be quite high, which is why I chose int64.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vorlif it is supposed to become quite high indeed, so int64 seems good to me :-)

Price float64 `json:"price"`
Recipient string `json:"recipient"`
Sender string `json:"sender"`
Expand Down Expand Up @@ -67,7 +68,8 @@ func (api *VoiceResource) Hangup(p VoiceHangupParams) (o *VoiceHangup, e error)
}

func (api *VoiceResource) HangupContext(ctx context.Context, p VoiceHangupParams) (*VoiceHangup, error) {
res, err := api.client.request(ctx, "voice/"+p.CallIdentifier+"/hangup", "POST", nil)
endpoint := fmt.Sprintf("voice/%d/hangup", p.CallIdentifier)
res, err := api.client.request(ctx, endpoint, "POST", nil)

if err != nil {
return nil, err
Expand Down
52 changes: 51 additions & 1 deletion seven/voice_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package seven

import (
a "github.com/stretchr/testify/assert"
"encoding/json"
"testing"

a "github.com/stretchr/testify/assert"
)

func TestVoiceResource_Dispatch(t *testing.T) {
Expand Down Expand Up @@ -30,3 +32,51 @@ func TestVoiceResource_Dispatch(t *testing.T) {
a.Nil(t, v)
}
}

func TestVoice_UnmarshalJSON(t *testing.T) {
// The following example is taken from the API documentation and should be able to be transformed without errors.
// See: https://docs.seven.io/de/rest-api/endpunkte/voice
apiExample := []byte(`
{
"success": "100",
"total_price": 0.045,
"balance": 3509.236,
"debug": false,
"messages": [
{
"id": 1384013,
"sender": "sender",
"recipient": "49176123456789",
"text": "Hallo Welt!",
"price": 0.045,
"success": true,
"error": null,
"error_text": null
}
]
}`)

js := &Voice{}
err := json.Unmarshal(apiExample, js)
if a.NoError(t, err) {
a.Len(t, js.Messages, 1)
}
}

func TestVoiceHangup_UnmarshalJSON(t *testing.T) {
// The following example is taken from the API documentation and should be able to be transformed without errors.
// See: https://docs.seven.io/de/rest-api/endpunkte/voice
apiExample := []byte(`
{
"success": true,
"error": null
}`)

js := &VoiceHangup{}
err := json.Unmarshal(apiExample, js)
if a.NoError(t, err) {
a.True(t, js.Success)
a.Nil(t, js.Error)
}

}