-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f0fd3a3
commit 9e160cc
Showing
6 changed files
with
208 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package httpclient | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/rohanraj7316/logger" | ||
) | ||
|
||
var ( | ||
REQUEST_TIMEOUT = "2s" | ||
) | ||
|
||
type Config struct { | ||
// Timeout gives you timeout for request | ||
// Default: 30s | ||
Timeout time.Duration | ||
|
||
// bool flag which help us in configuring proxy | ||
// Default: false | ||
UseProxy bool | ||
|
||
// url need to do the proxy | ||
// Default: nil | ||
ProxyURL string | ||
|
||
// LogReqResEnable helps in logging request & responses. | ||
// Default true | ||
LogReqResEnable bool | ||
|
||
// LogReqResBodyEnable helps in logging request and responses body | ||
// Default true | ||
LogReqResBodyEnable bool | ||
} | ||
|
||
var ConfigDefault = Config{ | ||
UseProxy: false, | ||
LogReqResEnable: true, | ||
LogReqResBodyEnable: true, | ||
} | ||
|
||
func configDefault(config ...Config) Config { | ||
timeout, err := time.ParseDuration(REQUEST_TIMEOUT) | ||
if err != nil { | ||
logger.Error(err.Error()) | ||
ConfigDefault.Timeout = 2 * time.Second | ||
} else { | ||
ConfigDefault.Timeout = timeout | ||
} | ||
|
||
if len(config) < 1 { | ||
return ConfigDefault | ||
} | ||
|
||
cfg := config[0] | ||
|
||
return cfg | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,69 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"libs/httpclient" | ||
"encoding/json" | ||
"log" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/rohanraj7316/logger" | ||
"github.com/rohanraj7316/httpclient" | ||
) | ||
|
||
func NewLogConfig(o *logger.Options) (*logger.Options, error) { | ||
o.JSONEncoding = true | ||
o.IncludeCallerSourceLocation = true | ||
o.LogGrpc = true | ||
return o, nil | ||
type Handler struct { | ||
client *httpclient.HttpClient | ||
} | ||
|
||
func main() { | ||
lOptions, _ := NewLogConfig(logger.NewOptions()) | ||
hOptions := httpclient.Options{ | ||
Timeout: 20 * time.Second, | ||
LoggerOptions: lOptions, | ||
LogReqResEnable: true, | ||
LogReqResBodyEnable: true, | ||
func NewHandler() *Handler { | ||
client, err := httpclient.New() | ||
if err != nil { | ||
log.Println(err) | ||
} | ||
|
||
client, err := httpclient.NewHTTPClient(hOptions) | ||
return &Handler{ | ||
client: client, | ||
} | ||
} | ||
|
||
func (h *Handler) Get(ctx context.Context) { | ||
url := "https://httpbin.org/anything" | ||
header := map[string]string{ | ||
"content-type": "application/json", | ||
} | ||
_, err := h.client.Request(ctx, http.MethodGet, url, header, nil) | ||
if err != nil { | ||
log.Println(err) | ||
} | ||
} | ||
|
||
// GET | ||
ctx := context.Background() | ||
func (h *Handler) Post(ctx context.Context) { | ||
url := "https://httpbin.org/anything" | ||
header := map[string]string{ | ||
"content-type": "application/json", | ||
} | ||
|
||
_, err = client.Request(ctx, http.MethodGet, url, header, nil) | ||
body := map[string]string{ | ||
"name": "Rohan Raj", | ||
} | ||
bBody, err := json.Marshal(&body) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
_, err = h.client.Request(ctx, http.MethodPost, url, header, bytes.NewBuffer(bBody)) | ||
if err != nil { | ||
log.Println(err) | ||
} | ||
} | ||
|
||
func main() { | ||
nH := NewHandler() | ||
c := context.Background() | ||
ctx := context.WithValue(c, "requestId", "123123123123123") | ||
|
||
// GET | ||
nH.Get(ctx) | ||
|
||
// Post | ||
nH.Post(ctx) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,15 @@ | ||
module libs/httpclient | ||
module github.com/rohanraj7316/httpclient | ||
|
||
go 1.17 | ||
|
||
require ( | ||
github.com/rohanraj7316/logger v0.0.0-20220201091549-a76bf7c5c14c // indirect | ||
go.uber.org/atomic v1.7.0 // indirect | ||
go.uber.org/multierr v1.6.0 // indirect | ||
github.com/google/uuid v1.1.2 | ||
github.com/rohanraj7316/logger v0.0.2 | ||
) | ||
|
||
require ( | ||
go.uber.org/atomic v1.9.0 // indirect | ||
go.uber.org/multierr v1.8.0 // indirect | ||
go.uber.org/zap v1.21.0 // indirect | ||
google.golang.org/grpc v1.44.0 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.