Skip to content

Commit dc36ad5

Browse files
authored
Added item draft api (#130)
* v2.7.7 * 2.8.7 * added deprecation message and update readme * fix item filter for diff sites * revert commit * fix lint * added item draftapi
1 parent 5c86254 commit dc36ad5

File tree

5 files changed

+52
-27
lines changed

5 files changed

+52
-27
lines changed

src/constants.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
'use strict';
22

3+
const body = {
4+
grant_type: 'client_credentials',
5+
scope: 'https://api.ebay.com/oauth/api_scope'
6+
};
7+
38
module.exports = {
49
PROD_OAUTHENVIRONMENT_WEBENDPOINT: 'https://auth.ebay.com/oauth2/authorize',
510
SANDBOX_OAUTHENVIRONMENT_WEBENDPOINT: 'https://auth.sandbox.ebay.com/oauth2/authorize',
@@ -8,5 +13,6 @@ module.exports = {
813
SANDBOX_BASE_URL: 'api.sandbox.ebay.com',
914
BASE_SVC_URL: 'svcs.ebay.com',
1015
BASE_SANDBX_SVC_URL: 'svcs.sandbox.ebay.com',
11-
MERCH_SRVC_NAME: 'MerchandisingService'
16+
MERCH_SRVC_NAME: 'MerchandisingService',
17+
DEFAULT_BODY: body
1218
};

src/credentials.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
const qs = require('querystring');
33
const { base64Encode } = require('./common-utils');
44
const { makeRequest } = require('./request');
5+
const { DEFAULT_BODY } = require('./constants');
56
const DEFAULT_API_SCOPE = 'https://api.ebay.com/oauth/api_scope';
67

78
/**
@@ -12,16 +13,20 @@ const DEFAULT_API_SCOPE = 'https://api.ebay.com/oauth/api_scope';
1213
const getAccessToken = function () {
1314
if (!this.options.clientID) throw new Error('Missing Client ID');
1415
if (!this.options.clientSecret) throw new Error('Missing Client Secret or Cert Id');
15-
if (!this.options.body) throw new Error('Missing Body, required Grant type');
16-
const scopesParam = this.options.body.scopes
17-
? Array.isArray(this.options.body.scopes)
18-
? this.options.body.scopes.join('%20')
19-
: this.options.body.scopes
20-
: DEFAULT_API_SCOPE;
21-
this.options.data = qs.stringify({
22-
grant_type: 'client_credentials',
23-
scope: scopesParam
24-
});
16+
if (!this.options.body) {
17+
this.options.body = qs.stringify(DEFAULT_BODY);
18+
}
19+
else {
20+
const scopesParam = this.options.body.scopes
21+
? Array.isArray(this.options.body.scopes)
22+
? this.options.body.scopes.join('%20')
23+
: this.options.body.scopes
24+
: DEFAULT_API_SCOPE;
25+
this.options.body = qs.stringify({
26+
grant_type: 'client_credentials',
27+
scope: scopesParam
28+
});
29+
}
2530
this.options.contentType = 'application/x-www-form-urlencoded';
2631
const self = this;
2732
const encodedStr = base64Encode(this.options.clientID + ':' + this.options.clientSecret);

src/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
'use strict';
2+
const qs = require('querystring');
23
const ebayBuyApi = require('./buy-api');
34
const shoppingApi = require('./shopping');
45
const taxonomyApi = require('./taxonomy-api');
56
const ebayFindingApi = require('./finding');
67
const dealsApi = require('./deals');
78
const commonUtils = require('./common-utils');
89
const merchandisingApi = require('./merchandising');
10+
const itemApi = require('./item');
911
const {
1012
getAccessToken,
1113
getUserAuthorizationUrl,
@@ -70,7 +72,8 @@ Ebay.prototype = {
7072
...ebayBuyApi,
7173
...taxonomyApi,
7274
...ebayFindingApi,
73-
...dealsApi
75+
...dealsApi,
76+
...itemApi
7477
};
7578

7679
module.exports = Ebay;

src/item.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
const { makeRequest } = require('./request');
4+
5+
const createItemDraft = function (itemData) {
6+
if (!itemData) return new Error('INVALID_REQUEST_PARMS -> Required item data to create item draft');
7+
if (!this.options.userAccessToken) return new Error('INVALID_REQUEST_PARMS -> Authorization user token is required');
8+
const auth = 'Bearer ' + this.options.userAccessToken;
9+
this.options.contentType = 'application/json';
10+
this.options.body = JSON.stringify(itemData);
11+
return makeRequest(this.options, '/sell/listing/v1_beta/item_draft/', 'POST', auth).then((result) => {
12+
const resultJSON = JSON.parse(result);
13+
return resultJSON;
14+
}).catch((error) => {
15+
return new Error(error);
16+
});
17+
};
18+
19+
module.exports = {
20+
createItemDraft
21+
};

src/request.js

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict';
22
let httpRequest = require('https');
3-
const qs = require('querystring');
43

54
const getRequest = (url) => {
65
if (url.includes('http://')) httpRequest = require('http');
@@ -25,22 +24,17 @@ const getRequest = (url) => {
2524
}));
2625
};
2726

28-
const makeRequest = function postRequest(self, endpoint, methodName, token) {
29-
let dataString = '';
30-
if (self.data) {
31-
dataString = self.data;
32-
}
33-
else {
34-
methodName === 'POST' ? dataString = qs.stringify(self.body) : '';
35-
}
27+
function makeRequest(self, endpoint, methodName, token) {
28+
const dataString = self.body || '';
3629
const options = {
3730
'hostname': self.baseUrl,
3831
'path': endpoint,
3932
'method': methodName || 'GET',
4033
'headers': {
4134
'content-type': self.contentType ? self.contentType : 'application/json',
42-
'authorization': token,
35+
'Authorization': token,
4336
'cache-control': 'no-cache',
37+
'X-EBAY-C-MARKETPLACE-ID': 'EBAY_US',
4438
...self.headers
4539
}
4640
};
@@ -50,23 +44,19 @@ const makeRequest = function postRequest(self, endpoint, methodName, token) {
5044
let body = '';
5145
res.on('data', (data) => {
5246
body += data;
53-
//console.log(body);
5447
});
5548
res.on('end', () => {
5649
resolve(body);
57-
5850
});
5951
res.on('error', (error) => {
6052
reject(error);
61-
6253
});
6354
});
6455
//console.log('request ' + dataString);
6556
if (methodName === 'POST') req.write(dataString);
6657
req.end();
6758
}));
68-
};
69-
59+
}
7060

7161
const base64Encode = (encodeData) => {
7262
const buff = Buffer.from(encodeData);

0 commit comments

Comments
 (0)