readwise.go (1652B)
1 package readwise 2 3 import ( 4 "context" 5 "encoding/json" 6 "fmt" 7 "io" 8 "net/http" 9 "strings" 10 "time" 11 ) 12 13 const ( 14 exportEndpoint = "https://readwise.io/api/v2/export/?" 15 FormatUpdatedAfter = "2006-01-02T15:04:05" 16 ) 17 18 func FetchFromAPI(ctx context.Context, apikey string, updateAfter *time.Time) ([]Result, error) { 19 results := []Result{} 20 httpClient := &http.Client{} 21 22 var e Export 23 var err error 24 var nextPageCursor *int = nil 25 for { 26 e, err = fetchExport(ctx, httpClient, apikey, updateAfter, nextPageCursor) 27 if err != nil { 28 return results, err 29 } 30 results = append(results, e.Results...) 31 nextPageCursor = e.NextPageCursor 32 if nextPageCursor == nil { 33 // No more pages to fetch, we get out 34 break 35 } 36 } 37 38 return results, nil 39 } 40 41 func fetchExport(ctx context.Context, client *http.Client, apikey string, updateAfter *time.Time, nextPageCursor *int) (Export, error) { 42 export := Export{} 43 endpoint := exportEndpoint 44 params := []string{} 45 if updateAfter != nil { 46 params = append(params, "updatedAfter="+updateAfter.Format(FormatUpdatedAfter)) 47 } 48 if nextPageCursor != nil { 49 params = append(params, fmt.Sprintf("pageCursor=%d", *nextPageCursor)) 50 } 51 endpoint = endpoint + strings.Join(params, "&&") 52 req, err := http.NewRequestWithContext(ctx, "GET", endpoint, nil) 53 if err != nil { 54 return export, err 55 } 56 req.Header.Add("Authorization", "Token "+apikey) 57 resp, err := client.Do(req) 58 if err != nil { 59 return export, err 60 } 61 62 defer resp.Body.Close() 63 64 body, err := io.ReadAll(resp.Body) 65 if err != nil { 66 return export, err 67 } 68 69 err = json.Unmarshal(body, &export) 70 if err != nil { 71 return export, err 72 } 73 74 return export, nil 75 }