Skip to content

Commit

Permalink
add timeout param
Browse files Browse the repository at this point in the history
  • Loading branch information
ernesto27 committed Feb 25, 2024
1 parent 8f5ba62 commit 5276ce5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ req -t grpc -u localhost:50051 -import-path /pathprotofiles/helloworld -proto he
| -v | show server header response |
| -d | Download response to file|
| -a | user agent http header |
| -timeout | seconds value |
| -import-path | GRPC - path to proto files |
| -proto | GRPC - proto file name |
| -method | GRPC - method to call |
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Params struct {
methodName string
verbose bool
userAgent string
timeout int
}

func getProtocol(p Params) Protocol {
Expand Down Expand Up @@ -67,6 +68,7 @@ func main() {
flag.StringVar(&params.method, "m", "GET", "method request")
flag.StringVar(&params.file, "f", "", "file path")
flag.StringVar(&params.userAgent, "a", "", "user agent header")
flag.IntVar(&params.timeout, "timeout", 0, "timeout in seconds")

// GRPC exclusive flags
flag.StringVar(&params.importPath, "import-path", "", "The path to a directory from which proto sources can be imported, for use with -proto flags")
Expand Down
13 changes: 10 additions & 3 deletions protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type GraphQL struct {
query string
httpResp *http.Response
response string
timeout int
}

func NewGrapQL(params Params) *GraphQL {
Expand All @@ -58,7 +59,7 @@ func (g *GraphQL) RequestResponse() (string, error) {
return "", err
}

resp, httpResp, err := DoRequest("POST", string(payloadBytes), g.url, "", "", "")
resp, httpResp, err := DoRequest("POST", string(payloadBytes), g.url, "", "", "", g.timeout)
g.httpResp = httpResp
return resp, err
}
Expand Down Expand Up @@ -196,6 +197,7 @@ type HTTP struct {
file string
response string
userAgent string
timeout int
}

func NewHTTP(params Params) *HTTP {
Expand All @@ -206,11 +208,12 @@ func NewHTTP(params Params) *HTTP {
body: params.message,
file: params.file,
userAgent: params.userAgent,
timeout: params.timeout,
}
}

func (h *HTTP) RequestResponse() (string, error) {
resp, httpResp, err := DoRequest(h.method, h.body, h.url, h.userAgent, h.headers, h.file)
resp, httpResp, err := DoRequest(h.method, h.body, h.url, h.userAgent, h.headers, h.file, h.timeout)
h.httpResp = httpResp

return resp, err
Expand Down Expand Up @@ -451,9 +454,13 @@ func hasFileExtension(path string) (bool, string) {
return len(parts) >= 2, lastSegment
}

func DoRequest(method string, body string, urlV string, userAgent string, headers string, file string) (string, *http.Response, error) {
func DoRequest(method string, body string, urlV string, userAgent string, headers string, file string, timeout int) (string, *http.Response, error) {
client := &http.Client{}

if timeout > 0 {
client.Timeout = time.Duration(timeout) * time.Second
}

m := strings.ToUpper(method)

validMethods := []string{"GET", "POST", "DELETE", "PUT"}
Expand Down

0 comments on commit 5276ce5

Please sign in to comment.