Skip to content

Commit

Permalink
Removed getAuthToken from Amazon AMC while creating audience (#2286)
Browse files Browse the repository at this point in the history
Co-authored-by: Gaurav Kochar <[email protected]>
  • Loading branch information
Innovative-GauravKochar and Gaurav Kochar authored Aug 26, 2024
1 parent 840ac25 commit 410fa13
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import nock from 'nock'
import { createTestIntegration, InvalidAuthenticationError } from '@segment/actions-core'
import { createTestIntegration } from '@segment/actions-core'
import Definition from '../index'
import { HTTPError } from '@segment/actions-core/*'
import { AUTHORIZATION_URL } from '../utils'
Expand Down Expand Up @@ -116,15 +116,6 @@ describe('Amazon-Ads (actions)', () => {
)
})

it('should fail if refresh token API gets failed', async () => {
const endpoint = AUTHORIZATION_URL[`${settings.region}`]
nock(`${endpoint}`).post('/auth/o2/token').reply(401)

await expect(testDestination.createAudience(createAudienceInputTemp)).rejects.toThrowError(
InvalidAuthenticationError
)
})

it('should throw an HTTPError when createAudience API response is not ok', async () => {
const endpoint = AUTHORIZATION_URL[`${settings.region}`]
nock(`${endpoint}`).post('/auth/o2/token').reply(200)
Expand All @@ -138,9 +129,6 @@ describe('Amazon-Ads (actions)', () => {
})

it('creates an audience', async () => {
const endpoint = AUTHORIZATION_URL[`${settings.region}`]
nock(`${endpoint}`).post('/auth/o2/token').reply(200)

nock(`${settings.region}`)
.post('/amc/audiences/metadata')
.matchHeader('content-type', 'application/vnd.amcaudiences.v1+json')
Expand Down Expand Up @@ -205,13 +193,6 @@ describe('Amazon-Ads (actions)', () => {
await expect(audiencePromise).rejects.toHaveProperty('response.statusText', 'Not Found')
await expect(audiencePromise).rejects.toHaveProperty('response.status', 404)
})
it('should fail if refresh token API gets failed ', async () => {
const endpoint = AUTHORIZATION_URL[`${settings.region}`]
nock(`${endpoint}`).post('/auth/o2/token').reply(401)

const audiencePromise = testDestination.getAudience(getAudienceInput)
await expect(audiencePromise).rejects.toThrow(InvalidAuthenticationError)
})

it('should throw an IntegrationError when the audienceId is not provided', async () => {
getAudienceInput.externalId = ''
Expand Down
17 changes: 2 additions & 15 deletions packages/destination-actions/src/destinations/amazon-amc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import type { Settings, AudienceSettings } from './generated-types'
import {
AudiencePayload,
extractNumberAndSubstituteWithStringValue,
getAuthSettings,
getAuthToken,
REGEX_ADVERTISERID,
REGEX_AUDIENCEID
Expand Down Expand Up @@ -198,10 +197,6 @@ const destination: AudienceDestinationDefinition<Settings, AudienceSettings> = {
})
}

// @ts-ignore - TS doesn't know about the oauth property
const authSettings = getAuthSettings(settings)
const authToken = await getAuthToken(request, createAudienceInput.settings, authSettings)

let payloadString = JSON.stringify(payload)
// Regular expression to find a advertiserId numeric string and replace the quoted advertiserId string with an unquoted number
// AdvertiserId is very big number string and can not be assigned or converted to number directly as it changes the value due to integer overflow.
Expand All @@ -211,8 +206,7 @@ const destination: AudienceDestinationDefinition<Settings, AudienceSettings> = {
method: 'POST',
body: payloadString,
headers: {
'Content-Type': 'application/vnd.amcaudiences.v1+json',
authorization: `Bearer ${authToken}`
'Content-Type': 'application/vnd.amcaudiences.v1+json'
}
})

Expand All @@ -229,18 +223,11 @@ const destination: AudienceDestinationDefinition<Settings, AudienceSettings> = {
const audience_id = getAudienceInput.externalId
const { settings } = getAudienceInput
const endpoint = settings.region

if (!audience_id) {
throw new IntegrationError('Missing audienceId value', 'MISSING_REQUIRED_FIELD', 400)
}
// @ts-ignore - TS doesn't know about the oauth property
const authSettings = getAuthSettings(settings)
const authToken = await getAuthToken(request, settings, authSettings)
const response = await request(`${endpoint}/amc/audiences/metadata/${audience_id}`, {
method: 'GET',
headers: {
authorization: `Bearer ${authToken}`
}
method: 'GET'
})
const res = await response.text()
// Regular expression to find a audienceId number and replace the audienceId with quoted string
Expand Down

0 comments on commit 410fa13

Please sign in to comment.