ChainSafe / ChainSafe/gossamer

refactor: use method Get instead Do

Open
#3,980 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
454
Forks
144
PR merge metrics
No merged PRs in 30d

Description

## Issue summary
The function 'GetIPBy' internally use the method 'Do' of the HTTP client, while generally Get, Post and others will be used.

The Do method of the http.Client in Go is a good choice if we want:
1. Flexibility with HTTP Methods - The 'Do' method allows you to use any HTTP method, not just the predefined ones like GET, POST, PUT, DELETE, etc. This is particularly useful for working with APIs that use custom HTTP methods.
2. Full Control Over the Request - When using Do, you can set custom headers, cookies, and other request properties which might not be as straightforward with the shorthand methods. Or custom body.
3. Context support - The Do method supports context for controlling timeouts and cancellations, which is essential for robust and responsive applications.

In our case, only one HTTP method is used ('GET'), context is not set for the requests, and no additional headers are set.The code can be simplified by using method Get:

from:
```go
req, err := http.NewRequest(http.MethodGet, dest, nil)
if err != nil {
return nil, err
}

for tries := 0; tries < MaxTries; tries++ {
resp, err := client.Do(req)
...
}

```

to:
```go
for tries := 0; tries < MaxTries; tries++ {
resp, err := client.Get(dest)
...
}

```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.