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
#### GET
* /api/claim-resolve/:name/:claimId
* example: `curl https://spee.ch/api/claim-resolve/doitlive/xyz`
* /api/claim-list/:name
* example: `curl https://spee.ch/api/claim-list/doitlive`
* /api/claim-is-available/:name (
* /api/claim/resolve/:name/:claimId
* example: `curl https://spee.ch/api/claim/resolve/doitlive/xyz`
* /api/claim/list/:name
* example: `curl https://spee.ch/api/claim/list/doitlive`
* /api/claim/availability/:name (
* returns `true`/`false` for whether a name is available through spee.ch
* example: `curl https://spee.ch/api/claim-is-available/doitlive`
* /api/channel-is-available/:name (
* example: `curl https://spee.ch/api/claim/availability/doitlive`
* /api/channel/availability/:name (
* 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
* /api/claim-publish
* example: `curl -X POST -F 'name=MyPictureName' -F 'file=@/path/to/myPicture.jpeg' https://spee.ch/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`
* Parameters:
* `name`
* `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) {
const url = `/api/channel-claims/${name}/${longId}/${page}`;
const url = `/api/channel/claims/${name}/${longId}/${page}`;
return request(url)
.then(({ success, message, data }) => {
console.log('api/channel-claims response:', data);

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -1,57 +1,8 @@
import { call, put, all, takeLatest } from 'redux-saga/effects';
import Request from 'utils/request';
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);
}
import { all } from 'redux-saga/effects';
import { watchFileIsRequested } from './show';
export default function* rootSaga () {
yield all([
helloSaga(),
watchUpdateFileIsAvailable(),
watchFileIsRequested(),
]);
}

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';
module.exports = (app) => {
// 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 => {
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}})
// route to check whether site has published to a channel
app.get('/api/channel/availability/:name', ({ params }, res) => {
checkChannelAvailability(params.name)
.then(result => {
if (result) {
isAvailable = true;
if (result === 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 => {
errorHandlers.handleApiError(originalUrl, ip, error, res);
});
});
// 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 claimId = params.claimId;
// resolve the claim
@ -67,24 +110,9 @@ module.exports = (app) => {
errorHandlers.handleApiError(originalUrl, ip, error, res);
});
});
// 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)
.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 => {
if (result === true) {
res.status(200).json(true);
@ -97,17 +125,17 @@ module.exports = (app) => {
});
});
// 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}`)
.then(resolvedUri => {
res.status(200).json(resolvedUri);
})
.catch(error => {
errorHandlers.handleApiError(originalUrl, ip, error, res);
});
.then(resolvedUri => {
res.status(200).json(resolvedUri);
})
.catch(error => {
errorHandlers.handleApiError(originalUrl, ip, error, res);
});
});
// 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 files:', files);
// 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
authenticateIfNoUserToken(channelName, channelPassword, user)
.then(authenticated => {
if (!authenticated) {
throw new Error('Authentication failed, you do not have access to that channel');
}
// make sure the claim name is available
return checkClaimNameAvailability(name);
})
.then(result => {
if (!result) {
throw new Error('That name is already claimed by another user.');
}
// create publish parameters object
return createPublishParams(filePath, name, title, description, license, nsfw, thumbnail, channelName);
})
.then(publishParams => {
// set the timing event type for reporting
timingActionType = returnPublishTimingActionType(publishParams.channel_name);
// publish the asset
return publish(publishParams, fileName, fileType);
})
.then(result => {
res.status(200).json({
success: true,
message: 'publish completed successfully',
data : {
name,
claimId: result.claim_id,
url : `${site.host}/${result.claim_id}/${name}`,
lbryTx : result,
},
.then(authenticated => {
if (!authenticated) {
throw new Error('Authentication failed, you do not have access to that channel');
}
// make sure the claim name is available
return checkClaimNameAvailability(name);
})
.then(result => {
if (!result) {
throw new Error('That name is already claimed by another user.');
}
// create publish parameters object
return createPublishParams(filePath, name, title, description, license, nsfw, thumbnail, channelName);
})
.then(publishParams => {
// set the timing event type for reporting
timingActionType = returnPublishTimingActionType(publishParams.channel_name);
// publish the asset
return publish(publishParams, fileName, fileType);
})
.then(result => {
res.status(200).json({
success: true,
message: 'publish completed successfully',
data : {
name,
claimId: result.claim_id,
url : `${site.host}/${result.claim_id}/${name}`,
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
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)
.then(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});
});
});
// route to get a short channel id from long channel Id
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) => {
app.post('/api/claim/long-id', ({ ip, originalUrl, body, params }, res) => {
logger.debug('body:', body);
const channelName = body.channelName;
const channelClaimId = body.channelClaimId;
@ -245,7 +228,7 @@ module.exports = (app) => {
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;
let claimId = params.claimId;
if (claimId === 'none') claimId = null;
@ -261,4 +244,20 @@ module.exports = (app) => {
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 => {
// logger.debug('fileInfo:', fileInfo);
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);
})

View file

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