-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.go
More file actions
45 lines (38 loc) · 979 Bytes
/
handler.go
File metadata and controls
45 lines (38 loc) · 979 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package main
import (
"fmt"
"github.com/gorilla/mux"
"io"
"net/http"
)
// CurrencyHandler calls the /currency endpoint on hitbtc.com
func CurrencyHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
url := fmt.Sprintf("%s/currency", HiBTCBaseEndpoint)
muxVars := mux.Vars(r)
if symbol, ok := muxVars["symbol"]; ok {
// If we specify /currency/<symbol> , we want to call
// /api/v2/currency/<symbol> to get data for that symbol.
// but if we specify /currency/all , we want to call
// /api/v2/currency to get data for all symbols
if symbol != "all" {
url = fmt.Sprintf("%s/%s", url, symbol)
}
} else {
reportError(w, fmt.Errorf("no symbol specified"))
return
}
resp, err := http.Get(url)
if err != nil {
reportError(w, err)
return
}
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
if err != nil {
reportError(w, err)
return
}
w.WriteHeader(resp.StatusCode)
w.Write(b)
}