From 67e4993f1aafac210c2a19b6174e6bdd14969d21 Mon Sep 17 00:00:00 2001 From: Florian Vogt Date: Tue, 11 Feb 2025 15:34:08 +0100 Subject: [PATCH] Fix: Voice response cannot be decoded --- seven/voice.go | 8 ++++--- seven/voice_test.go | 52 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/seven/voice.go b/seven/voice.go index 8de21e0..4fadb6f 100644 --- a/seven/voice.go +++ b/seven/voice.go @@ -3,10 +3,11 @@ package seven import ( "context" "encoding/json" + "fmt" ) type VoiceHangupParams struct { - CallIdentifier string + CallIdentifier int64 } type VoiceHangup struct { @@ -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"` Price float64 `json:"price"` Recipient string `json:"recipient"` Sender string `json:"sender"` @@ -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 diff --git a/seven/voice_test.go b/seven/voice_test.go index dd5572a..2dfc091 100644 --- a/seven/voice_test.go +++ b/seven/voice_test.go @@ -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) { @@ -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) + } + +}