diff --git a/api.go b/api.go index c4ebae7..86293c8 100644 --- a/api.go +++ b/api.go @@ -50,18 +50,14 @@ func onResponse(res *http.Response) { func reload() { // Ring the doorbell req := BuildRequest("GET", "/", nil) - onRequest(req) - res, _ := client.Do(req) - onResponse(res) + doRequest(req) // Ring the second doorbell (seems to be a way of validating whether a client is a 'browser' or not) req = BuildRequest("GET", "/index.php", map[string]string{ "width": "1920", "height": "1080", }) - onRequest(req) - res, _ = client.Do(req) - onResponse(res) + doRequest(req) // Verify that a PHPSESSID cookie is present site_cookies := client.Jar.Cookies(req.URL) @@ -111,9 +107,7 @@ func GetLocations() []Location { req := BuildRequestWithBody("POST", "/register-get-properties-from-name", nil, bytes.NewBufferString(body)) SetTypicalHeaders(req, nil, nil, true) - onRequest(req) - res, err := client.Do(req) - onResponse(res) + res, err := doRequest(req) if err != nil { log.Fatal(err) } @@ -164,10 +158,8 @@ func GetForm(id uint) GetFormResult { body := fmt.Sprintf("propertyIdSelected=%d&propertySource=parking-snap", id) req := BuildRequestWithBody("POST", "/register-get-vehicle-form", nil, bytes.NewBufferString(body)) SetTypicalHeaders(req, nil, nil, false) - onRequest(req) - res, _ := client.Do(req) - onResponse(res) + res, _ := doRequest(req) html, _ := io.ReadAll(res.Body) htmlString := string(html) @@ -204,10 +196,8 @@ func GetVipForm(id uint, guestCode string) GetFormResult { body := fmt.Sprintf("propertyIdSelected=%d&propertySource=parking-snap&guestCode=%s", id, guestCode) req := BuildRequestWithBody("POST", "/register-get-vehicle-form", nil, bytes.NewBufferString(body)) SetTypicalHeaders(req, nil, nil, false) - onRequest(req) - res, _ := client.Do(req) - onResponse(res) + res, _ := doRequest(req) html, _ := io.ReadAll(res.Body) htmlString := string(html) diff --git a/helpers.go b/helpers.go index 125a65e..b07b0e1 100644 --- a/helpers.go +++ b/helpers.go @@ -80,6 +80,14 @@ func SetTypicalHeaders(req *http.Request, contentType *string, referrer *string, } } +// doRequest performs the given request and calls onRequest and onResponse. +func doRequest(request *http.Request) (*http.Response, error) { + onRequest(request) + response, error := client.Do(request) + onResponse(response) + return response, error +} + // GetRandomItems returns N random items from the given array. // The seed_value is used to control the output. // If the array is not a slice, an error is returned. diff --git a/scan.go b/scan.go index ab4b7c6..6ea4d6e 100644 --- a/scan.go +++ b/scan.go @@ -21,10 +21,8 @@ func Scan() { body := fmt.Sprintf("propertyIdSelected=%d&propertySource=parking-snap", location.id) req := BuildRequestWithBody("POST", "/register-get-vehicle-form", nil, bytes.NewBufferString(body)) SetTypicalHeaders(req, nil, nil, false) - onRequest(req) + res, _ := doRequest(req) - res, _ := client.Do(req) - onResponse(res) html, _ := io.ReadAll(res.Body) html_path := filepath.Join("./forms", fmt.Sprintf("%d.html", location.id))