Skip to content

Commit 44a1655

Browse files
committed
Merge master: add per-package HTTP clients, status code check, tests
2 parents 5e5524a + 46d58ba commit 44a1655

11 files changed

Lines changed: 49 additions & 17 deletions

File tree

.github/workflows/go.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ jobs:
1414
build:
1515
runs-on: ubuntu-latest
1616
steps:
17-
- uses: actions/checkout@v4
17+
- uses: actions/checkout@v6
1818

1919
- name: Set up Go
20-
uses: actions/setup-go@v5
20+
uses: actions/setup-go@v6
2121
with:
2222
go-version: 1.25
2323

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ aklrubbish
22
*~
33
.vscode
44
.idea
5+
.env
56

67
# binary files
78
aklapi

AGENTS.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ This document provides guidance for agentic coding assistants operating in this
99
`aklapi` is a Go library and HTTP server that exposes Auckland Council APIs
1010
(rubbish collection schedules, property address lookup) as a simple REST service.
1111

12-
- **Module:** `github.com/rusq/aklapi` (`go 1.24`)
12+
- **Module:** `github.com/rusq/aklapi` (`go 1.25`)
1313
- **Library package:** root (`aklapi`)
1414
- **Binary:** `cmd/aklapi/` — standard HTTP server on port 8080
1515
- **Language:** Go only — no TypeScript, JavaScript, or Node tooling
@@ -54,6 +54,7 @@ docker build -t aklapi .
5454

5555
# Make targets
5656
make server # go build -o server ./cmd/aklapi
57+
make test # go test ./... -race
5758
make docker # docker build -t aklapi .
5859
```
5960

Makefile

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@ SHELL=/bin/sh
22

33
IMAGE=aklapi
44

5-
SRC=main.go $(wildcard aklapi/*.go)
5+
SRC=$(wildcard aklapi/*.go)
6+
PKG=./cmd/aklapi
67

78
server: $(SRC)
8-
go build -o $@
9+
go build -o $@ $(PKG)
10+
11+
test:
12+
go test ./... -race
13+
.PHONY: test
914

1015
docker:
1116
docker build -t $(IMAGE) .

addr.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
var (
1414
// defined as a variable so it can be overridden in tests.
1515
addrURI = `https://experience.aucklandcouncil.govt.nz/nextapi/property`
16+
// defined as a variable so tests can replace it.
17+
addrHTTPClient = &http.Client{Timeout: 15 * time.Second, Transport: &browserTransport{wrapped: http.DefaultTransport}}
1618
)
1719

1820
// AddrRequest is the address request.
@@ -61,7 +63,7 @@ func MatchingPropertyAddresses(ctx context.Context, addrReq *AddrRequest) (*Addr
6163
req.URL.RawQuery = q.Encode()
6264

6365
start := time.Now()
64-
resp, err := aklClient.Do(req)
66+
resp, err := addrHTTPClient.Do(req)
6567
if err != nil {
6668
return nil, err
6769
}

aklapi.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,6 @@ var (
1414
// http client, so we send a browser-compatible value instead.
1515
const userAgent = "Mozilla/5.0 (compatible; aklapi/1.0)"
1616

17-
// aklClient is a shared HTTP client that injects the required headers on every
18-
// outgoing request so that the Auckland Council CDN does not reject them.
19-
var aklClient = &http.Client{
20-
Transport: &browserTransport{wrapped: http.DefaultTransport},
21-
}
22-
2317
// browserTransport is an http.RoundTripper that adds browser-like headers to
2418
// every request before forwarding it to the underlying transport.
2519
type browserTransport struct {

cmd/aklapi/handlers.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ func respond(w http.ResponseWriter, data any, code int) {
3030
b, err := json.Marshal(data)
3131
if err != nil {
3232
http.Error(w, err.Error(), http.StatusInternalServerError)
33+
return
3334
}
3435
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
3536
w.WriteHeader(code)

go.mod

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
module github.com/rusq/aklapi
22

3-
go 1.24.0
4-
5-
toolchain go1.24.2
3+
go 1.25.0
64

75
require (
86
github.com/PuerkitoBio/goquery v1.11.0
@@ -15,6 +13,6 @@ require (
1513
github.com/andybalholm/cascadia v1.3.3 // indirect
1614
github.com/davecgh/go-spew v1.1.1 // indirect
1715
github.com/pmezard/go-difflib v1.0.0 // indirect
18-
golang.org/x/net v0.48.0 // indirect
16+
golang.org/x/net v0.51.0 // indirect
1917
gopkg.in/yaml.v3 v3.0.1 // indirect
2018
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ golang.org/x/net v0.39.0 h1:ZCu7HMWDxpXpaiKdhzIfaltL9Lp31x/3fCP11bc6/fY=
4646
golang.org/x/net v0.39.0/go.mod h1:X7NRbYVEA+ewNkCNyJ513WmMdQ3BineSwVtN2zD/d+E=
4747
golang.org/x/net v0.48.0 h1:zyQRTTrjc33Lhh0fBgT/H3oZq9WuvRR5gPC70xpDiQU=
4848
golang.org/x/net v0.48.0/go.mod h1:+ndRgGjkh8FGtu1w1FGbEC31if4VrNVMuKTgcAAnQRY=
49+
golang.org/x/net v0.51.0 h1:94R/GTO7mt3/4wIKpcR5gkGmRLOuE/2hNGeWq/GBIFo=
50+
golang.org/x/net v0.51.0/go.mod h1:aamm+2QF5ogm02fjy5Bb7CQ0WMt1/WVM7FtyaTLlA9Y=
4951
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
5052
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
5153
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=

rubbish.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ const (
2323
var (
2424
// defined as a variable so it can be overridden in tests.
2525
collectionDayURI = `https://www.aucklandcouncil.govt.nz/en/rubbish-recycling/rubbish-recycling-collections/rubbish-recycling-collection-days/%s.html`
26+
// defined as a variable so tests can replace it.
27+
collectionHTTPClient = &http.Client{Timeout: 15 * time.Second, Transport: &browserTransport{wrapped: http.DefaultTransport}}
2628
)
2729

2830
var errSkip = errors.New("skip this date")
@@ -118,11 +120,14 @@ func fetchandparse(ctx context.Context, addressID string) (*CollectionDayDetailR
118120
if err != nil {
119121
return nil, err
120122
}
121-
resp, err := aklClient.Do(req)
123+
resp, err := collectionHTTPClient.Do(req)
122124
if err != nil {
123125
return nil, err
124126
}
125127
defer resp.Body.Close()
128+
if resp.StatusCode != http.StatusOK {
129+
return nil, fmt.Errorf("collection API returned status code: %d", resp.StatusCode)
130+
}
126131
return parse(resp.Body)
127132
}
128133

0 commit comments

Comments
 (0)