github.com/line/ostracon@v1.0.10-0.20230328032236-7f20145f065d/libs/net/http.go (about)

     1  package net
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  	"time"
     7  )
     8  
     9  // HttpGet sends a GET request to the specified url with timeout and return the response.
    10  func HttpGet(url string, timeout time.Duration) (*http.Response, error) {
    11  	request, err := http.NewRequestWithContext(context.Background(), http.MethodGet, url, nil)
    12  	if err != nil {
    13  		return nil, err
    14  	}
    15  	return HttpRequest(request, timeout)
    16  }
    17  
    18  // HttpRequest sends the specified HTTP requests with timeout and return the response.
    19  // For stability and security reason, we need to be available request timeouts, so http.Client{} and http.Get() are
    20  // overridden with functions that rely on this function.
    21  func HttpRequest(request *http.Request, timeout time.Duration) (*http.Response, error) {
    22  	client := &http.Client{
    23  		Timeout: timeout,
    24  	}
    25  	return client.Do(request)
    26  }