Request and response moved to http package

This commit is contained in:
Musab Gültekin
2019-06-29 13:36:39 +03:00
parent 59757607eb
commit 1e109c555d
7 changed files with 63 additions and 62 deletions

30
http/request.go Normal file
View File

@ -0,0 +1,30 @@
package http
import (
"io"
"net/http"
)
// Request is a small wrapper around *http.Request that contains Metadata and Rendering option
type Request struct {
*http.Request
Meta map[string]interface{}
Synchronized bool
Rendered bool
Cancelled bool
}
// Cancel request
func (r *Request) Cancel() {
r.Cancelled = true
}
// NewRequest returns a new Request given a method, URL, and optional body.
func NewRequest(method, url string, body io.Reader) (*Request, error) {
req, err := http.NewRequest(method, url, body)
if err != nil {
return nil, err
}
return &Request{Request: req}, nil
}

40
http/response.go Normal file
View File

@ -0,0 +1,40 @@
package http
import (
"github.com/PuerkitoBio/goquery"
"net/http"
"net/url"
"strings"
)
// Response type wraps http.Response
// Contains parsed response data and Geziyor functions.
type Response struct {
*http.Response
Body []byte
HTMLDoc *goquery.Document
Meta map[string]interface{}
Request *Request
}
// JoinURL joins base response URL and provided relative URL.
func (r *Response) JoinURL(relativeURL string) string {
parsedRelativeURL, err := url.Parse(relativeURL)
if err != nil {
return ""
}
joinedURL := r.Response.Request.URL.ResolveReference(parsedRelativeURL)
return joinedURL.String()
}
// IsHTML checks if response content is HTML by looking to content-type header
func (r *Response) IsHTML() bool {
contentType := r.Header.Get("Content-Type")
for _, htmlContentType := range []string{"text/html", "application/xhtml+xml", "application/vnd.wap.xhtml+xml"} {
if strings.Contains(contentType, htmlContentType) {
return true
}
}
return false
}