Add EFAClient interface
This commit is contained in:
parent
fad4da3f6b
commit
ae7b2f170a
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
|||||||
result
|
result
|
||||||
vrnp
|
vrnp
|
||||||
|
main
|
||||||
*.swp
|
*.swp
|
||||||
|
56
main.go
56
main.go
@ -8,6 +8,7 @@ import "net/http"
|
|||||||
import "net/url"
|
import "net/url"
|
||||||
import "os"
|
import "os"
|
||||||
import "slices"
|
import "slices"
|
||||||
|
import "strings"
|
||||||
import "strconv"
|
import "strconv"
|
||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
@ -43,11 +44,23 @@ type DMDateTime struct {
|
|||||||
Minute string `json:"minute"`
|
Minute string `json:"minute"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func FetchDepartures(stopId string) (DMResponse, error) {
|
type EFAClient interface {
|
||||||
|
GetName() string
|
||||||
|
BuildRequest(string) (*http.Request, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type VRNEFAClient struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c VRNEFAClient) GetName() string {
|
||||||
|
return "VRN"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c VRNEFAClient) BuildRequest(stopId string) (*http.Request, error) {
|
||||||
// Create request object
|
// Create request object
|
||||||
req, err := http.NewRequest("GET", "https://www.vrn.de/mngvrn/XML_DM_REQUEST", nil)
|
req, err := http.NewRequest("GET", "https://www.vrn.de/mngvrn/XML_DM_REQUEST", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return DMResponse{}, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Configure our request
|
// Configure our request
|
||||||
@ -65,6 +78,45 @@ func FetchDepartures(stopId string) (DMResponse, error) {
|
|||||||
query.Set("useRealtime", "1")
|
query.Set("useRealtime", "1")
|
||||||
req.URL.RawQuery = query.Encode()
|
req.URL.RawQuery = query.Encode()
|
||||||
|
|
||||||
|
return req, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type KVVEFAClient struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c KVVEFAClient) GetName() string {
|
||||||
|
return "KVV"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c KVVEFAClient) BuildRequest(stopId string) (*http.Request, error) {
|
||||||
|
form := url.Values{}
|
||||||
|
form.Set("action", "XSLT_DM_REQUEST")
|
||||||
|
form.Set("name_dm", stopId)
|
||||||
|
form.Set("type_dm", "stop")
|
||||||
|
form.Set("useRealtime", "1")
|
||||||
|
form.Set("limit", "10")
|
||||||
|
form.Set("mode", "direct")
|
||||||
|
form.Set("outputFormat", "json")
|
||||||
|
body := strings.NewReader(form.Encode())
|
||||||
|
|
||||||
|
req, err := http.NewRequest("POST", "https://www.kvv.de/tunnelEfaDirect.php", body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
req.Header.Set("User-Agent", "coolio/1.0")
|
||||||
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||||
|
|
||||||
|
return req, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func FetchDepartures(stopId string) (DMResponse, error) {
|
||||||
|
var c EFAClient = KVVEFAClient{}
|
||||||
|
req, err := c.BuildRequest(stopId)
|
||||||
|
if err != nil {
|
||||||
|
return DMResponse{}, err
|
||||||
|
}
|
||||||
|
|
||||||
// Send the request, wait max 10 seconds
|
// Send the request, wait max 10 seconds
|
||||||
client := http.Client{
|
client := http.Client{
|
||||||
Timeout: 10 * time.Second,
|
Timeout: 10 * time.Second,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user