diff --git a/README.md b/README.md index 818225e..42fe04a 100644 --- a/README.md +++ b/README.md @@ -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 | diff --git a/main.go b/main.go index b928322..7b48f20 100644 --- a/main.go +++ b/main.go @@ -24,6 +24,7 @@ type Params struct { methodName string verbose bool userAgent string + timeout int } func getProtocol(p Params) Protocol { @@ -67,6 +68,7 @@ func main() { flag.StringVar(¶ms.method, "m", "GET", "method request") flag.StringVar(¶ms.file, "f", "", "file path") flag.StringVar(¶ms.userAgent, "a", "", "user agent header") + flag.IntVar(¶ms.timeout, "timeout", 0, "timeout in seconds") // GRPC exclusive flags flag.StringVar(¶ms.importPath, "import-path", "", "The path to a directory from which proto sources can be imported, for use with -proto flags") diff --git a/protocol.go b/protocol.go index dac1d34..835d79d 100644 --- a/protocol.go +++ b/protocol.go @@ -39,6 +39,7 @@ type GraphQL struct { query string httpResp *http.Response response string + timeout int } func NewGrapQL(params Params) *GraphQL { @@ -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 } @@ -196,6 +197,7 @@ type HTTP struct { file string response string userAgent string + timeout int } func NewHTTP(params Params) *HTTP { @@ -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 @@ -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"}