Skip to content

Commit

Permalink
feat(api): dispatch requests to soundcloud api
Browse files Browse the repository at this point in the history
  • Loading branch information
r-park committed Aug 2, 2016
1 parent bd6eb54 commit af07890
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 0 deletions.
107 changes: 107 additions & 0 deletions src/core/api/_specs/api-service.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { API_BASE_URL, CLIENT_ID_PARAM, PAGINATION_PARAMS } from 'src/core/constants';
import { api, dispatch, requestUrl } from '../api-service';


describe('api', () => {
describe('service', () => {
let body;
let server;
let successResponse;


beforeEach(() => {
body = {};
server = sinon.fakeServer.create();
successResponse = [200, {'Content-type': 'application/json'}, JSON.stringify(body)];
});


afterEach(() => {
server.restore();
});


describe('requestUrl()', () => {
it('should add client id param to url', () => {
let url = requestUrl({url: API_BASE_URL});
expect(url).toMatch(CLIENT_ID_PARAM);
});

it('should NOT add duplicate client id param to url', () => {
let regExp = new RegExp(CLIENT_ID_PARAM, 'gi');
let url = requestUrl({url: `${API_BASE_URL}?${CLIENT_ID_PARAM}`});
expect(url.match(regExp).length).toBe(1);
});

it('should add pagination params to url if options.paginate is true', () => {
let url = requestUrl({paginate: true, url: API_BASE_URL});
expect(url).toMatch(PAGINATION_PARAMS);
});

it('should NOT add pagination params to url by default', () => {
let url = requestUrl({url: API_BASE_URL});
expect(url).not.toMatch(PAGINATION_PARAMS);
});
});


describe('dispatch()', () => {
const url = `${API_BASE_URL}?${CLIENT_ID_PARAM}`;

it('should set request header `Accept: application/json`', () => {
server.respondWith('get', url, ({requestHeaders}) => {
expect(requestHeaders.Accept).toBe('application/json');
return successResponse;
});

dispatch({url: API_BASE_URL});
server.respond();
});

it('should resolve promise with response body', done => {
server.respondWith('get', url, successResponse);

dispatch({url: API_BASE_URL})
.then(responseBody => {
expect(responseBody).toEqual(body);
done();
})
.catch(error => {
fail(error);
done();
});

server.respond();
});

it('should reject promise for soundcloud error codes', () => {
[400, 401, 403, 404, 406, 422, 429, 500, 503, 504]
.forEach(code => {
server.respondWith([code, {}, JSON.stringify({})]);

dispatch({url: API_BASE_URL})
.catch(error => {
expect(error.status).toBe(code);
});

server.respond();
});
});
});


describe('api.fetch()', () => {
it('should perform GET request with correct url', () => {
const url = `${API_BASE_URL}?${CLIENT_ID_PARAM}`;

server.respondWith('get', url, request => {
expect(request.url).toBe(url);
return successResponse;
});

api.fetch(url);
server.respond();
});
});
});
});
30 changes: 30 additions & 0 deletions src/core/api/api-service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import request from 'superagent';
import { CLIENT_ID_PARAM, PAGINATION_PARAMS } from 'src/core/constants';


export const api = {
fetch(url) {
return dispatch({url});
}
};


export function dispatch(options) {
return request[options.method || 'get'](requestUrl(options))
.set('Accept', 'application/json')
.then(response => response.body);
}

export function requestUrl({paginate, url}) {
let params = [];

if (!url.includes(CLIENT_ID_PARAM)) params.push(CLIENT_ID_PARAM);
if (paginate) params.push(PAGINATION_PARAMS);

if (params.length) {
url += url.indexOf('?') === -1 ? '?' : '&';
url += params.join('&');
}

return url;
}
16 changes: 16 additions & 0 deletions src/core/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//=========================================================
// CONSTANTS
//---------------------------------------------------------
export const APP_NAME = 'soundcloud-redux';


//=====================================
// API
//-------------------------------------
export const API_BASE_URL = 'https://api.soundcloud.com';

export const CLIENT_ID = process.env.SOUNDCLOUD_CLIENT_ID || 'd02c42795f3bcac39f84eee0ae384b00';
export const CLIENT_ID_PARAM = `client_id=${CLIENT_ID}`;

export const PAGINATION_LIMIT = 3;
export const PAGINATION_PARAMS = `limit=${PAGINATION_LIMIT}&linked_partitioning=1`;

0 comments on commit af07890

Please sign in to comment.