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

add review submission live api #290

Open
wants to merge 1 commit into
base: entities
Choose a base branch
from
Open
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
19 changes: 13 additions & 6 deletions review.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,25 @@ type Review struct {
}

type ReviewCreate struct {
LocationId *string `json:"locationId"`
ExternalId *string `json:"externalId"` // Must have v param >= 20220120
AccountId *string `json:"accountId"`
Rating *float64 `json:"rating"`
Content *string `json:"content"`
AuthorName *string `json:"authorName"`
LocationId *string `json:"locationId,omitempty"`
ExternalId *string `json:"externalId,omitempty"` // Must have v param >= 20220120
AccountId *string `json:"accountId,omitempty"`
Rating *float64 `json:"rating,omitempty"`
Content *string `json:"content,omitempty"`
AuthorName *string `json:"authorName,omitempty"`
AuthorEmail *string `json:"authorEmail,omitempty"`
Status *string `json:"status,omitempty"`
FlagStatus *string `json:"flagStatus,omitempty"`
ReviewLanguage *string `json:"reviewLanguage,omitempty"`
TransactionId *string `json:"transactionId,omitempty"`
Date *string `json:"date,omitempty"`

//LiveAPI Specific Fields
ReviewEntity *ReviewEntity `json:"entity,omitempty"`
Title *string `json:"title,omitempty"`
ReviewLabelNames *[]string `json:"reviewLabelNames,omitempty"`
InvitationUID *string `json:"invitationUid"`
ReviewDate *string `json:"reviewDate"`
}

type Comment struct {
Expand Down
74 changes: 62 additions & 12 deletions review_service.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package yext

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
Expand Down Expand Up @@ -77,21 +80,20 @@ type ReviewUpdateResponse struct {
}

type ReviewCreateInvitationResponse struct {
InvitationUID string `json:"invitationUid"`
Id string `json:"id"`
LocationId string `json:"locationId"`
Entity *ReviewEntity `json:"entity,omitempty"` // Must have v param >= 20210728
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
Contact string `json:"contact"`
Image bool `json:"image"`
TemplateId int `json:"templateId"`
Status string `json:"status"`
Details string `json:"details"`
Id string `json:"id"`
LocationId string `json:"locationId"`
Entity *ReviewEntity `json:"entity,omitempty"` // Must have v param >= 20210728
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
Contact string `json:"contact"`
Image bool `json:"image"`
TemplateId int `json:"templateId"`
Status string `json:"status"`
Details string `json:"details"`
}

type ReviewCreateReviewResponse struct {
Id string `json:"id"`
Id string `json:"apiIdentifier"`
}

func (l *ReviewService) ListAllWithOptions(rlOpts *ReviewListOptions) ([]*Review, error) {
Expand Down Expand Up @@ -249,6 +251,7 @@ func (l *ReviewService) CreateInvitation(jsonData []*Reviewer) ([]*ReviewCreateI
return v, r, nil
}

//CreateReview: Pls use the new liveAPI implementation below
func (l *ReviewService) CreateReview(jsonData *ReviewCreate) (*ReviewCreateReviewResponse, *Response, error) {
var v *ReviewCreateReviewResponse
r, err := l.client.DoRequestJSON("POST", reviewsPath, jsonData, &v)
Expand All @@ -259,6 +262,53 @@ func (l *ReviewService) CreateReview(jsonData *ReviewCreate) (*ReviewCreateRevie
return v, r, nil
}

//the new way to create reviews on the yext platform
//refer to https://yextops.slack.com/archives/C01269F1ZTL/p1634751884059700
func (l *ReviewService) CreateReviewLiveAPI(jsonData *ReviewCreate) (*ReviewCreateReviewResponse, *Response, error) {
reviewCreateReviewResponse := &ReviewCreateReviewResponse{}
baseURL := "https://liveapi.yext.com"
if strings.Contains(l.client.Config.BaseUrl, "sandbox") {
baseURL = "https://liveapi-sandbox.yext.com"
}

reviewSubmissionLiveAPIURL := fmt.Sprintf("%s/v2/accounts/%s/reviewSubmission?api_key=%s&v=%s", baseURL, l.client.Config.AccountId, l.client.Config.ApiKey, l.client.Config.Version)

client := &http.Client{}
req, err := http.NewRequest(http.MethodPost, reviewSubmissionLiveAPIURL, strings.NewReader(jsonData.String()))
if err != nil {
fmt.Println(err)
return nil, nil, err
}

req.Header.Add("Content-Type", "application/json")

res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return nil, nil, err
}
defer res.Body.Close()

body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, nil, err
}

reviewResponse := &Response{}

err = json.Unmarshal(body, reviewResponse)
if err != nil {
return nil, nil, err
}

err = json.Unmarshal(*reviewResponse.ResponseRaw, reviewCreateReviewResponse)
if err != nil {
return nil, nil, err
}

return reviewCreateReviewResponse, reviewResponse, nil
}

type ReviewUpdateLabelOptions struct {
LabelIds *[]int `json:"labelIds,omitempty"`
}
Expand Down