forked from anothertobi/viseca-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
58 lines (49 loc) · 1.33 KB
/
main.go
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
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
import (
"context"
"fmt"
"log"
"net/http"
"net/http/cookiejar"
"os"
"strings"
"github.com/anothertobi/viseca-exporter/internal/csv"
"github.com/anothertobi/viseca-exporter/pkg/viseca"
)
const sessionCookieName = "AL_SESS-S"
// arg0: cardID
// arg1: sessionCookie (e.g. `AL_SESS-S=...`)
func main() {
if len(os.Args) < 3 {
log.Fatal("card ID and session cookie args required")
}
visecaClient, err := initClient(os.Args[2])
if err != nil {
log.Fatalf("error initializing Viseca API client: %v", err)
}
ctx := context.Background()
transactions, err := visecaClient.ListAllTransactions(ctx, os.Args[1])
if err != nil {
log.Fatalf("error listing all transactions: %v", err)
}
fmt.Println(csv.TransactionsString(transactions))
}
func initClient(sessionCookie string) (*viseca.Client, error) {
cookieJar, err := cookiejar.New(nil)
if err != nil {
return nil, err
}
httpClient := http.Client{
Jar: cookieJar,
}
visecaClient := viseca.NewClient(&httpClient)
cookie := &http.Cookie{
Name: sessionCookieName,
Value: extractSessionCookieValue(sessionCookie),
}
httpClient.Jar.SetCookies(visecaClient.BaseURL, []*http.Cookie{cookie})
return visecaClient, nil
}
func extractSessionCookieValue(sessionCookie string) string {
return strings.TrimPrefix(sessionCookie, fmt.Sprintf("%s=", sessionCookieName))
}