Skip to content

Commit 21aa687

Browse files
feat: Retry mechanism added
1 parent ae5e0f3 commit 21aa687

File tree

4 files changed

+26
-8
lines changed

4 files changed

+26
-8
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ module github.com/codescalersinternships/PokeAPIHTTPClient-RawanMostafa
33
go 1.23.1
44

55
require (
6-
github.com/cenkalti/backoff/v4 v4.3.0
6+
github.com/cenkalti/backoff v2.2.1+incompatible
77
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0
88
)

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
2-
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
1+
github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4=
2+
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
33
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 h1:e66Fs6Z+fZTbFBAxKfP3PALWBtpfqks2bwGcexMxgtk=
44
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0/go.mod h1:2TbTHSBQa924w8M6Xs1QcRcFwyucIwBGpK1p2f1YFFY=

pkg/httpUtil.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ func (c Client) sendRequest(options ...int) (*http.Response, error) {
4747
offset = options[0]
4848
}
4949

50+
5051
url := baseUrl + c.Endpoint + fmt.Sprintf("?offset=%d&limit=%d", offset, limit)
5152

5253
req, err := http.NewRequest("GET", url, nil)

pkg/pokemon.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,35 @@ package pokemon
44
import (
55
"encoding/json"
66
"fmt"
7+
"net/http"
8+
"time"
79

10+
"github.com/cenkalti/backoff"
811
"golang.org/x/exp/slog"
912
)
10-
// Pokemon takes the pokemon id as an integer, send the request with the /pokemon endpoint
13+
14+
// Pokemon takes the pokemon id as an integer, send the request with the /pokemon endpoint
1115
// parses the response and returns the Pokemon object and an error if exists
1216
func (c Client) Pokemon(id int) (pokemon Pokemon, err error) {
1317
c.Endpoint = fmt.Sprintf("/pokemon/%d", id)
14-
resp, err := c.sendRequest()
15-
if err != nil {
16-
slog.Error("error in sending the request: %w", err)
17-
return
18+
19+
var resp *http.Response
20+
21+
expBackoff := backoff.NewExponentialBackOff()
22+
expBackoff.MaxElapsedTime = 10 * time.Second
23+
24+
retryError := backoff.RetryNotify(func() error {
25+
resp, err = c.sendRequest()
26+
return err
27+
}, expBackoff, func(err error, d time.Duration) {
28+
slog.Warn("Request failed, Retrying ...")
29+
})
30+
31+
if retryError != nil {
32+
slog.Error("failed to make the request after retries: %v", err)
33+
return pokemon, retryError
1834
}
35+
1936
body, err := readBody(resp)
2037
if err != nil {
2138
slog.Error("error in reading the response body: %w", err)

0 commit comments

Comments
 (0)