Skip to content

Commit

Permalink
add /table_v2 api
Browse files Browse the repository at this point in the history
  • Loading branch information
ppaanngggg committed Jul 22, 2024
1 parent b6c6edf commit 0bcee32
Show file tree
Hide file tree
Showing 5 changed files with 13,585 additions and 0 deletions.
34 changes: 34 additions & 0 deletions cmd/main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,40 @@ func main() {
render.JSON(w, r, table)
},
)
r.Post(
"/table_v2", func(w http.ResponseWriter, r *http.Request) {
params, err := pkg.ParseTableParamsV2(globalParams, r.Body)
defer r.Body.Close()
if err != nil {
slog.Error("parse table params v2", "err", err)
render.Status(r, http.StatusBadRequest)
if pkg.IsParamsError(err) {
render.JSON(w, r, err)
} else {
render.PlainText(w, r, err.Error())
}
return
}
uri := params.BuildUri()
slog.Info("to fetch page", "uri", uri)
// check cache
if table, found := tableCache.Get(uri); found {
render.JSON(w, r, table)
return
}
// fetch page and parse table
table, err := pkg.FetchPageAndParseTable(r.Context(), uri, c.EliteLogin)
if err != nil {
slog.Error("fetch page and parse table", "err", err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
// cache table
tableCache.Set(uri, table, cache.DefaultExpiration)
render.JSON(w, r, table)
},
)
r.Get(
"/elite_table", func(w http.ResponseWriter, r *http.Request) {
params, err := pkg.ParseTableParamsWithAPIKey(globalParams, r.URL.Query())
Expand Down
Loading

0 comments on commit 0bcee32

Please sign in to comment.