added create collection external endpoint and updated docs styles

This commit is contained in:
Martina 2021-07-13 12:08:56 -07:00
parent 6b133388e0
commit 4ca134e744
21 changed files with 297 additions and 30 deletions

View File

@ -60,7 +60,7 @@ export default class APIDocsGetSlate extends React.Component {
return (
<React.Fragment>
<System.DescriptionGroup
style={{ maxWidth: 640, marginTop: 64 }}
style={{ maxWidth: 640, marginTop: 48, ...this.props.style }}
label="Get collection by ID"
description="This API request will return a specific collection. You can save the response locally and send this JSON back to our API server using the route /api/v1/update-slate to update your collection."
/>

View File

@ -118,7 +118,7 @@ export default class APIDocsGet extends React.Component {
return (
<React.Fragment>
<System.DescriptionGroup
style={{ maxWidth: 640, marginTop: 64 }}
style={{ maxWidth: 640, marginTop: 48, ...this.props.style }}
label="Get your data"
description="This API request returns your user data and collections. If the request body is omitted, the request will return only your public collections by default."
/>

View File

@ -60,7 +60,7 @@ export default class APIDocsUpdateSlate extends React.Component {
return (
<React.Fragment>
<System.DescriptionGroup
style={{ maxWidth: 640, marginTop: 64 }}
style={{ maxWidth: 640, marginTop: 48, ...this.props.style }}
label="Update collection"
description="This API endpoint allows you to modify a collection by saving the response from get-slate, modifying it, and sending it back"
/>

View File

@ -75,7 +75,7 @@ export default class APIDocsUploadToSlate extends React.Component {
return (
<React.Fragment>
<System.DescriptionGroup
style={{ maxWidth: 640, marginTop: 64 }}
style={{ maxWidth: 640, marginTop: 48, ...this.props.style }}
label="Upload"
description={
"This API endpoint allows you to upload a file to your data. This uses our data transfer microservice to interact with Textile Buckets and upload data to the IPFS/Filecoin network."

View File

@ -0,0 +1,73 @@
import * as React from "react";
import * as System from "~/components/system";
import CodeBlock from "~/components/system/CodeBlock";
const EXAMPLE_CODE_JS = (key, slateId) => {
return `const response = await fetch("https://slate.host/api/v2/create-collection", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: "Basic ${key}", // API key
},
body: JSON.stringify({
data: {
name: "My Dog Fido",
isPublic: true,
body: "This is an album of my dog, Fido, a golden retriever",
tags: ["dogs", "retrievers", "golden retriever"]
},
}),
});`;
};
const EXAMPLE_CODE_PY = (key, slateId) =>
`import requests
headers = {
"content-type": "application/json",
"Authorization": "Basic ${key}", # API key
}
postJson = {
data: {
name: "My Dog Fido",
isPublic: true,
body: "This is an album of my dog, Fido, a golden retriever",
tags: ["dogs", "retrievers", "golden retriever"]
}
}
url = "https://slate.host/api/v2/create-collection"
r = requests.post(url, headers=headers, json=postJson)`;
export default class APIDocsCreateCollection extends React.Component {
render() {
let language = this.props.language;
let key = this.props.APIKey;
let slateId = this.props.slateId;
let code = {
javascript: EXAMPLE_CODE_JS(key, slateId),
python: EXAMPLE_CODE_PY(key, slateId),
};
return (
<React.Fragment>
<System.DescriptionGroup
style={{ maxWidth: 640, marginTop: 48, ...this.props.style }}
label="Create Collection"
description="This API endpoint allows you to create a collection. All fields except name are optional."
/>
<CodeBlock
children={code}
style={{ maxWidth: "820px" }}
language={language}
title="Create collection"
multiLang="true"
onLanguageChange={this.props.onLanguageChange}
/>
</React.Fragment>
);
}
}

View File

@ -51,7 +51,7 @@ export default class APIDocsCreateLink extends React.Component {
return (
<React.Fragment>
<System.DescriptionGroup
style={{ maxWidth: 640, marginTop: 64 }}
style={{ maxWidth: 640, marginTop: 48, ...this.props.style }}
label="Create link"
description="This API endpoint allows you to upload a link and optionally add it to a slate. Include a slate id to add it to a slate."
/>

View File

@ -60,7 +60,7 @@ export default class APIDocsGetCollection extends React.Component {
return (
<React.Fragment>
<System.DescriptionGroup
style={{ maxWidth: 640, marginTop: 64 }}
style={{ maxWidth: 640, marginTop: 48, ...this.props.style }}
label="Get collection by ID"
description="This API request will return a specific collection. You can save the response locally and send this JSON back to our API server using the route /api/v2/update-collection to update your collection."
/>

View File

@ -60,7 +60,7 @@ export default class APIDocsGetUser extends React.Component {
return (
<React.Fragment>
<System.DescriptionGroup
style={{ maxWidth: 640, marginTop: 64 }}
style={{ maxWidth: 640, marginTop: 48, ...this.props.style }}
label="Get user by ID"
description="This API request will return a specific user"
/>

View File

@ -119,7 +119,7 @@ export default class APIDocsGet extends React.Component {
return (
<React.Fragment>
<System.DescriptionGroup
style={{ maxWidth: 640, marginTop: 64 }}
style={{ maxWidth: 640, marginTop: 48, ...this.props.style }}
label="Get your data"
description="This API request returns your user data and collections. If the request body is omitted, the request will return only your public collections by default."
/>

View File

@ -61,7 +61,7 @@ export default class APIDocsUpdateFile extends React.Component {
return (
<React.Fragment>
<System.DescriptionGroup
style={{ maxWidth: 640, marginTop: 64 }}
style={{ maxWidth: 640, marginTop: 48, ...this.props.style }}
label="Update file"
description="This API endpoint allows you to modify a file by saving the collection object in the response from get-collection, modifying it, and sending it back"
/>

View File

@ -60,7 +60,7 @@ export default class APIDocsUpdateCollection extends React.Component {
return (
<React.Fragment>
<System.DescriptionGroup
style={{ maxWidth: 640, marginTop: 64 }}
style={{ maxWidth: 640, marginTop: 48, ...this.props.style }}
label="Update collection"
description="This API endpoint allows you to modify a collection by saving the response from get-collection, modifying it, and sending it back"
/>

View File

@ -75,8 +75,8 @@ export default class APIDocsUploadToSlate extends React.Component {
return (
<React.Fragment>
<System.DescriptionGroup
style={{ maxWidth: 640, marginTop: 64 }}
label="Upload"
style={{ maxWidth: 640, marginTop: 48, ...this.props.style }}
label="Upload file"
description={
"This API endpoint allows you to upload file(s) to your collection. This uses our data transfer microservice to interact with Textile Buckets and upload data to the IPFS/Filecoin network."
}
@ -85,7 +85,7 @@ export default class APIDocsUploadToSlate extends React.Component {
children={uploadCode}
style={{ maxWidth: "820px" }}
language={language}
title="Upload"
title="Upload file"
multiLang="true"
onLanguageChange={this.props.onLanguageChange}
/>
@ -94,7 +94,7 @@ export default class APIDocsUploadToSlate extends React.Component {
children={slateUploadCode}
style={{ maxWidth: "820px" }}
language={language}
title="Upload to collection"
title="Upload file to collection"
multiLang="true"
onLanguageChange={this.props.onLanguageChange}
/>

View File

@ -0,0 +1,79 @@
import * as Data from "~/node_common/data";
import * as Strings from "~/common/strings";
import * as Utilities from "~/node_common/utilities";
export const checkAuthorizationInternal = async (req, res) => {
const id = Utilities.getIdFromCookie(req);
if (!id) {
return res.status(401).send({ decorator: "SERVER_NOT_AUTHENTICATED", error: true });
}
const user = await Data.getUserById({
id,
});
if (!user) {
return res.status(404).send({
decorator: "SERVER_USER_NOT_FOUND",
error: true,
});
}
if (user.error) {
return res.status(500).send({
decorator: "SERVER_USER_NOT_FOUND",
error: true,
});
}
return { id, user };
};
export const checkAuthorizationExternal = async (req, res) => {
if (Strings.isEmpty(req.headers.authorization)) {
return res.status(404).send({
decorator: "NO_API_KEY_PROVIDED",
error: true,
});
}
const parsed = Strings.getKey(req.headers.authorization);
const key = await Data.getAPIKeyByKey({
key: parsed,
});
if (!key) {
return res.status(403).send({
decorator: "NO_MATCHING_API_KEY_FOUND",
error: true,
});
}
if (key.error) {
return res.status(500).send({
decorator: "ERROR_WHILE_VERIFYING_API_KEY",
error: true,
});
}
const user = await Data.getUserById({
id: key.ownerId,
});
if (!user) {
return res.status(404).send({
decorator: "API_KEY_OWNER_NOT_FOUND",
error: true,
});
}
if (user.error) {
return res.status(500).send({
decorator: "ERROR_WHILE_LOCATING_API_KEY_OWNER",
error: true,
});
}
return { id, user };
};

View File

@ -0,0 +1,94 @@
import * as Utilities from "~/node_common/utilities";
import * as Data from "~/node_common/data";
import * as Strings from "~/common/strings";
import * as ViewerManager from "~/node_common/managers/viewer";
import * as SearchManager from "~/node_common/managers/search";
import * as Monitor from "~/node_common/monitor";
export default async (req, res) => {
if (Strings.isEmpty(req.headers.authorization)) {
return res.status(404).send({
decorator: "NO_API_KEY_PROVIDED",
error: true,
});
}
const parsed = Strings.getKey(req.headers.authorization);
const key = await Data.getAPIKeyByKey({
key: parsed,
});
if (!key) {
return res.status(403).send({
decorator: "NO_MATCHING_API_KEY_FOUND",
error: true,
});
}
if (key.error) {
return res.status(500).send({
decorator: "ERROR_WHILE_VERIFYING_API_KEY",
error: true,
});
}
const user = await Data.getUserById({
id: key.ownerId,
});
if (!user) {
return res.status(404).send({
decorator: "API_KEY_OWNER_NOT_FOUND",
error: true,
});
}
if (user.error) {
return res.status(500).send({
decorator: "ERROR_WHILE_LOCATING_API_KEY_OWNER",
error: true,
});
}
if (!req.body?.data?.name) {
return res.status(500).send({
decorator: "MUST_PROVIDE_DATA",
error: true,
});
}
const slatename = Strings.createSlug(req.body.data.name);
const existingSlate = await Data.getSlateByName({
slatename,
ownerId: user.id,
});
if (existingSlate) {
return res.status(500).send({ decorator: "EXISTING_SLATE_NAME", error: true });
}
const slate = await Data.createSlate({
ownerId: id,
slatename: Strings.createSlug(req.body.data.name),
isPublic: req.body.data.isPublic,
data: {
name: req.body.data.name,
body: req.body.data.body,
tags: req.body.data.tags,
},
});
if (!slate || slate.error) {
return res.status(500).send({ decorator: "CREATE_COLLECTION_FAILED", error: true });
}
ViewerManager.hydratePartial(id, { slates: true });
SearchManager.updateSlate(slate, "ADD");
Monitor.createSlate({ user, slate });
return res.status(200).send({ decorator: "CREATE_COLLECTION", slate });
};

View File

@ -61,17 +61,17 @@ export default async (req, res) => {
if (!slate || slate.error) {
slate = null;
decorator = "SERVER_CREATE_LINK_SLATE_NOT_FOUND";
decorator = "SLATE_NOT_FOUND";
}
}
let urls;
if (req.body.data.url) {
if (req.body?.data?.url) {
urls = [req.body.data.url];
} else if (req.body.data.urls) {
} else if (req.body?.data?.urls) {
urls = req.body.data.urls;
} else {
return res.status(400).send({ decorator: "SERVER_CREATE_LINK_NO_LINK_PROVIDED", error: true });
return res.status(400).send({ decorator: "NO_LINK_PROVIDED", error: true });
}
let files = [];
@ -86,7 +86,7 @@ export default async (req, res) => {
});
if (!filteredFiles?.length) {
return res.status(400).send({ decorator: "SERVER_CREATE_LINK_DUPLICATE", error: true });
return res.status(200).send({ decorator: "LINK_DUPLICATE", data: duplicateFiles });
}
files = [];
@ -197,6 +197,6 @@ export default async (req, res) => {
return res.status(200).send({
decorator,
data: { added, skipped: files.length - added },
data: filesToAddToSlate,
});
};

View File

@ -43,7 +43,7 @@ export default async (req, res) => {
return res.status(500).send({ decorator: "ERROR_WHILE_LOCATING_API_KEY_OWNER", error: true });
}
let slateId = req.body.data ? req.body.data.id : null;
let slateId = req.body?.data?.id;
let slate;
if (Strings.isEmpty(slateId)) {
@ -73,5 +73,5 @@ export default async (req, res) => {
});
}
return res.status(200).send({ decorator: "V2_GET_COLLECTION", collection: slate });
return res.status(200).send({ decorator: "GET_COLLECTION", collection: slate });
};

View File

@ -43,7 +43,7 @@ export default async (req, res) => {
return res.status(500).send({ decorator: "ERROR_WHILE_LOCATING_API_KEY_OWNER", error: true });
}
let userId = req.body.data ? req.body.data.id : null;
let userId = req.body?.data?.id;
if (Strings.isEmpty(userId)) {
return res.status(400).send({ decorator: "NO_USER_ID_PROVIDED", error: true });
@ -70,5 +70,5 @@ export default async (req, res) => {
});
}
return res.status(200).send({ decorator: "V2_GET_USER", user: targetUser });
return res.status(200).send({ decorator: "GET_USER", user: targetUser });
};

View File

@ -72,5 +72,5 @@ export default async (req, res) => {
return each;
});
return res.status(200).send({ decorator: "V2_GET", user, collections: slates });
return res.status(200).send({ decorator: "GET", user, collections: slates });
};

View File

@ -50,9 +50,9 @@ export default async (req, res) => {
});
}
if (!req.body.data) {
if (!req.body?.data?.id) {
return res.status(500).send({
decorator: "V2_UPDATE_COLLECTION_MUST_PROVIDE_DATA",
decorator: "UPDATE_COLLECTION_MUST_PROVIDE_DATA",
error: true,
});
}
@ -166,5 +166,5 @@ export default async (req, res) => {
ViewerManager.hydratePartial(user.id, { slates: true });
return res.status(200).send({ decorator: "V2_UPDATE_COLLECTION", collection: updatedSlate });
return res.status(200).send({ decorator: "UPDATE_COLLECTION", collection: updatedSlate });
};

View File

@ -49,7 +49,7 @@ export default async (req, res) => {
});
}
if (!req.body.data?.id) {
if (!req.body?.data?.id) {
return res.status(500).send({ decorator: "NO_FILE_ID_PROVIDED", error: true });
}
@ -105,7 +105,7 @@ export default async (req, res) => {
ViewerManager.hydratePartial(user.id, { library: true, slates: true });
return res.status(200).send({
decorator: "V2_UPDATE_FILE",
decorator: "UPDATE_FILE",
file: response,
});
};

View File

@ -26,6 +26,7 @@ import APIDocsUpdateSlateV2 from "~/components/api-docs/v2/update-slate.js";
import APIDocsUpdateFileV2 from "~/components/api-docs/v2/update-file.js";
import APIDocsUploadToSlateV2 from "~/components/api-docs/v2/upload.js";
import APIDocsCreateLinkV2 from "~/components/api-docs/v2/create-link.js";
import APIDocsCreateCollectionV2 from "~/components/api-docs/v2/create-collection.js";
import WebsitePrototypeWrapper from "~/components/core/WebsitePrototypeWrapper";
const STYLES_API_KEY = css`
@ -312,7 +313,9 @@ export default class SceneSettingsDeveloper extends React.Component {
{tab === "v2" ? (
<>
<System.H2 style={{ marginTop: 64 }}>Read operations</System.H2>
<APIDocsGetV2
style={{ marginTop: 24 }}
language={lang}
APIKey={APIKey}
onLanguageChange={this._handleChangeLanguage}
@ -329,7 +332,10 @@ export default class SceneSettingsDeveloper extends React.Component {
userId={userId}
onLanguageChange={this._handleChangeLanguage}
/>
<System.H2 style={{ marginTop: 64 }}>Update operations</System.H2>
<APIDocsUpdateSlateV2
style={{ marginTop: 24 }}
language={lang}
APIKey={APIKey}
slateId={slateId}
@ -341,6 +347,15 @@ export default class SceneSettingsDeveloper extends React.Component {
slateId={slateId}
onLanguageChange={this._handleChangeLanguage}
/>
<System.H2 style={{ marginTop: 64 }}>Create operations</System.H2>
<APIDocsCreateCollectionV2
style={{ marginTop: 24 }}
language={lang}
APIKey={APIKey}
slateId={slateId}
onLanguageChange={this._handleChangeLanguage}
/>
<APIDocsUploadToSlateV2
language={lang}
APIKey={APIKey}
@ -356,7 +371,9 @@ export default class SceneSettingsDeveloper extends React.Component {
</>
) : (
<>
<System.H2 style={{ marginTop: 64 }}>Read operations</System.H2>
<APIDocsGetV1
style={{ marginTop: 24 }}
language={lang}
APIKey={APIKey}
onLanguageChange={this._handleChangeLanguage}
@ -367,13 +384,17 @@ export default class SceneSettingsDeveloper extends React.Component {
slateId={slateId}
onLanguageChange={this._handleChangeLanguage}
/>
<System.H2 style={{ marginTop: 64 }}>Update operations</System.H2>
<APIDocsUpdateSlateV1
style={{ marginTop: 24 }}
language={lang}
APIKey={APIKey}
slateId={slateId}
onLanguageChange={this._handleChangeLanguage}
/>
<System.H2 style={{ marginTop: 64 }}>Create operations</System.H2>
<APIDocsUploadToSlateV1
style={{ marginTop: 24 }}
language={lang}
APIKey={APIKey}
slateId={slateId}