-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathcompanies.lua
81 lines (75 loc) · 2.16 KB
/
companies.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
local utils = require("leetcode.api.utils")
local config = require("leetcode.config")
local log = require("leetcode.logger")
local queries = require("leetcode.api.queries")
local urls = require("leetcode.api.urls")
local Spinner = require("leetcode.logger.spinner")
---@class lc.CompaniesApi
local Companies = {}
---@param cb? fun(res: lc.cache.Company[]|nil, err: lc.err)
---@param noti? boolean
--
---@return lc.cache.Company[] lc.err
function Companies.all(cb, noti)
local query = queries.companies
local spinner
if noti then
spinner = Spinner:init("updating cache...", "points")
end
if cb then
utils.query(query, _, {
endpoint = urls.companies,
callback = function(res, err)
if err then
if spinner then
spinner:stop(err.msg, false)
end
return cb(nil, err)
end
local data = res.data
local companies = data["companyTags"]
if spinner then
spinner:stop("cache updated")
end
cb(companies)
end,
})
else
local res, err = utils.query(query)
if err then
if spinner then
spinner:stop(err.msg, false)
end
return nil, err
else
local data = res.data
local companies = data["companyTags"]
if spinner then
spinner:stop("cache updated")
end
return companies
end
end
end
function Companies.problems(company, cb)
local url = urls.company_problems:format(company)
if cb then
utils.get(url, {
callback = function(res, err)
if err then
return cb(nil, err)
end
local questions = res["questions"]
cb(questions)
end
})
else
local res, err = utils.get(url)
if err then
return nil, err
end
local questions = res.data["questions"]
return questions
end
end
return Companies