skygragon / leetcode-cli

A cli tool to enjoy leetcode!
MIT License
3.65k stars 457 forks source link

API of switching session #108

Closed mqliang closed 6 years ago

mqliang commented 6 years ago

Hi, I am planning to write a chrome plugin to auto switch sessions when I login. Can you please share me the API of switching session, I tried to read the source code of leetcode-cli, but didn't find it.

Great thanks!

mqliang commented 6 years ago

@zhujinghui

skygragon commented 6 years ago

@mqliang see runSession() in latest lib/plugins/leetcode.js

412 function runSession(method, data, cb) {
413   const opts = makeOpts(config.sys.urls.session);
414   opts.json = true;
415   opts.method = method;
416   opts.body = data;
417
418   const spin = h.spin('Waiting session result');
419   request(opts, function(e, resp, body) {
420     spin.stop();
421     e = checkError(e, resp, 200);
422     if (e && e.statusCode === 302) e = session.errors.EXPIRED;
423
424     return e ? cb(e) : cb(null, body.sessions);
425   });
426 }
427
428 plugin.getSessions = function(cb) {
429   log.debug('running leetcode.getSessions');
430   runSession('POST', {}, cb);
431 };
432
433 plugin.activateSession = function(session, cb) {
434   log.debug('running leetcode.activateSession');
435   const data = {func: 'activate', target: session.id};
436   runSession('PUT', data, cb);
437 };