UNPKG

3.27 kBJavaScriptView Raw
1const request = require('request-promise'),
2 version = require('../package.json').version,
3 logger = require('./logger'),
4 ServerError = require('./ServerError');
5
6class Gateway {
7 constructor({ url, token, email }) {
8 this.url = url;
9 this.api_url = `${url}/api/marketplace_builder`;
10 this.private_api_url = `${url}/api/private`;
11
12 const headers = {
13 Authorization: `Token ${token}`,
14 'User-Agent': `marketplace-kit/${version}`,
15 From: email
16 };
17
18 this.authorizedRequest = request.defaults({ headers });
19
20 logger.Debug(`Request headers: ${JSON.stringify(headers, null, 2)}`);
21 }
22
23 apiRequest({ method = 'GET', uri, formData, json = true }) {
24 logger.Debug(`[${method}] ${uri}`);
25 return this.authorizedRequest({
26 method,
27 uri,
28 formData,
29 json: json
30 })
31 .catch({ statusCode: 500 }, ServerError.internal)
32 .catch({ statusCode: 401 }, ServerError.unauthorized);
33 }
34
35 dataExportStart(exportInternalIds) {
36 const formData = { 'export_internal': exportInternalIds };
37 return this.apiRequest({ uri: `${this.api_url}/exports`, method: 'POST', formData });
38 }
39
40 dataExportStatus(exportId) {
41 return this.apiRequest({ uri: `${this.api_url}/exports/${exportId}` });
42 }
43
44 dataImportStart(formData) {
45 return this.apiRequest({ uri: `${this.api_url}/imports`, method: 'POST', formData });
46 }
47
48 dataImportStatus(importId) {
49 return this.apiRequest({ uri: `${this.api_url}/imports/${importId}` });
50 }
51
52 dataUpdate(formData) {
53 return this.apiRequest({ uri: `${this.api_url}/data_updates`, method: 'POST', formData });
54 }
55
56 dataClean(confirmation) {
57 return this.apiRequest({ uri: `${this.api_url}/data_clean`, method: 'POST', json: { confirmation: confirmation } });
58 }
59
60 ping() {
61 return this.apiRequest({ uri: `${this.api_url}/logs` });
62 }
63
64 logs(json) {
65 return this.apiRequest({ uri: `${this.api_url}/logs`, json });
66 }
67
68 getInstance() {
69 return this.apiRequest({ method: 'GET', uri: `${this.api_url}/instance` });
70 }
71
72 getStatus(id) {
73 return this.apiRequest({ uri: `${this.api_url}/marketplace_releases/${id}` });
74 }
75
76 graph(json) {
77 return this.apiRequest({ method: 'POST', uri: `${this.url}/api/graph`, json });
78 }
79
80 listModules() {
81 return this.apiRequest({ uri: `${this.private_api_url}/modules` });
82 }
83
84 removeModule(formData) {
85 return this.apiRequest({ method: 'DELETE', uri: `${this.private_api_url}/modules`, formData });
86 }
87
88 listMigrations() {
89 return this.apiRequest({ uri: `${this.api_url}/migrations` });
90 }
91
92 generateMigration(formData) {
93 return this.apiRequest({ method: 'POST', uri: `${this.api_url}/migrations`, formData });
94 }
95
96 runMigration(formData) {
97 return this.apiRequest({ method: 'POST', uri: `${this.api_url}/migrations/run`, formData });
98 }
99
100 sendManifest(manifest) {
101 return this.apiRequest({ method: 'POST', uri: `${this.api_url}/assets_manifest`, json: { manifest: manifest } });
102 }
103
104 sync(formData) {
105 return this.apiRequest({ method: 'PUT', uri: `${this.api_url}/marketplace_releases/sync`, formData });
106 }
107
108 push(formData) {
109 return this.apiRequest({ method: 'POST', uri: `${this.api_url}/marketplace_releases`, formData });
110 }
111
112}
113
114module.exports = Gateway;