func getNumberOfEntries(date time.Time, w http.ResponseWriter, r *http.Request) (int, error) { type totalResponse struct { Total int `json:"total"` } token, err := getTokenOrError(w, r) if err != nil { l.Log(err, l.ErrorMessage) return 0, err } parsedAPIURL, err := parseURLandJoinToPath(*apiURL, sierraapi.BibRequestEndpoint) if err != nil { http.Error(w, "Server Error.", http.StatusInternalServerError) l.Log("Internal Server Error at /new handler, unable to parse url.", l.DebugMessage) return 0, err } q := parsedAPIURL.Query() q.Set("limit", "1") q.Set("offset", "0") q.Set("deleted", "false") q.Set("suppressed", "false") q.Set("createdDate", fmt.Sprintf("[%v,%v]", date.AddDate(0, 0, -1).Format(time.RFC3339), date.Format(time.RFC3339))) q.Set("fields", "default") parsedAPIURL.RawQuery = q.Encode() resp, err := sierraapi.SendRequestToAPI(parsedAPIURL.String(), token, w, r) if err != nil { l.Log(fmt.Sprintf("Internal Server Error at /new, %v", err), l.ErrorMessage) return 0, err } if resp.StatusCode == http.StatusUnauthorized { http.Error(w, "Token is out of date, or is refreshing. Try request again.", http.StatusInternalServerError) tokenStore.Refresh <- struct{}{} l.Log("Token is out of date.", l.ErrorMessage) return 0, errors.New("Unauthorized") } var response totalResponse err = json.NewDecoder(resp.Body).Decode(&response) return response.Total, nil }
func getNewItems(alreadyProcessed map[int]sierraapi.BibRecordOut, date time.Time, w http.ResponseWriter, r *http.Request) (map[int]sierraapi.BibRecordOut, error) { token, err := getTokenOrError(w, r) if err != nil { l.Log(err, l.ErrorMessage) return nil, err } parsedAPIURL, err := parseURLandJoinToPath(*apiURL, sierraapi.BibRequestEndpoint) if err != nil { http.Error(w, "Server Error.", http.StatusInternalServerError) l.Log("Internal Server Error at /new handler, unable to parse url.", l.DebugMessage) return nil, err } total, err := getNumberOfEntries(date, w, r) if err != nil { return nil, err } offset := 0 needOneMoreDay := false if total >= *newLimit { offset = total - *newLimit } else { needOneMoreDay = true } q := parsedAPIURL.Query() q.Set("offset", strconv.Itoa(offset)) q.Set("deleted", "false") q.Set("createdDate", fmt.Sprintf("[%v,%v]", date.AddDate(0, 0, -1).Format(time.RFC3339), date.Format(time.RFC3339))) q.Set("fields", "marc,default") q.Set("suppressed", "false") parsedAPIURL.RawQuery = q.Encode() resp, err := sierraapi.SendRequestToAPI(parsedAPIURL.String(), token, w, r) if err != nil { l.Log(fmt.Sprintf("Internal Server Error at /new, %v", err), l.ErrorMessage) return nil, err } if resp.StatusCode == http.StatusUnauthorized { http.Error(w, "Token is out of date, or is refreshing. Try request again.", http.StatusInternalServerError) tokenStore.Refresh <- struct{}{} l.Log("Token is out of date.", l.ErrorMessage) return nil, err } var response sierraapi.BibRecordsIn err = json.NewDecoder(resp.Body).Decode(&response) defer resp.Body.Close() if err != nil { http.Error(w, "JSON Decoding Error", http.StatusInternalServerError) l.Log(fmt.Sprintf("Internal Server Error at /new handler, JSON Decoding Error: %v", err), l.WarnMessage) return nil, err } entries := response.Convert() for _, entry := range *entries { alreadyProcessed[entry.BibID] = entry } if needOneMoreDay { return getNewItems(alreadyProcessed, date.Add(time.Duration(1435)*time.Minute*-1), w, r) } else { return alreadyProcessed, nil } }
func statusBibHandler(w http.ResponseWriter, r *http.Request) { setACAOHeader(w, r, *headerACAO) token, err := getTokenOrError(w, r) if err != nil { l.Log(err, l.ErrorMessage) return } bibID := strings.Split(r.URL.Path[len("/status/bib/"):], "/")[0] if bibID == "" { http.Error(w, "Error, you need to provide a BibID. /status/bib/[BidID]", http.StatusBadRequest) l.Log("Bad Request at /status/bib/ handler, no BidID provided.", l.TraceMessage) return } parsedAPIURL, err := parseURLandJoinToPath(*apiURL, sierraapi.ItemRequestEndpoint) if err != nil { http.Error(w, "Server Error.", http.StatusInternalServerError) l.Log("Internal Server Error at /status/bib/ handler, unable to parse url.", l.DebugMessage) return } q := parsedAPIURL.Query() q.Set("bibIds", bibID) q.Set("deleted", "false") q.Set("suppressed", "false") parsedAPIURL.RawQuery = q.Encode() resp, err := sierraapi.SendRequestToAPI(parsedAPIURL.String(), token, w, r) if err != nil { l.Log(fmt.Sprintf("Internal Server Error at /status/bib/, %v", err), l.ErrorMessage) return } if resp.StatusCode == http.StatusUnauthorized { http.Error(w, "Token is out of date, or is refreshing. Try request again.", http.StatusInternalServerError) tokenStore.Refresh <- struct{}{} l.Log("Token is out of date.", l.ErrorMessage) return } if resp.StatusCode == http.StatusNotFound { http.Error(w, "No item records for that BibID.", http.StatusNotFound) l.Log(fmt.Sprintf("No items records match BibID %v", bibID), l.TraceMessage) return } var responseJSON sierraapi.ItemRecordsIn err = json.NewDecoder(resp.Body).Decode(&responseJSON) defer resp.Body.Close() if err != nil { http.Error(w, "JSON Decoding Error", http.StatusInternalServerError) l.Log(fmt.Sprintf("Internal Server Error at /status/bib/ handler, JSON Decoding Error: %v", err), l.WarnMessage) return } finalJSON, err := json.Marshal(responseJSON.Convert()) if err != nil { http.Error(w, "JSON Encoding Error", http.StatusInternalServerError) l.Log(fmt.Sprintf("Internal Server Error at /status/bib/ handler, JSON Encoding Error: %v", err), l.WarnMessage) return } l.Log(fmt.Sprintf("Sending response at /status/bib/ handler: %v", responseJSON.Convert()), l.TraceMessage) w.Header().Set("Content-Type", "application/json;charset=UTF-8") w.Write(finalJSON) }