5
5
; ; Author: Wang Kai <[email protected] >
6
6
; ; Keywords: extensions, tools
7
7
; ; URL: https://github.com/kaiwk/leetcode.el
8
- ; ; Package-Requires: ((emacs "25 ") (request-deferred "0.2.0") (graphql "0.1.1") (spinner "1.7.3"))
9
- ; ; Version: 0.1.3
8
+ ; ; Package-Requires: ((emacs "26 ") (request-deferred "0.2.0") (graphql "0.1.1") (spinner "1.7.3") (aio "1.0 "))
9
+ ; ; Version: 0.1.4
10
10
11
11
; ; This program is free software; you can redistribute it and/or modify
12
12
; ; it under the terms of the GNU General Public License as published by
@@ -261,7 +261,7 @@ Such as 'Two Sum' will be converted to 'two-sum'."
261
261
(res (replace-regexp-in-string " [(),]" " " str1)))
262
262
res))
263
263
264
- (defun leetcode--problem-descr- graphql-params (operation &optional vars )
264
+ (defun leetcode--problem-graphql-params (operation &optional vars )
265
265
" Construct a GraphQL parameter.
266
266
OPERATION and VARS are LeetCode GraphQL parameters."
267
267
(list
@@ -278,7 +278,7 @@ OPERATION and VARS are LeetCode GraphQL parameters."
278
278
(codeSnippets langSlug code)))))
279
279
(if vars (cons " variables" vars))))
280
280
281
- (defun leetcode--parse -problem (title )
281
+ (aio- defun leetcode--fetch -problem (title)
282
282
" Fetch single problem.
283
283
TITLE is a problem's title.
284
284
Return a object with following attributes:
@@ -287,16 +287,20 @@ Return a object with following attributes:
287
287
:content String
288
288
:topicTags String"
289
289
(let* ((slug-title (leetcode--slugify-title title))
290
- (resp (request
291
- leetcode--api-graphql
292
- :type " POST"
293
- :headers `(, leetcode--User-Agent
294
- ,(cons " Content-Type" " application/json" ))
295
- :data (json-encode (leetcode--problem-descr-graphql-params
296
- " questionData"
297
- (list (cons " titleSlug" slug-title))))
298
- :parser 'json-read :sync t )))
299
- (alist-get 'question (alist-get 'data (request-response-data resp)))))
290
+ (url-request-method " POST" )
291
+ (url-request-extra-headers
292
+ `(, leetcode--User-Agent
293
+ ,(cons " Content-Type" " application/json" )))
294
+ (url-request-data
295
+ (json-encode (leetcode--problem-graphql-params
296
+ " questionData"
297
+ (list (cons " titleSlug" slug-title)))))
298
+ (result (aio-await (aio-url-retrieve leetcode--api-graphql))))
299
+ (if-let ((error-info (plist-get (car result) :error )))
300
+ (message " LeetCode Login ERROR: %S " error-info)
301
+ (with-current-buffer (cdr result)
302
+ (goto-char url-http-end-of-headers)
303
+ (alist-get 'question (alist-get 'data (json-read )))))))
300
304
301
305
(defun leetcode--replace-in-buffer (regex to )
302
306
" Replace string matched REGEX in `current-buffer' to TO."
@@ -662,7 +666,7 @@ following possible value:
662
666
" Generate problem link from title."
663
667
(concat leetcode--base-url " /problems/" (leetcode--slugify-title title)))
664
668
665
- (defun leetcode-show-description ()
669
+ (aio- defun leetcode-show-current-problem ()
666
670
" Show current entry problem description.
667
671
Get current entry by using `tabulated-list-get-entry' and use
668
672
`shr-render-buffer' to render problem description."
@@ -671,7 +675,7 @@ Get current entry by using `tabulated-list-get-entry' and use
671
675
(pos (aref entry 1 ))
672
676
(title (aref entry 2 ))
673
677
(difficulty (aref entry 4 ))
674
- (problem (leetcode--parse -problem title))
678
+ (problem (aio-await ( leetcode--fetch -problem title) ))
675
679
(buf-name leetcode--description-buffer-name)
676
680
(html-margin " " ))
677
681
(let-alist problem
@@ -810,7 +814,7 @@ for current problem."
810
814
(let ((map (make-sparse-keymap )))
811
815
(prog1 map
812
816
(suppress-keymap map)
813
- (define-key map (kbd " RET" ) #'leetcode-show-description )
817
+ (define-key map (kbd " RET" ) #'leetcode-show-current-problem )
814
818
(define-key map " n" #'next-line )
815
819
(define-key map " p" #'previous-line )
816
820
(define-key map " g" #'leetcode-problems-refresh )
0 commit comments