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

105 lines
2.8 KiB
JavaScript
Raw Normal View History

2021-03-24 07:10:26 +03:00
import * as React from "react";
import * as System from "~/components/system";
import CodeBlock from "~/components/system/CodeBlock";
2021-03-24 07:10:26 +03:00
2021-04-21 03:14:43 +03:00
const EXAMPLE_CODE_JS = (key) => `const url = 'https://uploads.slate.host/api/public';
let file = e.target.files[0];
let data = new FormData();
data.append("data", file);
const response = await fetch(url, {
method: 'POST',
headers: {
Authorization: 'Basic ${key}', // API key
},
body: data
});`;
const EXAMPLE_CODE_PY = (key) => `import requests
url = "https://uploads.slate.host/api/public"
files = {
"file": open("example-file.txt", "rb")
}
headers = {
"Authorization": "Basic ${key}" # API key
}
r = requests.post(url, headers=headers, files=files)`;
const SLATE_EXAMPLE_CODE_JS = (
2021-03-24 07:10:26 +03:00
key,
slateId
2021-04-23 07:18:02 +03:00
) => `const url = 'https://uploads.slate.host/api/public/${slateId}'; // collection ID
2021-03-24 07:10:26 +03:00
let file = e.target.files[0];
let data = new FormData();
data.append("data", file);
const response = await fetch(url, {
method: 'POST',
headers: {
2021-04-21 03:14:43 +03:00
Authorization: 'Basic ${key}', // API key
2021-03-24 07:10:26 +03:00
},
body: data
2021-03-30 04:35:06 +03:00
});`;
2021-03-24 07:10:26 +03:00
2021-04-21 03:14:43 +03:00
const SLATE_EXAMPLE_CODE_PY = (key, slateId) => `import requests
2021-04-05 23:21:23 +03:00
url = "https://uploads.slate.host/api/public/${slateId}" # collection ID. Omit this to just upload without adding to a specific collection
2021-04-21 03:14:43 +03:00
files = {
"file": open("example-file.txt", "rb")
}
headers = {
"Authorization": "Basic ${key}" # API key
}
2021-03-24 07:10:26 +03:00
2021-03-30 04:19:36 +03:00
r = requests.post(url, headers=headers, files=files)`;
2021-03-24 07:10:26 +03:00
2021-03-30 04:19:36 +03:00
export default class APIDocsUploadToSlate extends React.Component {
render() {
let language = this.props.language;
let key = this.props.APIKey;
let slateId = this.props.slateId;
2021-03-24 07:10:26 +03:00
2021-04-21 03:14:43 +03:00
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),
2021-03-30 04:19:36 +03:00
};
2021-03-24 07:10:26 +03:00
return (
<React.Fragment>
<System.DescriptionGroup
2021-03-30 04:19:36 +03:00
style={{ maxWidth: 640, marginTop: 64 }}
2021-04-21 03:14:43 +03:00
label="Upload"
2021-03-30 04:19:36 +03:00
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."
2021-03-30 04:19:36 +03:00
}
2021-03-24 07:10:26 +03:00
/>
<CodeBlock
2021-04-21 03:14:43 +03:00
children={uploadCode}
style={{ maxWidth: "820px" }}
language={language}
title="Upload"
multiLang="true"
onLanguageChange={this.props.onLanguageChange}
/>
<br />
<CodeBlock
children={slateUploadCode}
2021-03-30 04:19:36 +03:00
style={{ maxWidth: "820px" }}
2021-03-24 07:10:26 +03:00
language={language}
title="Upload to collection"
2021-03-30 04:19:36 +03:00
multiLang="true"
onLanguageChange={this.props.onLanguageChange}
2021-03-24 07:10:26 +03:00
/>
</React.Fragment>
);
}
}