89 lines
2.5 KiB
Go
89 lines
2.5 KiB
Go
package connector
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
const dispoliveBaseUrl = "https://avicenna.dispolive.de"
|
|
|
|
type DispoliveLegacyDataSource struct{}
|
|
|
|
func (dc *DispoliveLegacyDataSource) Read(ctx context.Context) ([]byte, error) {
|
|
ctxWithTimeout, cancel := context.WithTimeout(ctx, 1000*time.Millisecond)
|
|
defer cancel()
|
|
|
|
key := os.Getenv("DISPOLIVE_API_KEY")
|
|
if key == "" {
|
|
return []byte{}, fmt.Errorf("couldn't perform request: Dispolive API key not found")
|
|
}
|
|
|
|
req, err := http.NewRequestWithContext(ctxWithTimeout, "GET", "https://avicenna.dispolive.de/custom/open-api/fahrberichte/findByRange/2025-06-20_2025-06-21", nil)
|
|
if err != nil {
|
|
return []byte{}, fmt.Errorf("couldn't construct the request: %w", err)
|
|
}
|
|
req.Header.Set("Authorization", "Bearer "+key)
|
|
req.Header.Set("dispolive-api-version", "v1.0.1")
|
|
|
|
client := &http.Client{}
|
|
res, err := client.Do(req)
|
|
if err != nil {
|
|
return []byte{}, fmt.Errorf("request returned an error: %w", err)
|
|
}
|
|
body, err := io.ReadAll(res.Body)
|
|
res.Body.Close()
|
|
if err != nil {
|
|
return []byte{}, fmt.Errorf("couldn't read body: %w", err)
|
|
}
|
|
|
|
return body, nil
|
|
}
|
|
|
|
func (dc *DispoliveLegacyDataSource) Login() (string, error) {
|
|
username := os.Getenv("DISPOLIVE_USERNAME")
|
|
password := os.Getenv("DISPOLIVE_PASSWORD")
|
|
if username == "" || password == "" {
|
|
return "", fmt.Errorf("authentication not possible, no username or password")
|
|
}
|
|
return dc.login(username, password)
|
|
}
|
|
|
|
func (dc *DispoliveLegacyDataSource) login(username, password string) (string, error) {
|
|
ctxWithTimeout, cancel := context.WithTimeout(context.TODO(), 1000*time.Millisecond)
|
|
defer cancel()
|
|
|
|
payload := map[string]string{"username": username, "password": password}
|
|
jsonPayload, _ := json.Marshal(payload)
|
|
req, err := http.NewRequestWithContext(ctxWithTimeout, "POST", dispoliveBaseUrl+"/login", bytes.NewBuffer(jsonPayload))
|
|
if err != nil {
|
|
return "", fmt.Errorf("couldn't construct the request: %w", err)
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
res, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
return "", fmt.Errorf("request returned an error: %w", err)
|
|
}
|
|
|
|
cookies := res.Header.Values("set-cookie")
|
|
|
|
cookieString := strings.Split(strings.Join(cookies, ";"), ";")
|
|
var authCookies = ""
|
|
for _, v := range cookieString {
|
|
if strings.Contains(v, "koa.sid") {
|
|
if authCookies == "" {
|
|
authCookies = v
|
|
} else {
|
|
authCookies = strings.Join([]string{authCookies, v}, "; ")
|
|
}
|
|
}
|
|
}
|
|
return authCookies, nil
|
|
}
|