diff --git a/internal/hub/places/location.go b/internal/hub/places/location.go index 8c7fc54bd..383eb7607 100644 --- a/internal/hub/places/location.go +++ b/internal/hub/places/location.go @@ -77,60 +77,11 @@ func FindLocation(id string) (result Location, err error) { } var r *http.Response - var req *http.Request // Try all the specified backend service URLs. for _, serviceUrl := range ServiceUrls { - // Compose request URL with S2 cell ID. - reqUrl := fmt.Sprintf(serviceUrl, id) - - // Log request URL. - log.Tracef("places: sending request to %s", reqUrl) - - // Create GET request instance. - req, err = http.NewRequest(http.MethodGet, reqUrl, nil) - - // Ok? - if err != nil { - log.Errorf("places: %s", err.Error()) - return result, err - } - - // Set user agent. - if UserAgent != "" { - req.Header.Set("User-Agent", UserAgent) - } else { - req.Header.Set("User-Agent", "PhotoPrism/Test") - } - - // Add API key? - if Key != "" { - req.Header.Set("X-Key", Key) - req.Header.Set("X-Signature", fmt.Sprintf("%x", sha1.Sum([]byte(Key+reqUrl+Secret)))) - } - - // Create new http.Client. - // - // NOTE: Timeout specifies a time limit for requests made by - // this Client. The timeout includes connection time, any - // redirects, and reading the response body. The timer remains - // running after Get, Head, Post, or Do return and will - // interrupt reading of the Response.Body. - client := &http.Client{Timeout: 60 * time.Second} - - // Perform request. - for i := 0; i < Retries; i++ { - r, err = client.Do(req) - - // Successful? - if err == nil { - break - } - - // Wait before trying again? - if RetryDelay.Nanoseconds() > 0 { - time.Sleep(RetryDelay) - } + if r, err = PerformRequest(serviceUrl, id); err == nil { + break } } @@ -163,6 +114,65 @@ func FindLocation(id string) (result Location, err error) { return result, nil } +// PerformRequest fetches the cell ID data from the service URL. +func PerformRequest(serviceUrl, id string) (r *http.Response, err error) { + var req *http.Request + + // Compose request URL with S2 cell ID. + reqUrl := fmt.Sprintf(serviceUrl, id) + + // Log request URL. + log.Tracef("places: sending request to %s", reqUrl) + + // Create GET request instance. + req, err = http.NewRequest(http.MethodGet, reqUrl, nil) + + // Ok? + if err != nil { + log.Errorf("places: %s", err.Error()) + return r, err + } + + // Set user agent. + if UserAgent != "" { + req.Header.Set("User-Agent", UserAgent) + } else { + req.Header.Set("User-Agent", "PhotoPrism/Test") + } + + // Add API key? + if Key != "" { + req.Header.Set("X-Key", Key) + req.Header.Set("X-Signature", fmt.Sprintf("%x", sha1.Sum([]byte(Key+reqUrl+Secret)))) + } + + // Create new http.Client. + // + // NOTE: Timeout specifies a time limit for requests made by + // this Client. The timeout includes connection time, any + // redirects, and reading the response body. The timer remains + // running after Get, Head, Post, or Do return and will + // interrupt reading of the Response.Body. + client := &http.Client{Timeout: 60 * time.Second} + + // Perform request. + for i := 0; i < Retries; i++ { + r, err = client.Do(req) + + // Ok? + if err == nil { + return r, nil + } + + // Wait before trying again? + if RetryDelay.Nanoseconds() > 0 { + time.Sleep(RetryDelay) + } + } + + return r, err +} + // CellID returns the S2 cell identifier string. func (l Location) CellID() string { return l.ID