Skip to content

Commit

Permalink
review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
narayana-plivo committed Aug 25, 2021
1 parent 0d568cb commit 32ff890
Show file tree
Hide file tree
Showing 24 changed files with 451 additions and 391 deletions.
29 changes: 9 additions & 20 deletions baseclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (client *BaseClient) NewRequest(method string, params interface{}, baseRequ
isCallInsightsRequest, requestPath = checkAndFetchCallInsightsRequestDetails(param)
}
if param == nil || param == "" {
err = errors.New(fmt.Sprintf("Request path parameter #%d is nil/empty but should not be so.", i))
err = fmt.Errorf("Request path parameter #%d is nil/empty but should not be so.", i)
return
}
}
Expand Down Expand Up @@ -99,11 +99,10 @@ func (client *BaseClient) ExecuteRequest(request *http.Request, body interface{}
if extra != nil {
if _, ok := extra[0]["is_voice_request"]; ok {
isVoiceRequest = true
if extra[0]["retry"] == 0 {
request.URL.Host = voiceBaseUrlString
request.Host = voiceBaseUrlString
request.URL.Scheme = HttpsScheme
} else if extra[0]["retry"] == 1 {
request.URL.Host = voiceBaseUrlString
request.Host = voiceBaseUrlString
request.URL.Scheme = HttpsScheme
if extra[0]["retry"] == 1 {
request.URL.Host = voiceBaseUrlStringFallback1
request.Host = voiceBaseUrlStringFallback2
request.URL.Scheme = HttpsScheme
Expand All @@ -122,15 +121,6 @@ func (client *BaseClient) ExecuteRequest(request *http.Request, body interface{}
}
}
}

if client == nil {
return errors.New("client cannot be nil")
}

if client.httpClient == nil {
return errors.New("httpClient cannot be nil")
}

bodyCopy, _ := ioutil.ReadAll(request.Body)
request.Body = ioutil.NopCloser(bytes.NewReader(bodyCopy))
response, err := client.httpClient.Do(request)
Expand Down Expand Up @@ -158,13 +148,12 @@ func (client *BaseClient) ExecuteRequest(request *http.Request, body interface{}
if body != nil {
err = json.Unmarshal(data, body)
}
} else if string(data) == "{}" && response.StatusCode == 404 {
err = errors.New("Resource not found exception \n" + response.Status)
} else {
if string(data) == "{}" && response.StatusCode == 404 {
err = errors.New("Resource not found exception \n" + response.Status)
} else {
err = errors.New(string(data))
}
err = errors.New(string(data))
}

}

return
Expand Down
2 changes: 1 addition & 1 deletion call_feedback.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type CallFeedbackParams struct {
CallUUID string `json:"call_uuid"`
Notes string `json:"notes"`
Rating interface{} `json:"rating"`
Issues []string `json:issues`
Issues []string `json:"issues"`
}

type CallFeedbackCreateResponse struct {
Expand Down
3 changes: 3 additions & 0 deletions compliance_applications.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ type ListComplianceApplicationResponse struct {

func (service *ComplianceApplicationService) Get(complianceApplicationId string) (response *ComplianceApplicationResponse, err error) {
req, err := service.client.NewRequest("GET", nil, "ComplianceApplication/%s", complianceApplicationId)
if err != nil {
return
}
response = &ComplianceApplicationResponse{}
err = service.client.ExecuteRequest(req, response)
return
Expand Down
3 changes: 3 additions & 0 deletions compliance_document_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ type ListComplianceDocumentTypeResponse struct {

func (service *ComplianceDocumentTypeService) Get(docId string) (response *GetComplianceDocumentTypeResponse, err error) {
req, err := service.client.NewRequest("GET", nil, "ComplianceDocumentType/%s", docId)
if err != nil {
return
}
response = &GetComplianceDocumentTypeResponse{}
err = service.client.ExecuteRequest(req, response)
return
Expand Down
9 changes: 7 additions & 2 deletions compliance_documents.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,17 @@ type CreateComplianceDocumentParams struct {
}

type UpdateComplianceDocumentParams struct {
ComplianceDocumentID string `json:compliance_document_id`
ComplianceDocumentID string `json:"compliance_document_id"`
CreateComplianceDocumentParams
}

type UpdateComplianceDocumentResponse BaseResponse

func (service *ComplianceDocumentService) Get(complianceDocumentId string) (response *GetComplianceDocumentResponse, err error) {
req, err := service.client.NewRequest("GET", nil, "ComplianceDocument/%s", complianceDocumentId)
if err != nil {
return
}
response = &GetComplianceDocumentResponse{}
err = service.client.ExecuteRequest(req, response)
return
Expand Down Expand Up @@ -182,7 +185,9 @@ func newfileUploadRequest(uri string, params map[string]string, paramName, path
if err != nil {
return nil, err
}
part.Write(fileContents)
if _, err := part.Write(fileContents); err != nil {
return nil, err
}
}

for key, val := range params {
Expand Down
3 changes: 3 additions & 0 deletions compliance_requirements.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ type ListComplianceRequirementParams struct {

func (service *ComplianceRequirementService) Get(complianceRequirementId string) (response *GetComplianceRequirementResponse, err error) {
req, err := service.client.NewRequest("GET", nil, "ComplianceRequirement/%s", complianceRequirementId)
if err != nil {
return nil, err
}
response = &GetComplianceRequirementResponse{}
err = service.client.ExecuteRequest(req, response)
return
Expand Down
3 changes: 3 additions & 0 deletions end_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ type UpdateEndUserResponse BaseResponse

func (service *EndUserService) Get(endUserId string) (response *EndUserGetResponse, err error) {
req, err := service.client.NewRequest("GET", nil, "EndUser/%s", endUserId)
if err != nil {
return nil, err
}
response = &EndUserGetResponse{}
err = service.client.ExecuteRequest(req, response)
return
Expand Down
3 changes: 2 additions & 1 deletion examples/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package main
import (
"fmt"
"os"
"plivo/plivo-go"

"github.com/plivo/plivo-go"
)

var client *plivo.Client
Expand Down
3 changes: 2 additions & 1 deletion fixtures/numberpoolSingleNoResponse.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"country_iso2": "CA",
"number": "15799140348",
"numberpool_uuid": "659c7f88-c819-46e2-8af4-2d8a84249099",
"type": "fixed"
"type": "fixed",
"service": "mms"
}
2 changes: 1 addition & 1 deletion fixtures/powerpackList.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"next": null,
"offset": 0,
"previous": null,
"total_count": 0
"total_count": 2
},
"objects": [
{
Expand Down
6 changes: 0 additions & 6 deletions jwt/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,13 @@ func (acctkn *AccessToken) New(authId string, authToken string, username string,

if validFrom.IsZero() {
acctkn.Lifetime = lifetime
if lifetime == 0 {
acctkn.Lifetime = 86400 * time.Second
}
if validTill.IsZero() {
acctkn.ValidFrom = time.Now()
} else {
acctkn.ValidFrom = validTill.Add(-acctkn.Lifetime)
}
} else if validTill.IsZero() {
acctkn.Lifetime = lifetime
if lifetime == 0 {
acctkn.Lifetime = 86400 * time.Second
}
acctkn.ValidFrom = validFrom

} else {
Expand Down
30 changes: 22 additions & 8 deletions media.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ import (
"strings"
)

// MediaService struct hold the client and Media detail
type MediaService struct {
client *Client
Media
}

// Contains the infomation about media
type Media struct {
ContentType string `json:"content_type,omitempty" url:"content_type,omitempty"`
FileName string `json:"file_name,omitempty" url:"file_name,omitempty"`
Expand All @@ -25,6 +28,7 @@ type Media struct {
URL string `json:"url,omitempty" url:"url,omitempty"`
}

//Media related information
type MediaUploadResponse struct {
ContentType string `json:"content_type,omitempty" url:"content_type,omitempty"`
FileName string `json:"file_name,omitempty" url:"file_name,omitempty"`
Expand All @@ -38,32 +42,40 @@ type MediaUploadResponse struct {
ErrorCode int `json:"error_code,omitempty" url:"error_code,omitempty"`
}

//Meta data information
type MediaMeta struct {
Previous *string
Next *string
TotalCount int `json:"total_count" url:"api_id"`
Offset int `json:"offset,omitempty" url:"offset,omitempty"`
Limit int `json:"limit,omitempty" url:"limit,omitempty"`
}

//Media upload response to client
type MediaResponseBody struct {
Media []MediaUploadResponse `json:"objects" url:"objects"`
ApiID string `json:"api_id" url:"api_id"`
}

// List of media information
type BaseListMediaResponse struct {
ApiID string `json:"api_id" url:"api_id"`
Meta MediaMeta `json:"meta" url:"meta"`
Media []Media `json:"objects" url:"objects"`
}

//Input param to upload media
type MediaUpload struct {
UploadFiles []Files
}

//Information about files
type Files struct {
FilePath string
ContentType string
}

//Media list metadata
type MediaListParams struct {
Limit int `url:"limit,omitempty"`
Offset int `url:"offset,omitempty"`
Expand All @@ -74,11 +86,16 @@ var quoteEscaper = strings.NewReplacer("\\", "\\\\", `"`, "\\\"")
func escapeQuotes(s string) string {
return quoteEscaper.Replace(s)
}

//Upload the media to plivo api, use media id for sending MMS
func (service *MediaService) Upload(params MediaUpload) (response *MediaResponseBody, err error) {
payload := &bytes.Buffer{}
writer := multipart.NewWriter(payload)
for i := 0; i < len(params.UploadFiles); i++ {
file, errFile1 := os.Open(params.UploadFiles[i].FilePath)
if errFile1 != nil {
return nil, errFile1
}
defer file.Close()
filename := filepath.Base(params.UploadFiles[i].FilePath)
h := make(textproto.MIMEHeader)
Expand All @@ -87,6 +104,9 @@ func (service *MediaService) Upload(params MediaUpload) (response *MediaResponse
escapeQuotes("file"), escapeQuotes(filename)))
h.Set("Content-Type", params.UploadFiles[i].ContentType)
part1, errFile1 := writer.CreatePart(h)
if errFile1 != nil {
return nil, errFile1
}
_, errFile1 = io.Copy(part1, file)
if errFile1 != nil {
return nil, errFile1
Expand All @@ -109,14 +129,7 @@ func (service *MediaService) Upload(params MediaUpload) (response *MediaResponse
return
}

func mustOpen(f string) *os.File {
r, err := os.Open(f)
if err != nil {
panic(err)
}
return r
}

// Get the single media information from media ID
func (service *MediaService) Get(media_id string) (response *Media, err error) {
req, err := service.client.NewRequest("GET", nil, "Media/%s", media_id)
if err != nil {
Expand All @@ -131,6 +144,7 @@ func (service *MediaService) Get(media_id string) (response *Media, err error) {
return resp, nil
}

//List all the media information
func (service *MediaService) List(param MediaListParams) (response *BaseListMediaResponse, err error) {
req, err := service.client.NewRequest("GET", param, "Media")
if err != nil {
Expand Down
42 changes: 19 additions & 23 deletions media_test.go
Original file line number Diff line number Diff line change
@@ -1,48 +1,44 @@
package plivo

import (
"errors"
"testing"

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

func TestMedia_List(t *testing.T) {
expectResponse("MediaListResponse.json", 200)

if _, err := client.Media.List(MediaListParams{Limit: 0, Offset: 20}); err != nil {
panic(err)
}

assert := require.New(t)
resp, err := client.Media.List(MediaListParams{Limit: 0, Offset: 20})
assert.NotNil(resp)
assert.Nil(err)
assert.NotEmpty(resp.Media[0].MediaID)
assert.NotNil(resp.Meta)
cl := client.httpClient
client.httpClient = nil
_, err := client.Media.List(MediaListParams{Limit: 0, Offset: 20})
if err == nil {
client.httpClient = cl
panic(errors.New("error expected"))
}
resp, err = client.Media.List(MediaListParams{Limit: 0, Offset: 20})
assert.NotNil(err)
assert.Nil(resp)
client.httpClient = cl

assertRequest(t, "GET", "Media")
}

func TestMedia_Get(t *testing.T) {
expectResponse("MediaGetResponse.json", 200)
MediaID := "MediaID"

MediaID := "98854bc5-ea05-4837-a301-0272523e6156"
assert := require.New(t)
media, err := client.Media.Get(MediaID)
assert.Equal(t, MediaID, media.MediaID)
if err != nil {
panic(err)
}
assert.NotNil(media)
assert.Nil(err)
assert.Equal(MediaID, media.MediaID)
assert.NotEmpty(media.ContentType)

cl := client.httpClient
client.httpClient = nil
_, err = client.Media.Get(MediaID)
if err == nil {
client.httpClient = cl
panic(errors.New("error expected"))
}
media, err = client.Media.Get(MediaID)
assert.NotNil(err)
assert.Nil(media)
client.httpClient = cl

assertRequest(t, "GET", "Media/%s", MediaID)
Expand Down
Loading

0 comments on commit 32ff890

Please sign in to comment.