This repository has been archived by the owner on Jun 6, 2023. It is now read-only.
/
client.js
70 lines (60 loc) · 1.89 KB
/
client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
'use strict';
var configInfo = require('./config');
var request = require('./request');
// for credential caching
var projectConfig = require('./projectConfig');
projectConfig.getProject();
var processEnv = (typeof process !== 'undefined' && process.env) || {};
var nodeEnv = processEnv.NODE_ENV;
var config;
if (nodeEnv in configInfo) config = configInfo[nodeEnv];
else config = configInfo['production'];
var envConfigHost = processEnv.PAPERSPACE_CONFIG_HOST;
var envConfigLogHost = processEnv.PAPERSPACE_CONFIG_LOG_HOST;
if (envConfigHost) config.host = envConfigHost;
if (envConfigLogHost) config.logHost = envConfigLogHost;
// Simple wrapper over the request function that inserts a 'host' parameter
// as the first argument, since most use cases are always going to use the
// same host as provided via the baked-in config.
function client(method, path, inputs, headers, file, cb) {
var body = null;
var query = {};
var options = {};
// It's assumed these will be provided, but just in case...
if (!headers) headers = {};
// Send input params as the query string if this is a GET, otherwise send
// with the HTTP request body
switch (method.toLowerCase()) {
case 'get':
query = inputs;
break;
case 'post':
if (file) {
query = inputs;
break;
}
default: // eslint-disable-line no-fallthrough
// Don't leave the access token on the inputs object since the jwt
// really belongs on the query string (at least, per the way the interface
// to the Paperspace API is currently set up).
query.access_token = inputs.access_token;
delete inputs.access_token;
body = inputs;
break;
}
// The rubber finally meets the road with 'request', which initiates
// the request via Superagent
return request(
path === '/jobs/logs' ? config.logHost : config.host,
method,
path,
body,
query,
headers,
options,
file,
cb,
inputs.debug
);
}
module.exports = client;