Skip to content

Commit

Permalink
feat: add trackStreamStart with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jsalonen committed Oct 8, 2018
1 parent fcea6cc commit bbf3ce1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
25 changes: 25 additions & 0 deletions src/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,28 @@ describe('Client', () => {
expect(url).toMatch(`${IMAGES_URL}w_1920,h_1080,c_fit/image1.jpg`);
})
});

describe('trackStreamStart', () => {
afterEach(fetchMock.restore);

test('returns 200 OK on successful track event registration', async () => {
fetchMock.once('*', {
status: 200,
body: ''
});

const client = makeClient(VALID_APIKEYS);
await client.trackStreamStart('valid-program-id', 'valid-media-id');
});

test('returns 400 Bad Request with invalid or missing parameters', async () => {
fetchMock.once('*', {
status: 400,
body: 'Bad Request'
});

const client = makeClient(VALID_APIKEYS);
const trackStreamStart = client.trackStreamStart('valid-program-id', '');
await expect(trackStreamStart).rejects.toEqual('Track stream failed: 400 Bad Request')
});
});
20 changes: 18 additions & 2 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import fetch from 'node-fetch';
import URI from 'urijs';
import { decrypt } from './mediaurl';
import {
Expand All @@ -16,7 +15,7 @@ import {
CloudinaryImageTransformations,
CloudinaryImageFormat
} from './cloudinary';
import { Response } from 'node-fetch';
import fetch, { Response } from 'node-fetch';

const API_URL = 'https://external.api.yle.fi/v1/';
export const IMAGES_URL = 'https://images.cdn.yle.fi/image/upload/';
Expand Down Expand Up @@ -112,6 +111,23 @@ class Client {
)
}

async trackStreamStart(programId: string, mediaId: string): Promise<void> {
const url =
URI(API_URL)
.segment('tracking')
.segment('streamstart')
.query(this.queryParamsWithCredentials({
program_id: programId,
media_id: mediaId
}))
.toString();

const response = await this.fetcher(url);
if(!response.ok) {
return Promise.reject(`Track stream failed: ${response.status} ${response.statusText}`);
}
}

private decryptMediaUrls(playouts: ApiResponseMediaPlayouts, decryptKey: string) {
return {
...playouts,
Expand Down

0 comments on commit bbf3ce1

Please sign in to comment.