Skip to content

Commit

Permalink
[CON-1037] fix(Outreach): ReadResult.Raw
Browse files Browse the repository at this point in the history
  • Loading branch information
Cobalt0s committed Feb 19, 2025
1 parent 247223c commit 083a0f8
Show file tree
Hide file tree
Showing 7 changed files with 293 additions and 2,085 deletions.
1 change: 1 addition & 0 deletions providers/outreach/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type DataItem struct {
ID int `json:"id"`
Relationships map[string]any `json:"relationships"`
Attributes map[string]any `json:"attributes"`
Links map[string]any `json:"links"`
}

func (c *Connector) ListObjectMetadata(ctx context.Context,
Expand Down
51 changes: 4 additions & 47 deletions providers/outreach/parse.go
Original file line number Diff line number Diff line change
@@ -1,57 +1,14 @@
package outreach

import (
"encoding/json"

"github.com/amp-labs/connectors/internal/jsonquery"
"github.com/spyzhov/ajson"
)

// getNextRecords returns the "next" url for the next page of results,
// If available, else returns an empty string.
func getNextRecordsURL(node *ajson.Node) (string, error) {
nextPageURL, err := jsonquery.New(node, "links").StringOptional("next")
if err != nil {
return "", err
}

if nextPageURL == nil {
return "", nil
}

return *nextPageURL, nil
func getRecords(node *ajson.Node) ([]*ajson.Node, error) {
return jsonquery.New(node).ArrayOptional(dataKey)
}

// getRecords returns the records from the response.
func getRecords(node *ajson.Node) ([]map[string]any, error) {
var d Data

b := node.Source()
if err := json.Unmarshal(b, &d); err != nil {
return nil, err
}

records := constructRecords(d)

return records, nil
}

func constructRecords(d Data) []map[string]any {
records := make([]map[string]any, len(d.Data))

for idx, record := range d.Data {
recordItems := make(map[string]any)
recordItems[idKey] = record.ID

// Attributes are flattened into the recordItems map.
for k, v := range record.Attributes {
recordItems[k] = v
}

recordItems[relationshipsKey] = record.Relationships

records[idx] = recordItems
}

return records
func getNextRecordsURL(node *ajson.Node) (string, error) {
return jsonquery.New(node, "links").StrWithDefault("next", "")
}
2 changes: 1 addition & 1 deletion providers/outreach/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (c *Connector) Read(ctx context.Context, config common.ReadParams) (*common
return common.ParseResult(res,
getRecords,
getNextRecordsURL,
common.GetMarshaledData,
common.MakeMarshaledDataFunc(common.RecordFlattener(attributesKey)),
config.Fields,
)
}
Expand Down
49 changes: 27 additions & 22 deletions providers/outreach/read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func TestRead(t *testing.T) { // nolint:funlen,gocognit,cyclop
Name: "Successfully Read Mailings",
Input: common.ReadParams{
ObjectName: "mailings",
Fields: connectors.Fields("bodyHtml", "errorReason", "id"),
Fields: connectors.Fields("bodyHtml", "errorReason", "id", "type"),
},
Server: mockserver.Fixed{
Setup: mockserver.ContentJSON(),
Expand All @@ -82,31 +82,36 @@ func TestRead(t *testing.T) { // nolint:funlen,gocognit,cyclop
Rows: 1,
Data: []common.ReadResultRow{{
Fields: map[string]any{
// Some values come from the nested "attributes".
"bodyhtml": "\u003chtml\u003e\u003cbody\u003e\u003cp\u003eHere Goes your HTML email\u003c/p\u003e\u003c/body\u003e\u003e\u003c/html\u003e", //nolint:lll
"errorreason": nil,
"id": 1,
"id": float64(1),
"type": "mailing",
},
Raw: map[string]any{
"bodyHtml": "\u003chtml\u003e\u003cbody\u003e\u003cp\u003eHere Goes your HTML email\u003c/p\u003e\u003c/body\u003e\u003e\u003c/html\u003e", //nolint:lll
"bodyText": "Here Goes your HTML email\u003e",
"clickCount": float64(0),
"errorreason": nil,
"createdAt": "2024-07-26T06:27:17.000Z",
"followUpTaskType": "string",
"mailboxAddress": "[email protected]",
"mailingType": "sequence",
"id": 1,
"openCount": float64(0),
"overrideSafetySettings": false,
"references": []any{},
"retryCount": float64(0),
"scheduledAt": "2019-08-24T14:15:22.000Z",
"state": "drafted",
"stateChangedAt": "2024-07-26T06:27:17.000Z",
"subject": "My Email Subject",
"trackLinks": true,
"trackOpens": true,
"updatedAt": "2024-08-02T22:28:21.000Z",
"id": float64(1),
"type": "mailing",
"attributes": map[string]any{
"bodyHtml": "\u003chtml\u003e\u003cbody\u003e\u003cp\u003eHere Goes your HTML email\u003c/p\u003e\u003c/body\u003e\u003e\u003c/html\u003e", //nolint:lll
"bodyText": "Here Goes your HTML email\u003e",
"clickCount": float64(0),
"errorreason": nil,
"createdAt": "2024-07-26T06:27:17.000Z",
"followUpTaskType": "string",
"mailboxAddress": "[email protected]",
"mailingType": "sequence",
"openCount": float64(0),
"overrideSafetySettings": false,
"references": []any{},
"retryCount": float64(0),
"scheduledAt": "2019-08-24T14:15:22.000Z",
"state": "drafted",
"stateChangedAt": "2024-07-26T06:27:17.000Z",
"subject": "My Email Subject",
"trackLinks": true,
"trackOpens": true,
"updatedAt": "2024-08-02T22:28:21.000Z",
},
},
}},
NextPage: "",
Expand Down
2 changes: 1 addition & 1 deletion test/outreach/read/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func testReadSequences(ctx context.Context, conn *outreach.Connector) error {
config := connectors.ReadParams{
ObjectName: "sequences",
Since: time.Now().Add(-720 * time.Hour),
Fields: connectors.Fields("openCount", "description", "id"),
Fields: connectors.Fields("openCount", "description", "id", "links", "type"),
}

result, err := conn.Read(ctx, config)
Expand Down
143 changes: 143 additions & 0 deletions test/outreach/read/sample_responses/mailings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
{
"rows": 1,
"data": [
{
"fields": {
"bodyhtml": "\u003chtml\u003e\u003cbody\u003e\u003cp\u003eHere Goes your HTML email\u003c/p\u003e\u003c/body\u003e\u003e\u003c/html\u003e",
"errorreason": null,
"id": 33
},
"raw": {
"attributes": {
"attributableSequenceId": null,
"attributableSequenceName": null,
"bodyHtml": "\u003chtml\u003e\u003cbody\u003e\u003cp\u003eHere Goes your HTML email\u003c/p\u003e\u003c/body\u003e\u003e\u003c/html\u003e",
"bodyText": "Here Goes your HTML email\u003e",
"bouncedAt": null,
"clickCount": 0,
"clickedAt": null,
"createdAt": "2025-01-13T11:36:42.000Z",
"deliveredAt": null,
"desiredAt": null,
"errorBacktrace": null,
"errorReason": null,
"followUpSequenceId": null,
"followUpSequenceName": null,
"followUpSequenceStartingDate": null,
"followUpTaskScheduledAt": null,
"followUpTaskType": null,
"mailboxAddress": "[email protected]",
"mailingType": "sequence",
"markedAsSpamAt": null,
"meetingDescription": null,
"meetingDuration": null,
"meetingLocation": null,
"meetingTitle": null,
"messageId": null,
"notifyThreadCondition": null,
"notifyThreadScheduledAt": null,
"notifyThreadStatus": null,
"openCount": 0,
"openedAt": null,
"optimizedScheduledAt": null,
"overrideSafetySettings": false,
"references": [],
"repliedAt": null,
"replySentiment": null,
"retryAt": null,
"retryCount": 0,
"retryInterval": null,
"scheduleId": null,
"scheduledAt": null,
"state": "drafted",
"stateChangedAt": "2025-01-13T11:36:42.000Z",
"subject": "string",
"trackLinks": null,
"trackOpens": null,
"unsubscribedAt": null,
"updatedAt": "2025-01-13T11:36:42.000Z"
},
"id": 33,
"links": {
"self": "https://api.outreach.io/api/v2/mailings/33"
},
"relationships": {
"attachments": {
"data": [],
"links": {
"related": "https://api.outreach.io/api/v2/attachments?filter%5Bmailing%5D%5Bid%5D=33"
},
"meta": {
"count": 0
}
},
"calendar": {
"data": null
},
"followUpSequence": {
"data": null
},
"mailbox": {
"data": {
"id": 1,
"type": "mailbox"
}
},
"opportunity": {
"data": null
},
"prospect": {
"data": {
"id": 1,
"type": "prospect"
}
},
"recipients": {
"data": [],
"links": {
"related": "https://api.outreach.io/api/v2/recipients?filter%5Bmailing%5D%5Bid%5D=33"
},
"meta": {
"count": 0
}
},
"schedule": {
"data": null
},
"sequence": {
"data": null
},
"sequenceState": {
"data": null
},
"sequenceStep": {
"data": null
},
"task": {
"data": null
},
"tasks": {
"data": [],
"links": {
"related": "https://api.outreach.io/api/v2/tasks?filter%5Bmailing%5D%5Bid%5D=33"
},
"meta": {
"count": 0
}
},
"template": {
"data": null
},
"user": {
"data": {
"id": 1,
"type": "user"
}
}
},
"type": "mailing"
}
}
],
"done": true
}
Loading

0 comments on commit 083a0f8

Please sign in to comment.