new.go 853 B

1234567891011121314151617181920212223242526272829303132333435
  1. // SPDX-FileCopyrightText: Adam Evyčędo
  2. //
  3. // SPDX-License-Identifier: AGPL-3.0-or-later
  4. package gtfs_rt
  5. import (
  6. pb "apiote.xyz/p/szczanieckiej/gtfs_rt/transit_realtime"
  7. "fmt"
  8. "io"
  9. "net/http"
  10. "time"
  11. "google.golang.org/protobuf/proto"
  12. )
  13. func GetMessages(feedID string, feedURL string) (*pb.FeedMessage, error) {
  14. var message *pb.FeedMessage
  15. client := http.Client{Timeout: 5 * time.Second}
  16. response, err := client.Get(feedURL)
  17. if err != nil {
  18. return nil, fmt.Errorf("cannot download from ‘%s’: %w", feedURL, err)
  19. }
  20. message = new(pb.FeedMessage)
  21. bytes, err := io.ReadAll(response.Body)
  22. if err != nil {
  23. return nil, fmt.Errorf("cannot read response for ‘%s’: %w", feedURL, err)
  24. }
  25. if err := proto.Unmarshal(bytes, message); err != nil {
  26. return nil, fmt.Errorf("Failed to parse message: %w", err)
  27. }
  28. return message, nil
  29. }