@@ -15,8 +15,12 @@ function signOpts(opts, user) {
1515 opts . headers [ 'X-Requested-With' ] = 'XMLHttpRequest' ;
1616}
1717
18- function makeOpts ( url ) {
19- var opts = { url : url , headers : { } , _expectedStatus : 200 } ;
18+ function makeOpts ( url , expectedStatus ) {
19+ var opts = { } ;
20+ opts . url = url ;
21+ opts . headers = { } ;
22+ opts . expectedStatus = expectedStatus || 200 ;
23+
2024 var core = require ( './core' ) ;
2125 if ( core . isLogin ( ) ) signOpts ( opts , core . getUser ( ) ) ;
2226 return opts ;
@@ -25,49 +29,45 @@ function makeOpts(url) {
2529function checkError ( e , resp , expectedStatus , msg ) {
2630 if ( e ) return e ;
2731
28- if ( resp && resp . statusCode !== expectedStatus ) {
29- if ( resp . statusCode === 403 ) {
32+ var code = resp . statusCode ;
33+ if ( resp && code !== expectedStatus ) {
34+ if ( code === 403 ) {
3035 msg = msg || 'session expired, please login again' ;
31-
3236 var core = require ( './core' ) ;
3337 core . logout ( ) ;
3438 }
35-
36- return {
37- msg : msg || 'http error' ,
38- statusCode : resp . statusCode
39- } ;
39+ e = { msg : msg || 'http error' , statusCode : code } ;
4040 }
41+ return e ;
4142}
4243
4344// leetcode.com is limiting one session alive in the same time,
4445// which means once you login on web, your cli session will get
4546// expired immediately. In that case we will try to re-login in
4647// the backend to give a seamless user experience.
4748function requestWithReLogin ( opts , cb ) {
48- if ( ! config . AUTO_LOGIN )
49- return request ( opts , cb ) ;
50-
51- var core = require ( './core' ) ;
52- var user = core . getUser ( ) ;
53-
5449 request ( opts , function ( e , resp , body ) {
55- e = checkError ( e , resp , opts . _expectedStatus ) ;
50+ e = checkError ( e , resp , opts . expectedStatus ) ;
5651
5752 // not 403: transparently pass down
58- if ( ! e || e . statusCode !== 403 )
53+ if ( ! config . AUTO_LOGIN || ! e || e . statusCode !== 403 )
5954 return cb ( e , resp , body ) ;
6055
6156 // if 403: try re-login
6257 log . debug ( 'session expired, auto re-login...' ) ;
6358
59+ var core = require ( './core' ) ;
60+ var user = core . getUser ( ) ;
6461 core . login ( user , function ( e2 , user ) {
6562 if ( e2 ) return cb ( e , resp , body ) ;
6663
6764 log . debug ( 'login successfully, cont\'d...' ) ;
6865 signOpts ( opts , user ) ;
6966
70- request ( opts , cb ) ;
67+ request ( opts , function ( e , resp , body ) {
68+ e = checkError ( e , resp , opts . expectedStatus ) ;
69+ return cb ( e , resp , body ) ;
70+ } ) ;
7171 } ) ;
7272 } ) ;
7373}
@@ -78,7 +78,6 @@ leetcodeClient.getProblems = function(cb) {
7878 var opts = makeOpts ( config . URL_PROBLEMS ) ;
7979
8080 requestWithReLogin ( opts , function ( e , resp , body ) {
81- e = checkError ( e , resp , 200 ) ;
8281 if ( e ) return cb ( e ) ;
8382
8483 var json = JSON . parse ( body ) ;
@@ -120,8 +119,7 @@ var aceCtrl = {
120119leetcodeClient . getProblem = function ( problem , cb ) {
121120 var opts = makeOpts ( ) ;
122121 opts . url = problem . link ;
123- request ( opts , function ( e , resp , body ) {
124- e = checkError ( e , resp , 200 ) ;
122+ requestWithReLogin ( opts , function ( e , resp , body ) {
125123 // FIXME: if session expired, this will still return 200
126124 if ( e ) return cb ( e ) ;
127125
@@ -154,8 +152,7 @@ leetcodeClient.getSubmissions = function(problem, cb) {
154152 opts . url = config . URL_SUBMISSIONS . replace ( '$key' , problem . key ) ;
155153 opts . headers . Referer = config . URL_PROBLEM . replace ( '$id' , problem . key ) ;
156154
157- request ( opts , function ( e , resp , body ) {
158- e = checkError ( e , resp , 200 ) ;
155+ requestWithReLogin ( opts , function ( e , resp , body ) {
159156 // FIXME: if session expired, this will still return 200
160157 if ( e ) return cb ( e ) ;
161158
@@ -173,8 +170,7 @@ leetcodeClient.getSubmission = function(submission, cb) {
173170 var opts = makeOpts ( ) ;
174171 opts . url = config . URL_SUBMISSION . replace ( '$id' , submission . id ) ;
175172
176- request ( opts , function ( e , resp , body ) {
177- e = checkError ( e , resp , 200 ) ;
173+ requestWithReLogin ( opts , function ( e , resp , body ) {
178174 if ( e ) return cb ( e ) ;
179175
180176 var re = body . match ( / s u b m i s s i o n C o d e : \s ( ' [ ^ ' ] * ' ) / ) ;
@@ -227,7 +223,6 @@ function verifyResult(opts, jobs, results, cb) {
227223 opts . url = config . URL_VERIFY . replace ( '$id' , jobs [ 0 ] . id ) ;
228224
229225 requestWithReLogin ( opts , function ( e , resp , body ) {
230- e = checkError ( e , resp , 200 ) ;
231226 if ( e ) return cb ( e ) ;
232227
233228 var result = JSON . parse ( body ) ;
@@ -257,7 +252,6 @@ function runCode(opts, problem, cb) {
257252 } ) ;
258253
259254 requestWithReLogin ( opts , function ( e , resp , body ) {
260- e = checkError ( e , resp , 200 ) ;
261255 if ( e ) return cb ( e ) ;
262256
263257 if ( body . error ) {
@@ -320,7 +314,6 @@ leetcodeClient.starProblem = function(problem, starred, cb) {
320314 opts . body = { 'qid' : problem . id } ;
321315
322316 requestWithReLogin ( opts , function ( e , resp , body ) {
323- e = checkError ( e , resp , 200 ) ;
324317 if ( e ) return cb ( e ) ;
325318
326319 cb ( null , body . is_favor ) ;
0 commit comments