Skip to content

Commit

Permalink
fix: change readme from get to execute
Browse files Browse the repository at this point in the history
  • Loading branch information
tonnytg committed Jul 20, 2024
1 parent 9c4cb4e commit 0f20b8e
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 49 deletions.
72 changes: 47 additions & 25 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ You can use Get or Post to make a request, and then use the `Response` object to
)

func main() {
response, err := webreq.Get("https://api.example.com/data")
request := webreq.NewRequest("GET")
request.SetURL("https://example.com")
request.SetTimeout(10)

// Execute the request and get the response
response, err := request.Execute()
if err != nil {
fmt.Println("Error:", err)
fmt.Println("Error executing request:", err)
return
}
fmt.Println("Response:", response)
}


Expand Down Expand Up @@ -60,10 +64,9 @@ What do you think? Liked, set star in this project to help me to help others!

### Using WebReq

headers := webreq.NewHeaders(map[string]string{
"Content-Type": "application/json",
"Authorization": "Bearer your_token",
})
var headers = make(map[string]string)
headers["Content-Type"] = "application/json"
headers["Authorization"] = "Bearer your_token"

request := webreq.NewRequest("POST")
request.SetURL("https://api.example.com/data")
Expand Down Expand Up @@ -96,12 +99,15 @@ Create a slice of headers to use and input in request
)

func main() {
response, err := webreq.Get("https://api.example.com/data")
request := webreq.NewRequest("GET")
request.SetURL("https://example.com")

// Execute the request and get the response
response, err := request.Execute()
if err != nil {
fmt.Println("Error:", err)
fmt.Println("Error executing request:", err)
return
}
fmt.Println("Response:", response)
}


Expand All @@ -117,13 +123,22 @@ Create a body data to send in request
)

func main() {
data := map[string]string{"key": "value"}
response, err := webreq.Post("https://api.example.com/data", data)
var headers = make(map[string]string)
headers["Content-Type"] = "application/json"
headers["Authorization"] = "Bearer your_token"
request := webreq.NewRequest("POST")
request.SetURL("https://api.example.com/data")
request.SetData([]byte(`{"key":"value"}`)) // set data to POST
request.SetHeaders(headers.Headers)
request.SetTimeout(10)
response, err := request.Execute()
if err != nil {
fmt.Println("Error:", err)
fmt.Println("Error executing request:", err)
return
}
fmt.Println("Response:", response)
fmt.Println("Response:", string(response))
}

Expand All @@ -133,17 +148,24 @@ Create a body data to send in request

### Custom Headers

headers := map[string]string{
"Content-Type": "application/json",
"Authorization": "Bearer your_token",
}

response, err := webreq.GetWithHeaders("https://api.example.com/data", headers)
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println("Response:", response)
headers := webreq.NewHeaders(nil)
headers.Add("Content-Type", "application/json")
if headers.Headers["Content-Type"] != "application/json" {
t.Error("headers.Headers[Content-Type] is not application/json")
}
request := webreq.NewRequest("POST")
request.SetURL("https://api.example.com/data")
request.SetData([]byte(`{"key":"value"}`)) // set data to POST
request.SetHeaders(headers.Headers)
request.SetTimeout(10)
response, err := request.Execute()
if err != nil {
fmt.Println("Error executing request:", err)
return
}
fmt.Println("Response:", string(response))



Expand Down
17 changes: 8 additions & 9 deletions examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,18 @@ type Friend struct {
}

func main() {
// Initialize headers with an empty map
headers := webreq.NewHeaders(nil)
// Add Content-Type header
headers.Add("Content-Type", "application/json")

var headers = make(map[string]string)
headers["Content-Type"] = "application/json"

// Create a new Friend instance
f := Friend{
data := Friend{
CreatedAt: time.Now(),
Name: "Tonny",
}

// Convert Friend instance to JSON bytes
fBytes, err := json.Marshal(f)
dataBytes, err := json.Marshal(data)
if err != nil {
fmt.Println("Error marshalling JSON:", err)
return
Expand All @@ -35,11 +34,11 @@ func main() {
// Create a new POST request
request := webreq.NewRequest("POST")
// Set the request URL
request.SetURL("https://623a666d5f037c136217238f.mockapi.io/api/v1/categories")
request.SetURL("https://example.com/api/v1/values")
// Set the request body data
request.SetData(fBytes)
request.SetData(dataBytes)
// Set the request headers
request.SetHeaders(headers.Headers) // Set map directly
request.SetHeaders(headers) // Set map directly
// Set the request timeout to 10 seconds
request.SetTimeout(10)

Expand Down
12 changes: 8 additions & 4 deletions webreq.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,25 @@ const (
type HeadersMap map[string]string

type Headers struct {
Headers HeadersMap
ListHeaders HeadersMap
}

// NewHeaders creates a new Headers instance, initializing with provided headers or an empty map
func NewHeaders(headers map[string]string) *Headers {
if headers != nil {
return &Headers{
Headers: headers,
ListHeaders: headers,
}
}
return &Headers{
Headers: make(HeadersMap),
ListHeaders: make(HeadersMap),
}
}

// Add adds a new header key-value pair to the Headers
func (header *Headers) Add(key string, value string) {
if key != "" && value != "" {
header.Headers[key] = value
header.ListHeaders[key] = value
}
}

Expand Down Expand Up @@ -114,6 +114,10 @@ func (request *Request) SetStatusCode(statusCodeValue int) *Request {
return request
}

func (request *Request) Check() error {
return nil
}

// Execute sends the request and returns the response body and error if any
func (request *Request) Execute() ([]byte, error) {
client := &http.Client{}
Expand Down
16 changes: 8 additions & 8 deletions webreq_get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ func TestPackageCall(t *testing.T) {
headers := webreq.NewHeaders(headersList)

request := webreq.NewRequest("GET")
request.SetURL("https://610aa52552d56400176afebe.mockapi.io/api/v1/friendlist")
request.SetHeaders(headers.Headers) // Pass the map directly here
request.SetURL("https://examples.com/values")
request.SetHeaders(headers.ListHeaders) // Pass the map directly here
request.SetTimeout(10)

body, err := request.Execute()
Expand Down Expand Up @@ -58,12 +58,12 @@ func TestSetHeaders(t *testing.T) {
headersList["Content-Type"] = "application/json"

headers := webreq.NewHeaders(headersList)
if headers.Headers["Content-Type"] != "application/json" {
if headers.ListHeaders["Content-Type"] != "application/json" {
t.Error("headers.Headers[Content-Type] is not application/json")
}

request := webreq.NewRequest("GET")
request.SetHeaders(headers.Headers)
request.SetHeaders(headers.ListHeaders)
if request.ErrorMessage != "" {
t.Error("request.ErrorMessage is not empty")
}
Expand All @@ -74,12 +74,12 @@ func TestSetHeaders2(t *testing.T) {

headers := webreq.NewHeaders(nil)
headers.Add("Content-Type", "application/json")
if headers.Headers["Content-Type"] != "application/json" {
if headers.ListHeaders["Content-Type"] != "application/json" {
t.Error("headers.Headers[Content-Type] is not application/json")
}

request := webreq.NewRequest("GET")
request.SetHeaders(headers.Headers)
request.SetHeaders(headers.ListHeaders)
if request.ErrorMessage != "" {
t.Error("request.ErrorMessage is not empty")
}
Expand Down Expand Up @@ -149,11 +149,11 @@ func TestEndToEnd(t *testing.T) {
if request == nil {
t.Error("request is nil")
}
request.SetURL("https://610aa52552d56400176afebe.mockapi.io/api/v1/friendlist")
request.SetURL("https://example.com/values")
if request.ErrorMessage != "" {
t.Error("request.ErrorMessage is not empty")
}
request.SetHeaders(headers.Headers)
request.SetHeaders(headers.ListHeaders)
if request.ErrorMessage != "" {
t.Error("request.ErrorMessage is not empty")
}
Expand Down
6 changes: 3 additions & 3 deletions webreq_post_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestPackagePost(t *testing.T) {

headers := webreq.NewHeaders(nil)
headers.Add("Content-Type", "application/json")
if len(headers.Headers) != 1 {
if len(headers.ListHeaders) != 1 {
t.Error("headers is empty")
}

Expand All @@ -36,9 +36,9 @@ func TestPackagePost(t *testing.T) {
}

request := webreq.NewRequest("POST")
request.SetURL("https://610aa52552d56400176afebe.mockapi.io/api/v1/friendlist")
request.SetURL("https://examples.com/values")
request.SetData(fBytes)
request.SetHeaders(headers.Headers) // Set map directly
request.SetHeaders(headers.ListHeaders) // Set map directly
request.SetTimeout(10)

body, err := request.Execute()
Expand Down

0 comments on commit 0f20b8e

Please sign in to comment.