Skip to content

Commit 626d0b1

Browse files
committedOct 4, 2019
Add a function to build the Query Params
1 parent 8ee70f1 commit 626d0b1

File tree

4 files changed

+49
-4
lines changed

4 files changed

+49
-4
lines changed
 

Diff for: ‎Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,8 @@ build:
1111
-X ${PROJECT}/internal/version.BuildTime=${BUILD_TIME}" \
1212
-o bin/server ${PROJECT}/cmd/api
1313

14+
test:
15+
GO111MODULE=on go test ./... -v
16+
1417
run:
1518
PORT=8080 DIAG_PORT=8081 ./bin/server

Diff for: ‎internal/controllers/utils.go

+15
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package controllers
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"net/http"
7+
"strings"
68
)
79

810
func getJson(url string, target interface{}) error {
@@ -14,3 +16,16 @@ func getJson(url string, target interface{}) error {
1416

1517
return json.NewDecoder(r.Body).Decode(target)
1618
}
19+
20+
func buildQueryParams(prefix string, params map[string][]string) string {
21+
var b strings.Builder
22+
b.WriteString(prefix)
23+
24+
for k, v := range params {
25+
for _, element := range v {
26+
b.WriteString(fmt.Sprintf("%s=%s&", k, element))
27+
}
28+
}
29+
30+
return strings.TrimSuffix(b.String(), "&")
31+
}

Diff for: ‎internal/controllers/utils_test.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package controllers
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestBuildQueryUrl(t *testing.T) {
8+
queryParams := map[string][]string{
9+
"dataset": []string{"velib-disponibilite-en-temps-reel"},
10+
"facet": []string{"overflowactivation", "creditcard", "kioskstate", "station_state"},
11+
"geofilter.distance": []string{"1,2,3"},
12+
}
13+
14+
response := buildQueryParams("?", queryParams)
15+
16+
expected := "?dataset=velib-disponibilite-en-temps-reel&facet=overflowactivation&facet=creditcard&facet=kioskstate&facet=station_state&geofilter.distance=1,2,3"
17+
18+
if response != expected {
19+
t.Errorf("Query build was incorrect, got: %s, want: %s.", response, expected)
20+
}
21+
}

Diff for: ‎internal/controllers/velibs.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,23 @@ func ReadVelib() http.HandlerFunc {
1515
long := "2.402782"
1616
radius := "100"
1717

18+
queryParams := map[string][]string{
19+
"dataset": []string{"velib-disponibilite-en-temps-reel"},
20+
"facet": []string{"overflowactivation", "creditcard", "kioskstate", "station_state"},
21+
"geofilter.distance": []string{fmt.Sprintf("%s,%s,%s", lat, long, radius)},
22+
}
23+
1824
return func(w http.ResponseWriter, r *http.Request) {
1925
route := "https://opendata.paris.fr/api/records/1.0/search"
2026

21-
// Assemblage route + query string avec encodage des caractères spéciaux
22-
params := fmt.Sprintf("?dataset=velib-disponibilite-en-temps-reel&facet=overflowactivation&facet=creditcard&facet=kioskstate&facet=station_state&geofilter.distance=%s,%s,%s",
23-
lat, long, radius)
24-
url := fmt.Sprintf("%s/%s", route, html.EscapeString(params))
27+
queryParams := buildQueryParams("?", queryParams)
28+
29+
url := fmt.Sprintf("%s/%s", route, html.EscapeString(queryParams))
2530

2631
payload := models.VelibStatus{}
2732
err := getJson(url, &payload)
2833
if err != nil {
34+
log.Warn("Error reading velib status")
2935
http.Error(w, err.Error(), http.StatusBadRequest)
3036
return
3137
}

0 commit comments

Comments
 (0)
Please sign in to comment.