Skip to content

Commit

Permalink
New SalesWings API urls (#1039)
Browse files Browse the repository at this point in the history
* Use new SalesWings API urls

* Do not check response status in testAuthentication

* Fix testAuthentication

* Remove unused file
  • Loading branch information
denis-egorushkin-sw authored Feb 16, 2023
1 parent 81c45f9 commit eae86be
Show file tree
Hide file tree
Showing 28 changed files with 279 additions and 681 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
import nock from 'nock'
import { createTestIntegration } from '@segment/actions-core'
import Definition from '../index'
import { apiBaseUrl } from '../api'
import { getAccountUrl } from '../api'

const testDestination = createTestIntegration(Definition)

describe('Saleswings', () => {
const env = 'helium'
describe('testAuthentication', () => {
it('should validate authentication inputs', async () => {
nock(apiBaseUrl).get('/project/account').matchHeader('authorization', 'Bearer myApiKey').reply(200, {})
await expect(testDestination.testAuthentication({ apiKey: 'myApiKey' })).resolves.not.toThrowError()
nock(getAccountUrl(env)).get('').matchHeader('authorization', 'Bearer myApiKey').reply(200, {})
await expect(
testDestination.testAuthentication({ apiKey: 'myApiKey', environment: env })
).resolves.not.toThrowError()
})

it('should reject invalid API key', async () => {
nock(apiBaseUrl).get('/project/account').matchHeader('authorization', 'Bearer myApiKey').reply(200, {})
nock(apiBaseUrl).get('/project/account').reply(401, {})
await expect(testDestination.testAuthentication({ apiKey: 'invalidApiKey' })).rejects.toThrowError()
nock(getAccountUrl(env)).get('').matchHeader('authorization', 'Bearer myApiKey').reply(200, {})
nock(getAccountUrl(env)).get('').reply(401, {})
await expect(
testDestination.testAuthentication({ apiKey: 'invalidApiKey', environment: env })
).rejects.toThrowError()
})
})
})
51 changes: 6 additions & 45 deletions packages/destination-actions/src/destinations/saleswings/api.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,7 @@
export const apiBaseUrl = 'https://helium.saleswings.pro/api/core'
export type EventType = 'track' | 'identify' | 'page' | 'screen'

export type Event = TrackingEvent | PageVisitEvent

export type EventBatch = {
events: Event[]
}

export class TrackingEvent {
leadRefs: LeadRef[]
kind: string
data: string
url?: string
referrerUrl?: string
userAgent?: string
timestamp: number
values: ValueMap
readonly type: string = 'tracking'

public constructor(fields?: Partial<TrackingEvent>) {
Object.assign(this, fields)
}
}

export class PageVisitEvent {
leadRefs: LeadRef[]
url: string
referrerUrl?: string
userAgent?: string
timestamp: number
readonly type: string = 'page-visit'

public constructor(fields?: Partial<PageVisitEvent>) {
Object.assign(this, fields)
}
}

export type Value = string | number | boolean
export type ValueMap = { [k: string]: Value }

export type LeadRefType = 'email' | 'client-id'

export type LeadRef = {
type: LeadRefType
value: string
}
export const submitEventUrl = (env: string, eventType: EventType): string =>
`https://${env}.saleswings.pro/api/segment/events/${eventType}`
export const submitEventBatchUrl = (env: string, eventType: EventType): string =>
`https://${env}.saleswings.pro/api/segment/events/${eventType}/batches`
export const getAccountUrl = (env: string): string => `https://${env}.saleswings.pro/api/core/project/account`
29 changes: 7 additions & 22 deletions packages/destination-actions/src/destinations/saleswings/common.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,23 @@
import { RequestFn } from '@segment/actions-core'
import { apiBaseUrl, Event, EventBatch } from './api'
import { submitEventUrl, submitEventBatchUrl, EventType } from './api'
import { Settings } from './generated-types'

export function perform<Payload>(convertEvent: (payload: Payload) => Event | undefined): RequestFn<Settings, Payload> {
export function perform<Payload>(eventType: EventType): RequestFn<Settings, Payload> {
return (request, data) => {
const event = convertEvent(data.payload)
if (!event) return
return request(`${apiBaseUrl}/events`, {
return request(submitEventUrl(data.settings.environment, eventType), {
method: 'post',
json: event,
json: data.payload,
headers: { Authorization: `Bearer ${data.settings.apiKey}` }
})
}
}

export function performBatch<Payload>(
convertEvent: (payload: Payload) => Event | undefined
): RequestFn<Settings, Payload[]> {
export function performBatch<Payload>(eventType: EventType): RequestFn<Settings, Payload[]> {
return (request, data) => {
const batch = convertEventBatch(data.payload, convertEvent)
if (!batch) return
return request(`${apiBaseUrl}/events/batches`, {
return request(submitEventBatchUrl(data.settings.environment, eventType), {
method: 'post',
json: batch,
json: data.payload,
headers: { Authorization: `Bearer ${data.settings.apiKey}` }
})
}
}

function convertEventBatch<Payload>(
payloads: Payload[],
convertEvent: (payload: Payload) => Event | undefined
): EventBatch | undefined {
const events = payloads.map(convertEvent).filter((evt) => evt) as Event[]
if (events.length == 0) return undefined
return { events }
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { InputField } from '@segment/actions-core'
import { Directive } from '@segment/actions-core/src/destination-kit/types'

export const userId: InputField = {
export const userID: InputField = {
label: 'Segment User ID',
description: 'Permanent identifier of a Segment user the event is attributed to.',
type: 'string',
Expand All @@ -11,7 +11,7 @@ export const userId: InputField = {
}
}

export const anonymousId: InputField = {
export const anonymousID: InputField = {
label: 'Segment Anonymous User ID',
description: 'A pseudo-unique substitute for a Segment user ID the event is attributed to.',
type: 'string',
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 13 additions & 3 deletions packages/destination-actions/src/destinations/saleswings/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defaultValues, DestinationDefinition } from '@segment/actions-core'
import { apiBaseUrl } from './api'
import { getAccountUrl } from './api'
import type { Settings } from './generated-types'

import submitTrackEvent from './submitTrackEvent'
Expand All @@ -20,13 +20,23 @@ const destination: DestinationDefinition<Settings> = {
description: 'Segment.io API key for your SalesWings project.',
type: 'password',
required: true
},
environment: {
label: 'Environment',
description: 'SalesWings environment this destination is connected with.',
type: 'string',
choices: [
{ value: 'helium', label: 'Helium (live environment)' },
{ value: 'ozone', label: 'Ozone (test environment)' }
],
required: true,
default: 'helium'
}
},
testAuthentication: async (request, { settings }) => {
const resp = await request(`${apiBaseUrl}/project/account`, {
return request(getAccountUrl('helium'), {
headers: { Authorization: `Bearer ${settings.apiKey}` }
})
return resp.status == 200
}
},

Expand Down
Original file line number Diff line number Diff line change
@@ -1,55 +1,31 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Testing snapshot for 's submitIdentifyEvent destination action: all fields 1`] = `
exports[`Testing snapshot for saleswings's submitIdentifyEvent destination action: all fields 1`] = `
Object {
"anonymousID": "anonId1234",
"data": "peter@example.com",
"email": "peter@example.com",
"kind": "Identify",
"leadRefs": Array [
Object {
"type": "client-id",
"value": "user1234",
},
Object {
"type": "client-id",
"value": "anonId1234",
},
Object {
"type": "email",
"value": "peter@example.com",
},
],
"timestamp": 1671371213652,
"type": "tracking",
"url": "Cge8DWzjce",
"timestamp": "2022-12-18T13:46:53.652Z",
"url": "MrDZodCq([l&%Xby8@jL",
"userAgent": "Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1",
"userID": "user1234",
"values": Object {
"email": "peter@example.com",
},
}
`;

exports[`Testing snapshot for 's submitIdentifyEvent destination action: required fields 1`] = `
exports[`Testing snapshot for saleswings's submitIdentifyEvent destination action: required fields 1`] = `
Object {
"anonymousID": "anonId1234",
"data": "peter@example.com",
"email": "peter@example.com",
"kind": "Identify",
"leadRefs": Array [
Object {
"type": "client-id",
"value": "user1234",
},
Object {
"type": "client-id",
"value": "anonId1234",
},
Object {
"type": "email",
"value": "peter@example.com",
},
],
"timestamp": 1671371213632,
"type": "tracking",
"timestamp": "2022-12-18T13:46:53.632Z",
"url": "https://segment.com/academy/",
"userAgent": "Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1",
"userID": "user1234",
"values": Object {
"email": "peter@example.com",
},
Expand Down
Loading

0 comments on commit eae86be

Please sign in to comment.