slate/components/api-docs/v2/upload.js

106 lines
2.8 KiB
JavaScript
Raw Normal View History

2021-04-21 03:14:43 +03:00
import * as React from "react";
import * as System from "~/components/system";
import CodeBlock from "~/components/system/CodeBlock";
const EXAMPLE_CODE_JS = (key) => `const url = 'https://uploads.slate.host/api/v2/public';
2021-04-21 03:14:43 +03:00
let file = e.target.files[0];
let data = new FormData();
data.append("data", file);
const response = await fetch(url, {
method: 'POST',
headers: {
Authorization: '${key}', // API key
2021-04-21 03:14:43 +03:00
},
body: data
});`;
const EXAMPLE_CODE_PY = (key) => `import requests
url = "https://uploads.slate.host/api/v2/public"
2021-04-21 03:14:43 +03:00
files = {
"file": open("example-file.txt", "rb")
}
headers = {
"Authorization": "${key}" # API key
2021-04-21 03:14:43 +03:00
}
r = requests.post(url, headers=headers, files=files)`;
const SLATE_EXAMPLE_CODE_JS = (
key,
slateId
) => `const url = 'https://uploads.slate.host/api/v2/public/${slateId}'; // collection ID
2021-04-21 03:14:43 +03:00
let file = e.target.files[0];
let data = new FormData();
data.append("data", file);
const response = await fetch(url, {
method: 'POST',
headers: {
Authorization: '${key}', // API key
2021-04-21 03:14:43 +03:00
},
body: data
});
const json = await response.json();`;
2021-04-21 03:14:43 +03:00
const SLATE_EXAMPLE_CODE_PY = (key, slateId) => `import requests
url = "https://uploads.slate.host/api/v2/public/${slateId}" # collection ID
2021-04-21 03:14:43 +03:00
files = {
"file": open("example-file.txt", "rb")
}
headers = {
"Authorization": "${key}" # API key
2021-04-21 03:14:43 +03:00
}
r = requests.post(url, headers=headers, files=files)`;
export default class APIDocsUploadToSlate extends React.Component {
render() {
let language = this.props.language;
let key = this.props.APIKey;
let slateId = this.props.slateId;
let uploadCode = {
javascript: EXAMPLE_CODE_JS(key),
python: EXAMPLE_CODE_PY(key),
};
let slateUploadCode = {
javascript: SLATE_EXAMPLE_CODE_JS(key, slateId),
python: SLATE_EXAMPLE_CODE_PY(key, slateId),
};
return (
2021-08-02 05:56:55 +03:00
<div css={this.props.cssValue} style={this.props.style}>
2021-04-21 03:14:43 +03:00
<System.DescriptionGroup
2021-08-02 05:56:55 +03:00
style={{ maxWidth: 640 }}
label="Upload File"
2021-04-21 03:14:43 +03:00
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."
2021-04-21 03:14:43 +03:00
}
/>
<CodeBlock
children={uploadCode}
style={{ maxWidth: "820px" }}
language={language}
title="Upload file"
2021-04-21 03:14:43 +03:00
multiLang="true"
onLanguageChange={this.props.onLanguageChange}
/>
<br />
<CodeBlock
children={slateUploadCode}
style={{ maxWidth: "820px" }}
language={language}
title="Upload file to collection"
2021-04-21 03:14:43 +03:00
multiLang="true"
onLanguageChange={this.props.onLanguageChange}
/>
2021-08-02 05:56:55 +03:00
</div>
2021-04-21 03:14:43 +03:00
);
}
}