feat: add prototype for go importer

This commit is contained in:
Marcel Arndt
2025-06-30 07:56:03 +02:00
parent c2b5f55d2d
commit 79da91df00
12 changed files with 480 additions and 0 deletions
@@ -0,0 +1,13 @@
package connector
import "time"
type DataSource interface {
Read() ([]byte, error)
}
var backoffSchedule = []time.Duration{
1 * time.Second,
3 * time.Second,
10 * time.Second,
}
@@ -0,0 +1,43 @@
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
}
@@ -0,0 +1,88 @@
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
}
@@ -0,0 +1,7 @@
package transformer
import "synapse/pkg/model"
func TransformTourData(rawData []byte) ([]model.TransformedTourModel, error) {
return []model.TransformedTourModel{}, nil
}