44 lines
1.1 KiB
Go
44 lines
1.1 KiB
Go
package connector
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"time"
|
|
)
|
|
|
|
type DispoliveDataSource struct {
|
|
}
|
|
|
|
func (dc *DispoliveDataSource) 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
|
|
}
|