Simplify onRequest/onResponse/client.Do calls with doRequest

This commit is contained in:
2023-12-17 04:55:05 -06:00
parent 4f384618bc
commit 5b4da7b0c3
3 changed files with 14 additions and 18 deletions

20
api.go
View File

@@ -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)

View File

@@ -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.

View File

@@ -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))