updated routes to better REST style

This commit is contained in:
bill bittner 2018-02-06 21:55:04 -08:00
parent c9d4aed229
commit b3f5d83f73
13 changed files with 205 additions and 203 deletions

View file

@ -29,20 +29,20 @@ spee.ch is a single-serving site that reads and publishes images and videos to a
## API ## API
#### GET #### GET
* /api/claim-resolve/:name/:claimId * /api/claim/resolve/:name/:claimId
* example: `curl https://spee.ch/api/claim-resolve/doitlive/xyz` * example: `curl https://spee.ch/api/claim/resolve/doitlive/xyz`
* /api/claim-list/:name * /api/claim/list/:name
* example: `curl https://spee.ch/api/claim-list/doitlive` * example: `curl https://spee.ch/api/claim/list/doitlive`
* /api/claim-is-available/:name ( * /api/claim/availability/:name (
* returns `true`/`false` for whether a name is available through spee.ch * returns `true`/`false` for whether a name is available through spee.ch
* example: `curl https://spee.ch/api/claim-is-available/doitlive` * example: `curl https://spee.ch/api/claim/availability/doitlive`
* /api/channel-is-available/:name ( * /api/channel/availability/:name (
* returns `true`/`false` for whether a channel is available through spee.ch * returns `true`/`false` for whether a channel is available through spee.ch
* example: `curl https://spee.ch/api/channel-is-available/@CoolChannel` * example: `curl https://spee.ch/api/channel/availability/@CoolChannel`
#### POST #### POST
* /api/claim-publish * /api/claim/publish
* example: `curl -X POST -F 'name=MyPictureName' -F 'file=@/path/to/myPicture.jpeg' https://spee.ch/api/claim-publish` * example: `curl -X POST -F 'name=MyPictureName' -F 'file=@/path/to/myPicture.jpeg' https://spee.ch/api/claim/publish`
* Parameters: * Parameters:
* `name` * `name`
* `file` (must be type .mp4, .jpeg, .jpg, .gif, or .png) * `file` (must be type .mp4, .jpeg, .jpg, .gif, or .png)

11
react/api/fileApi.js Normal file
View file

@ -0,0 +1,11 @@
import Request from 'utils/request';
export function checkFileAvailability (name, claimId) {
const url = `/api/file/availability/${name}/${claimId}`;
return Request(url);
}
export function triggerClaimGet (name, claimId) {
const url = `/api/claim/get/${name}/${claimId}`;
return Request(url);
}

View file

@ -20,7 +20,7 @@ class ChannelClaimsDisplay extends React.Component {
} }
} }
updateClaimsData (name, longId, page) { updateClaimsData (name, longId, page) {
const url = `/api/channel-claims/${name}/${longId}/${page}`; const url = `/api/channel/claims/${name}/${longId}/${page}`;
return request(url) return request(url)
.then(({ success, message, data }) => { .then(({ success, message, data }) => {
console.log('api/channel-claims response:', data); console.log('api/channel-claims response:', data);

View file

@ -37,7 +37,7 @@ class ChannelCreateForm extends React.Component {
} }
updateIsChannelAvailable (channel) { updateIsChannelAvailable (channel) {
const channelWithAtSymbol = `@${channel}`; const channelWithAtSymbol = `@${channel}`;
request(`/api/channel-is-available/${channelWithAtSymbol}`) request(`/api/channel/availability/${channelWithAtSymbol}`)
.then(isAvailable => { .then(isAvailable => {
if (isAvailable) { if (isAvailable) {
this.setState({'error': null}); this.setState({'error': null});
@ -52,7 +52,7 @@ class ChannelCreateForm extends React.Component {
checkIsChannelAvailable (channel) { checkIsChannelAvailable (channel) {
const channelWithAtSymbol = `@${channel}`; const channelWithAtSymbol = `@${channel}`;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
request(`/api/channel-is-available/${channelWithAtSymbol}`) request(`/api/channel/availability/${channelWithAtSymbol}`)
.then(isAvailable => { .then(isAvailable => {
console.log('checkIsChannelAvailable result:', isAvailable); console.log('checkIsChannelAvailable result:', isAvailable);
if (!isAvailable) { if (!isAvailable) {

View file

@ -48,7 +48,7 @@ class PublishForm extends React.Component {
} }
makePublishRequest (file, metadata) { makePublishRequest (file, metadata) {
console.log('making publish request'); console.log('making publish request');
const uri = '/api/claim-publish'; const uri = '/api/claim/publish';
const xhr = new XMLHttpRequest(); const xhr = new XMLHttpRequest();
const fd = this.appendDataToFormData(file, metadata); const fd = this.appendDataToFormData(file, metadata);
xhr.upload.addEventListener('loadstart', () => { xhr.upload.addEventListener('loadstart', () => {

View file

@ -37,7 +37,7 @@ class PublishUrlInput extends React.Component {
this.props.onClaimChange(cleanClaimName); this.props.onClaimChange(cleanClaimName);
} }
checkClaimIsAvailable (claim) { checkClaimIsAvailable (claim) {
request(`/api/claim-is-available/${claim}`) request(`/api/claim/availability/${claim}`)
.then(isAvailable => { .then(isAvailable => {
// console.log('checkClaimIsAvailable request response:', isAvailable); // console.log('checkClaimIsAvailable request response:', isAvailable);
if (isAvailable) { if (isAvailable) {

View file

@ -49,7 +49,7 @@ class ShowAsset extends React.Component {
}); });
} }
getLongClaimId (params) { getLongClaimId (params) {
const url = `/api/claim-get-long-id`; const url = `/api/claim/long-id`;
console.log('params:', params); console.log('params:', params);
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
request(url, params) request(url, params)
@ -66,7 +66,7 @@ class ShowAsset extends React.Component {
}); });
} }
getShortClaimId (longId, name) { getShortClaimId (longId, name) {
const url = `/api/claim-shorten-id/${longId}/${name}`; const url = `/api/claim/short-id/${longId}/${name}`;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
request(url) request(url)
.then(({ success, message, data }) => { .then(({ success, message, data }) => {
@ -83,7 +83,7 @@ class ShowAsset extends React.Component {
} }
getClaimData (claimId, claimName) { getClaimData (claimId, claimName) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const url = `/api/claim-get-data/${claimName}/${claimId}`; const url = `/api/claim/data/${claimName}/${claimId}`;
return request(url) return request(url)
.then(({ success, message }) => { .then(({ success, message }) => {
console.log('get claim data response:', message); console.log('get claim data response:', message);

View file

@ -20,7 +20,7 @@ class ShowChannel extends React.Component {
} }
getAndStoreChannelData (name, id) { getAndStoreChannelData (name, id) {
if (!id) id = 'none'; if (!id) id = 'none';
const url = `/api/channel-data/${name}/${id}`; const url = `/api/channel/data/${name}/${id}`;
return request(url) return request(url)
.then(({ success, message, data }) => { .then(({ success, message, data }) => {
console.log('api/channel-data response:', data); console.log('api/channel-data response:', data);

View file

@ -1,57 +1,8 @@
import { call, put, all, takeLatest } from 'redux-saga/effects'; import { all } from 'redux-saga/effects';
import Request from 'utils/request'; import { watchFileIsRequested } from './show';
import * as actions from 'constants/show_action_types';
import { updateFileIsAvailable, updateShowAssetError } from 'actions/show';
import { UNAVAILABLE, AVAILABLE } from 'constants/asset_display_states';
function* helloSaga () {
console.log('Hello Sagas!');
}
function* retriveFile (action) {
const name = action.data.name;
const claimId = action.data.claimId;
// see if the file is available
console.log(`checking if file is available for ${name}#${claimId}`);
let url = `/api/file-is-available/${name}/${claimId}`;
let success, message, isAvailable;
try {
({ success, message, data: isAvailable } = yield call(Request, url));
} catch (error) {
return yield put(updateShowAssetError(error.message));
};
if (success) {
console.log('/api/file-is-available response:', isAvailable);
if (isAvailable) {
return yield put(updateFileIsAvailable(AVAILABLE));
}
yield put(updateFileIsAvailable(UNAVAILABLE));
} else {
yield put(updateShowAssetError(message));
}
// initiate get request for the file
console.log(`getting claim for ${name}#${claimId}`);
url = `/api/claim-get/${name}/${claimId}`;
try {
({ success, message } = yield call(Request, url));
} catch (error) {
return yield put(updateShowAssetError(error.message));
};
if (success) {
console.log('/api/glaim-get response:', message);
yield put(updateFileIsAvailable(AVAILABLE));
} else {
yield put(updateShowAssetError(message));
}
}
function* watchUpdateFileIsAvailable () {
yield takeLatest(actions.FILE_REQUESTED, retriveFile);
}
export default function* rootSaga () { export default function* rootSaga () {
yield all([ yield all([
helloSaga(), watchFileIsRequested(),
watchUpdateFileIsAvailable(),
]); ]);
} }

41
react/sagas/show.js Normal file
View file

@ -0,0 +1,41 @@
import { call, put, takeLatest } from 'redux-saga/effects';
import * as actions from 'constants/show_action_types';
import { updateFileIsAvailable, updateShowAssetError } from 'actions/show';
import { UNAVAILABLE, AVAILABLE } from 'constants/asset_display_states';
import { checkFileAvailability, triggerClaimGet } from 'api/fileApi';
function* retriveFile (action) {
const name = action.data.name;
const claimId = action.data.claimId;
// see if the file is available
let success, message, isAvailable;
try {
({ success, message, data: isAvailable } = yield call(checkFileAvailability, name, claimId));
} catch (error) {
return yield put(updateShowAssetError(error.message));
};
if (success) {
if (isAvailable) {
return yield put(updateFileIsAvailable(AVAILABLE));
}
yield put(updateFileIsAvailable(UNAVAILABLE));
} else {
yield put(updateShowAssetError(message));
}
// initiate get request for the file
try {
({ success, message } = yield call(triggerClaimGet, name, claimId));
} catch (error) {
return yield put(updateShowAssetError(error.message));
};
if (success) {
console.log('/api/glaim-get response:', message);
yield put(updateFileIsAvailable(AVAILABLE));
} else {
yield put(updateShowAssetError(message));
}
}
export function* watchFileIsRequested () {
yield takeLatest(actions.FILE_REQUESTED, retriveFile);
}

View file

@ -15,34 +15,77 @@ const NO_CHANNEL = 'NO_CHANNEL';
const NO_CLAIM = 'NO_CLAIM'; const NO_CLAIM = 'NO_CLAIM';
module.exports = (app) => { module.exports = (app) => {
// route to run a claim_list request on the daemon // route to check whether site has published to a channel
app.get('/api/claim-list/:name', ({ ip, originalUrl, params }, res) => { app.get('/api/channel/availability/:name', ({ params }, res) => {
getClaimList(params.name) checkChannelAvailability(params.name)
.then(claimsList => {
res.status(200).json(claimsList);
})
.catch(error => {
errorHandlers.handleApiError(originalUrl, ip, error, res);
});
});
// route to see if asset is available locally
app.get('/api/file-is-available/:name/:claimId', ({ ip, originalUrl, params }, res) => {
const name = params.name;
const claimId = params.claimId;
let isAvailable = false;
db.File.findOne({where: {name, claimId}})
.then(result => { .then(result => {
if (result) { if (result === true) {
isAvailable = true; res.status(200).json(true);
} else {
res.status(200).json(false);
} }
res.status(200).json({success: true, data: isAvailable}); })
.catch(error => {
res.status(500).json(error);
});
});
// route to get a short channel id from long channel Id
app.get('/api/channel/short-id/:longId/:name', ({ ip, originalUrl, params }, res) => {
db.Certificate.getShortChannelIdFromLongChannelId(params.longId, params.name)
.then(shortId => {
logger.debug('sending back short channel id', shortId);
res.status(200).json(shortId);
})
.catch(error => {
logger.error('api error getting short channel id', error);
errorHandlers.handleApiError(originalUrl, ip, error, res);
});
});
app.get('/api/channel/data/:channelName/:channelClaimId', ({ ip, originalUrl, body, params }, res) => {
const channelName = params.channelName;
let channelClaimId = params.channelClaimId;
if (channelClaimId === 'none') channelClaimId = null;
getChannelData(channelName, channelClaimId, 0) // getChannelViewData(channelName, channelId, 0)
.then(data => {
if (data === NO_CHANNEL) {
return res.status(200).json({success: false, message: 'No matching channel was found'});
}
res.status(200).json({success: true, data});
})
.catch(error => {
logger.error('api error getting channel contents', error);
errorHandlers.handleApiError(originalUrl, ip, error, res);
});
});
app.get('/api/channel/claims/:channelName/:channelClaimId/:page', ({ ip, originalUrl, body, params }, res) => {
const channelName = params.channelName;
let channelClaimId = params.channelClaimId;
if (channelClaimId === 'none') channelClaimId = null;
const page = params.page;
getChannelClaims(channelName, channelClaimId, page)// getChannelViewData(channelName, channelClaimId, page)
.then(data => {
if (data === NO_CHANNEL) {
return res.status(200).json({success: false, message: 'No matching channel was found'});
}
res.status(200).json({success: true, data});
})
.catch(error => {
logger.error('api error getting channel contents', error);
errorHandlers.handleApiError(originalUrl, ip, error, res);
});
});
// route to run a claim_list request on the daemon
app.get('/api/claim/list/:name', ({ ip, originalUrl, params }, res) => {
getClaimList(params.name)
.then(claimsList => {
res.status(200).json(claimsList);
}) })
.catch(error => { .catch(error => {
errorHandlers.handleApiError(originalUrl, ip, error, res); errorHandlers.handleApiError(originalUrl, ip, error, res);
}); });
}); });
// route to get an asset // route to get an asset
app.get('/api/claim-get/:name/:claimId', ({ ip, originalUrl, params }, res) => { app.get('/api/claim/get/:name/:claimId', ({ ip, originalUrl, params }, res) => {
const name = params.name; const name = params.name;
const claimId = params.claimId; const claimId = params.claimId;
// resolve the claim // resolve the claim
@ -67,24 +110,9 @@ module.exports = (app) => {
errorHandlers.handleApiError(originalUrl, ip, error, res); errorHandlers.handleApiError(originalUrl, ip, error, res);
}); });
}); });
// route to check whether this site published to a claim // route to check whether this site published to a claim
app.get('/api/claim-is-available/:name', ({ params }, res) => { app.get('/api/claim/availability/:name', ({ params }, res) => {
checkClaimNameAvailability(params.name) checkClaimNameAvailability(params.name)
.then(result => {
if (result === true) {
res.status(200).json(true);
} else {
res.status(200).json(false);
}
})
.catch(error => {
res.status(500).json(error);
});
});
// route to check whether site has published to a channel
app.get('/api/channel-is-available/:name', ({ params }, res) => {
checkChannelAvailability(params.name)
.then(result => { .then(result => {
if (result === true) { if (result === true) {
res.status(200).json(true); res.status(200).json(true);
@ -97,17 +125,17 @@ module.exports = (app) => {
}); });
}); });
// route to run a resolve request on the daemon // route to run a resolve request on the daemon
app.get('/api/claim-resolve/:name/:claimId', ({ headers, ip, originalUrl, params }, res) => { app.get('/api/claim/resolve/:name/:claimId', ({ headers, ip, originalUrl, params }, res) => {
resolveUri(`${params.name}#${params.claimId}`) resolveUri(`${params.name}#${params.claimId}`)
.then(resolvedUri => { .then(resolvedUri => {
res.status(200).json(resolvedUri); res.status(200).json(resolvedUri);
}) })
.catch(error => { .catch(error => {
errorHandlers.handleApiError(originalUrl, ip, error, res); errorHandlers.handleApiError(originalUrl, ip, error, res);
}); });
}); });
// route to run a publish request on the daemon // route to run a publish request on the daemon
app.post('/api/claim-publish', multipartMiddleware, ({ body, files, headers, ip, originalUrl, user }, res) => { app.post('/api/claim/publish', multipartMiddleware, ({ body, files, headers, ip, originalUrl, user }, res) => {
logger.debug('api/claim-publish body:', body); logger.debug('api/claim-publish body:', body);
logger.debug('api/claim-publish files:', files); logger.debug('api/claim-publish files:', files);
// record the start time of the request and create variable for storing the action type // record the start time of the request and create variable for storing the action type
@ -128,48 +156,48 @@ module.exports = (app) => {
} }
// check channel authorization // check channel authorization
authenticateIfNoUserToken(channelName, channelPassword, user) authenticateIfNoUserToken(channelName, channelPassword, user)
.then(authenticated => { .then(authenticated => {
if (!authenticated) { if (!authenticated) {
throw new Error('Authentication failed, you do not have access to that channel'); throw new Error('Authentication failed, you do not have access to that channel');
} }
// make sure the claim name is available // make sure the claim name is available
return checkClaimNameAvailability(name); return checkClaimNameAvailability(name);
}) })
.then(result => { .then(result => {
if (!result) { if (!result) {
throw new Error('That name is already claimed by another user.'); throw new Error('That name is already claimed by another user.');
} }
// create publish parameters object // create publish parameters object
return createPublishParams(filePath, name, title, description, license, nsfw, thumbnail, channelName); return createPublishParams(filePath, name, title, description, license, nsfw, thumbnail, channelName);
}) })
.then(publishParams => { .then(publishParams => {
// set the timing event type for reporting // set the timing event type for reporting
timingActionType = returnPublishTimingActionType(publishParams.channel_name); timingActionType = returnPublishTimingActionType(publishParams.channel_name);
// publish the asset // publish the asset
return publish(publishParams, fileName, fileType); return publish(publishParams, fileName, fileType);
}) })
.then(result => { .then(result => {
res.status(200).json({ res.status(200).json({
success: true, success: true,
message: 'publish completed successfully', message: 'publish completed successfully',
data : { data : {
name, name,
claimId: result.claim_id, claimId: result.claim_id,
url : `${site.host}/${result.claim_id}/${name}`, url : `${site.host}/${result.claim_id}/${name}`,
lbryTx : result, lbryTx : result,
}, },
});
// log the publish end time
const publishEndTime = Date.now();
logger.debug('publish request completed @', publishEndTime);
sendGoogleAnalyticsTiming(timingActionType, headers, ip, originalUrl, publishStartTime, publishEndTime);
})
.catch(error => {
errorHandlers.handleApiError(originalUrl, ip, error, res);
}); });
// log the publish end time
const publishEndTime = Date.now();
logger.debug('publish request completed @', publishEndTime);
sendGoogleAnalyticsTiming(timingActionType, headers, ip, originalUrl, publishStartTime, publishEndTime);
})
.catch(error => {
errorHandlers.handleApiError(originalUrl, ip, error, res);
});
}); });
// route to get a short claim id from long claim Id // route to get a short claim id from long claim Id
app.get('/api/claim-shorten-id/:longId/:name', ({ params }, res) => { app.get('/api/claim/short-id/:longId/:name', ({ params }, res) => {
db.Claim.getShortClaimIdFromLongClaimId(params.longId, params.name) db.Claim.getShortClaimIdFromLongClaimId(params.longId, params.name)
.then(shortId => { .then(shortId => {
res.status(200).json({success: true, data: shortId}); res.status(200).json({success: true, data: shortId});
@ -179,52 +207,7 @@ module.exports = (app) => {
res.status(200).json({success: false, message: error.message}); res.status(200).json({success: false, message: error.message});
}); });
}); });
// route to get a short channel id from long channel Id app.post('/api/claim/long-id', ({ ip, originalUrl, body, params }, res) => {
app.get('/api/channel-shorten-id/:longId/:name', ({ ip, originalUrl, params }, res) => {
db.Certificate.getShortChannelIdFromLongChannelId(params.longId, params.name)
.then(shortId => {
logger.debug('sending back short channel id', shortId);
res.status(200).json(shortId);
})
.catch(error => {
logger.error('api error getting short channel id', error);
errorHandlers.handleApiError(originalUrl, ip, error, res);
});
});
app.get('/api/channel-data/:channelName/:channelClaimId', ({ ip, originalUrl, body, params }, res) => {
const channelName = params.channelName;
let channelClaimId = params.channelClaimId;
if (channelClaimId === 'none') channelClaimId = null;
getChannelData(channelName, channelClaimId, 0) // getChannelViewData(channelName, channelId, 0)
.then(data => {
if (data === NO_CHANNEL) {
return res.status(200).json({success: false, message: 'No matching channel was found'});
}
res.status(200).json({success: true, data});
})
.catch(error => {
logger.error('api error getting channel contents', error);
errorHandlers.handleApiError(originalUrl, ip, error, res);
});
});
app.get('/api/channel-claims/:channelName/:channelClaimId/:page', ({ ip, originalUrl, body, params }, res) => {
const channelName = params.channelName;
let channelClaimId = params.channelClaimId;
if (channelClaimId === 'none') channelClaimId = null;
const page = params.page;
getChannelClaims(channelName, channelClaimId, page)// getChannelViewData(channelName, channelClaimId, page)
.then(data => {
if (data === NO_CHANNEL) {
return res.status(200).json({success: false, message: 'No matching channel was found'});
}
res.status(200).json({success: true, data});
})
.catch(error => {
logger.error('api error getting channel contents', error);
errorHandlers.handleApiError(originalUrl, ip, error, res);
});
});
app.post('/api/claim-get-long-id', ({ ip, originalUrl, body, params }, res) => {
logger.debug('body:', body); logger.debug('body:', body);
const channelName = body.channelName; const channelName = body.channelName;
const channelClaimId = body.channelClaimId; const channelClaimId = body.channelClaimId;
@ -245,7 +228,7 @@ module.exports = (app) => {
errorHandlers.handleApiError(originalUrl, ip, error, res); errorHandlers.handleApiError(originalUrl, ip, error, res);
}); });
}); });
app.get('/api/claim-get-data/:claimName/:claimId', ({ ip, originalUrl, body, params }, res) => { app.get('/api/claim/data/:claimName/:claimId', ({ ip, originalUrl, body, params }, res) => {
const claimName = params.claimName; const claimName = params.claimName;
let claimId = params.claimId; let claimId = params.claimId;
if (claimId === 'none') claimId = null; if (claimId === 'none') claimId = null;
@ -261,4 +244,20 @@ module.exports = (app) => {
errorHandlers.handleApiError(originalUrl, ip, error, res); errorHandlers.handleApiError(originalUrl, ip, error, res);
}); });
}); });
// route to see if asset is available locally
app.get('/api/file/availability/:name/:claimId', ({ ip, originalUrl, params }, res) => {
const name = params.name;
const claimId = params.claimId;
let isAvailable = false;
db.File.findOne({where: {name, claimId}})
.then(result => {
if (result) {
isAvailable = true;
}
res.status(200).json({success: true, data: isAvailable});
})
.catch(error => {
errorHandlers.handleApiError(originalUrl, ip, error, res);
});
});
}; };

View file

@ -58,7 +58,7 @@ function serveAssetToClient (claimId, name, res) {
.then(fileInfo => { .then(fileInfo => {
// logger.debug('fileInfo:', fileInfo); // logger.debug('fileInfo:', fileInfo);
if (fileInfo === NO_FILE) { if (fileInfo === NO_FILE) {
return res.status(307).redirect(`/api/claim-get/${name}/${claimId}`); return res.status(307).redirect(`/api/claim/get/${name}/${claimId}`);
} }
return serveHelpers.serveFile(fileInfo, claimId, name, res); return serveHelpers.serveFile(fileInfo, claimId, name, res);
}) })

View file

@ -105,7 +105,7 @@ describe('end-to-end', function () {
}); });
describe('publish requests', function () { describe('publish requests', function () {
const publishUrl = '/api/claim-publish'; const publishUrl = '/api/claim/publish';
const filePath = './test/mock-data/bird.jpeg'; const filePath = './test/mock-data/bird.jpeg';
const fileName = 'byrd.jpeg'; const fileName = 'byrd.jpeg';
const channelName = testChannel; const channelName = testChannel;