|
5 | 5 | "encoding/json" |
6 | 6 | "errors" |
7 | 7 | "fmt" |
| 8 | + "github.com/codingpot/paperswithcode-go/models" |
8 | 9 | "net/http" |
9 | 10 | "net/url" |
10 | 11 | "time" |
@@ -177,45 +178,47 @@ func (c *Client) sendRequest(req *http.Request, v interface{}) error { |
177 | 178 | return nil |
178 | 179 | } |
179 | 180 |
|
180 | | -/* |
181 | | - GetPapers API is currently broken. need to wait until it gets fixed |
182 | | -*/ |
183 | | - |
184 | | -// type PaperList struct { |
185 | | -// Count int `json:"count"` |
186 | | -// NextPage string `json:"next,omitempty"` |
187 | | -// PrePage string `json:"previous,omitempty"` |
188 | | -// Results []struct { |
189 | | -// ID string `json:"id"` |
190 | | -// ArxivID []string `json:"arxiv_id,omitempty"` |
191 | | -// NipsID []string `json:"nips_id,omitempty"` |
192 | | -// URLAbs string `json:"url_abs"` |
193 | | -// URLPdf string `json:"url_pdf"` |
194 | | -// Title string `json:"title"` |
195 | | -// Abstract string `json:"abstract"` |
196 | | -// Authors []string `json:"authors"` |
197 | | -// Published string `json:"published"` |
198 | | -// Conference []string `json:"conference,omitempty"` |
199 | | -// ConferenceURLAbs []string `json:"conference_url_abs,omitempty"` |
200 | | -// ConferenceURLPdf []string `json:"conference_url_pdf,omitempty"` |
201 | | -// Proceeding []string `json:"proceeding,omitempty"` |
202 | | -// } `json:"results"` |
203 | | -// } |
204 | | - |
205 | | -// func (c *Client) GetPapers(ctx context.Context, query string, page int, limit int) (*PaperList, error) { |
206 | | -// url := fmt.Sprintf("%s/papers?q=%s&items_per_page=%d&page=%d", c.BaseURL, url.QueryEscape(query), limit, page) |
207 | | -// req, err := http.NewRequest("GET", url, nil) |
208 | | - |
209 | | -// if err != nil { |
210 | | -// return nil, err |
211 | | -// } |
212 | | - |
213 | | -// req = req.WithContext(ctx) |
214 | | - |
215 | | -// res := PaperList{} |
216 | | -// if err := c.sendRequest(req, &res); err != nil { |
217 | | -// return nil, err |
218 | | -// } |
219 | | - |
220 | | -// return &res, nil |
221 | | -// } |
| 181 | +type PaperListParams struct { |
| 182 | + Query string |
| 183 | + Page int |
| 184 | + Limit int |
| 185 | +} |
| 186 | + |
| 187 | +func (p PaperListParams) Build() string { |
| 188 | + if p.Query == "" { |
| 189 | + return fmt.Sprintf("items_per_page=%d&page=%d", p.Limit, p.Page) |
| 190 | + } |
| 191 | + return fmt.Sprintf("q=%s&items_per_page=%d&page=%d", url.QueryEscape(p.Query), p.Limit, p.Page) |
| 192 | +} |
| 193 | + |
| 194 | +func (c *Client) PaperList(params PaperListParams) (*models.PaperListResult, error) { |
| 195 | + papersListURL := c.BaseURL + "/papers?" + params.Build() |
| 196 | + |
| 197 | + request, err := http.NewRequest(http.MethodGet, papersListURL, nil /*body*/) |
| 198 | + if err != nil { |
| 199 | + return nil, err |
| 200 | + } |
| 201 | + request.Header.Set("Authorization", "Token "+c.apiToken) |
| 202 | + |
| 203 | + response, err := c.HTTPClient.Get(papersListURL) |
| 204 | + if err != nil { |
| 205 | + return nil, err |
| 206 | + } |
| 207 | + |
| 208 | + var paperListResult models.PaperListResult |
| 209 | + |
| 210 | + err = json.NewDecoder(response.Body).Decode(&paperListResult) |
| 211 | + if err != nil { |
| 212 | + return nil, err |
| 213 | + } |
| 214 | + |
| 215 | + return &paperListResult, nil |
| 216 | +} |
| 217 | + |
| 218 | +func PaperListParamsDefault() PaperListParams { |
| 219 | + return PaperListParams{ |
| 220 | + Query: "", |
| 221 | + Page: 1, |
| 222 | + Limit: 50, |
| 223 | + } |
| 224 | +} |
0 commit comments