ChainSafe / gossamer

🕸️ Go Implementation of the Polkadot Host
https://chainsafe.github.io/gossamer
GNU Lesser General Public License v3.0
427 stars 110 forks source link

refactor: use method Get instead Do #3980

Open EmilGeorgiev opened 1 month ago

EmilGeorgiev commented 1 month ago

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:

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:

for tries := 0; tries < MaxTries; tries++ {
    resp, err := client.Get(dest)
    ...
}
segunjkf commented 1 month ago

Hello, I would like to be assigned to this issue.