11//let baseURL = "http://svcs.ebay.com/services/search/FindingService/v1";
2- let makeRequest = require ( './request' ) ;
2+ let { getRequest , makeRequest, base64Encode } = require ( './request' ) ;
33let urlObject = require ( './buildURL' ) ;
44
55function Ebay ( options ) {
66 console . log ( options ) ;
77
88 if ( ! options ) throw new Error ( "Options is missing, please provide the input" ) ;
99 if ( ! options . clientID ) throw Error ( "Client ID is Missing\ncheck documentation to get Client ID http://developer.ebay.com/DevZone/account/" ) ;
10+ if ( ! ( this instanceof Ebay ) ) return new Ebay ( options ) ;
1011 this . options = options ;
1112 this . options . globalID = options . countryCode || "EBAY-US" ;
1213 this . options . keyword = "iphone" ;
@@ -22,7 +23,7 @@ Ebay.prototype = {
2223 this . options . param = "keywords" ;
2324 let url = urlObject . buildSearchUrl ( this . options ) ;
2425 console . log ( url ) ;
25- return makeRequest ( url ) . then ( ( data ) => {
26+ return getRequest ( url ) . then ( ( data ) => {
2627 let result = JSON . parse ( data ) ;
2728 return result [ "findItemsByKeywordsResponse" ] ;
2829
@@ -39,7 +40,7 @@ Ebay.prototype = {
3940 this . options . param = "categoryId" ;
4041 let url = urlObject . buildSearchUrl ( this . options ) ;
4142 console . log ( url ) ;
42- return makeRequest ( url ) . then ( ( data ) => {
43+ return getRequest ( url ) . then ( ( data ) => {
4344 let result = JSON . parse ( data ) ;
4445 return result [ "findItemsByCategoryResponse" ] ;
4546
@@ -56,7 +57,7 @@ Ebay.prototype = {
5657 this . options . param = "CategoryID" ;
5758 let url = urlObject . buildShoppingUrl ( this . options ) ;
5859 console . log ( url ) ;
59- return makeRequest ( url ) . then ( ( data ) => {
60+ return getRequest ( url ) . then ( ( data ) => {
6061 let result = JSON . parse ( data ) ;
6162 return result ;
6263 } , ( error ) => {
@@ -67,7 +68,7 @@ Ebay.prototype = {
6768 getVersion : function ( ) {
6869 this . options . operationName = "getVersion" ;
6970 let url = urlObject . buildSearchUrl ( this . options ) ;
70- return makeRequest ( url ) . then ( ( data ) => {
71+ return getRequest ( url ) . then ( ( data ) => {
7172 let result = JSON . parse ( data ) ;
7273 return result [ "getVersionResponse" ] [ 0 ] ;
7374 } , ( error ) => {
@@ -83,13 +84,49 @@ Ebay.prototype = {
8384 this . options . includeSelector = this . options . details ? "Details" : null ;
8485 let url = urlObject . buildShoppingUrl ( this . options ) ;
8586 console . log ( url ) ;
86- return makeRequest ( url ) . then ( ( data ) => {
87+ return getRequest ( url ) . then ( ( data ) => {
8788 let result = JSON . parse ( data ) ;
8889 console . log ( result ) ;
8990 return result ;
9091 } , ( error ) => {
9192 console . log ( error ) ;
9293 } )
94+ } ,
95+
96+ getItem : function ( itemId ) {
97+ console . log ( this . options ) ;
98+ if ( ! itemId ) throw new Error ( "Item Id is required" ) ;
99+ if ( ! this . options . access_token ) throw new Error ( "Missing Access token, Generate access token" ) ;
100+ const auth = "Bearer " + this . options . access_token ;
101+ const id = encodeURIComponent ( itemId ) ;
102+ return makeRequest ( 'api.ebay.com' , `/buy/browse/v1/item/${ id } ` , 'GET' , this . options . body , auth ) . then ( ( result ) => {
103+ console . log ( "Success" ) ;
104+ let resultJSON = JSON . parse ( result ) ;
105+ //this.setAccessToken(resultJSON);
106+ return resultJSON ;
107+ } ) ;
108+ } ,
109+
110+ setAccessToken : function ( token ) {
111+ console . log ( "inside access tokeeeeee" + token ) ;
112+ this . options . access_token = token ;
113+ } ,
114+
115+ getAccessToken : function ( ) {
116+ if ( ! this . options . clientID ) throw new Error ( "Missing Client ID" ) ;
117+ if ( ! this . options . clientSecret ) throw new Error ( "Missing Client Secret or Cert Id" ) ;
118+ if ( ! this . options . body ) throw new Error ( "Missing Body, required Grant type" ) ;
119+ const encodedStr = base64Encode ( this . options . clientID + ":" + this . options . clientSecret ) ;
120+ let self = this ;
121+ console . log ( this . options . body ) ;
122+ const auth = "Basic " + encodedStr ;
123+ return makeRequest ( 'api.ebay.com' , '/identity/v1/oauth2/token' , 'POST' , this . options . body , auth ) . then ( ( result ) => {
124+ console . log ( "Successssssssss" ) ;
125+ let resultJSON = JSON . parse ( result ) ;
126+ // console.log(this);
127+ self . setAccessToken ( resultJSON . access_token ) ;
128+ return resultJSON ;
129+ } ) ;
93130 }
94131
95132} ;
0 commit comments