コード例 #1
0
        public async Task <TransportApiResult <IEnumerable <Line> > > GetLines(ITokenComponent tokenComponent, TransportApiClientSettings settings, CancellationToken ct, IEnumerable <string> onlyAgencies, IEnumerable <string> omitAgencies, IEnumerable <TransportMode> limitModes, IEnumerable <string> servesStops, DateTime?at, string boundingBox, double latitude, double longitude, int radiusInMeters = -1, int limit = 100, int offset = 0, string exclude = null)
        {
            var result = new TransportApiResult <IEnumerable <Line> >();

            if (limit > maxLimit || limit < 0)
            {
                result.Error = $"Invalid limit. Valid values are positive numbers up to {maxLimit}.";

                return(result);
            }
            if (onlyAgencies != null && omitAgencies != null && onlyAgencies.Any() && omitAgencies.Any())
            {
                result.Error = "Either onlyAgencies or omitAgencies can be provided. Not both.";

                return(result);
            }

            var token = await tokenComponent.GetAccessToken();

            if (token == null)
            {
                result.Error = tokenComponent.DefaultErrorMessage;

                return(result);
            }

            var client = Client(settings.Timeout);

            var request = GetRequest("lines", token);

            if (omitAgencies != null && omitAgencies.Any())
            {
                request.AddParameter("agencies", string.Join(",", omitAgencies.Select(r => string.Concat('~', r))));
            }
            if (onlyAgencies != null && onlyAgencies.Any())
            {
                request.AddParameter("agencies", string.Join(",", onlyAgencies));
            }
            if (limitModes != null && limitModes.Any())
            {
                request.AddParameter("modes", string.Join(",", limitModes));
            }
            if (servesStops != null && servesStops.Any())
            {
                request.AddParameter("servesStops", string.Join(",", servesStops));
            }
            if (limit != 100)
            {
                request.AddParameter("limit", limit.ToString(CultureInfo.InvariantCulture));
            }
            if (offset > 0)
            {
                request.AddParameter("offset", offset.ToString(CultureInfo.InvariantCulture));
            }
            if (!double.IsNaN(latitude) && !double.IsNaN(longitude))
            {
                request.AddParameter("point", latitude.ToString(CultureInfo.InvariantCulture) + "," + longitude.ToString(CultureInfo.InvariantCulture));
            }
            if (!string.IsNullOrWhiteSpace(boundingBox))
            {
                request.AddParameter("bbox", boundingBox);
            }
            if (at != null)
            {
                request.AddParameter("at", at.Value.ToString("o"));
            }
            if (radiusInMeters > 0)
            {
                request.AddParameter("radius", radiusInMeters.ToString(CultureInfo.InvariantCulture));
            }
            if (!string.IsNullOrWhiteSpace(exclude))
            {
                request.AddParameter("exclude", exclude);
            }

            try
            {
                IRestResponse <List <Line> > restResponse = await client.ExecuteTaskAsync <List <Line> >(request, ct);

                result.StatusCode = restResponse.StatusCode;

                if (restResponse.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    result.IsSuccess = true;
                    result.Data      = restResponse.Data;
                }
                else
                {
                    result.Error = ((RestResponseBase)restResponse).Content;
                }
            }
            catch (Exception e)
            {
                result.Error = e.Message;
            }

            return(result);
        }
コード例 #2
0
        public async Task <TransportApiResult <Itinerary> > GetJourneyItinerary(ITokenComponent tokenComponent, TransportApiClientSettings settings, CancellationToken ct, string journeyId, string itineraryId, DateTime?at, string exclude = null)
        {
            var result = new TransportApiResult <Itinerary>();

            if (string.IsNullOrWhiteSpace(journeyId))
            {
                result.Error = "Journey Id is required.";

                return(result);
            }

            if (string.IsNullOrWhiteSpace(itineraryId))
            {
                result.Error = "Itinerary Id is required.";

                return(result);
            }

            var token = await tokenComponent.GetAccessToken();

            if (token == null)
            {
                result.Error = tokenComponent.DefaultErrorMessage;

                return(result);
            }

            var client = Client(settings.Timeout);

            var request = GetRequest($"journeys/{journeyId}/itineraries/{itineraryId}", token);

            if (at != null)
            {
                request.AddParameter("at", at.Value.ToString("o"));
            }
            if (!string.IsNullOrWhiteSpace(exclude))
            {
                request.AddParameter("exclude", exclude);
            }

            try
            {
                IRestResponse <Itinerary> restResponse = await client.ExecuteTaskAsync <Itinerary>(request, ct);

                result.StatusCode = restResponse.StatusCode;

                if (restResponse.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    result.IsSuccess = true;
                    result.Data      = restResponse.Data;
                }
                else
                {
                    result.Error = ((RestResponseBase)restResponse).Content;
                }
            }
            catch (Exception e)
            {
                result.Error = e.Message;
            }

            return(result);
        }
コード例 #3
0
        public async Task <TransportApiResult <Journey> > PostJourney(ITokenComponent tokenComponent, TransportApiClientSettings settings, CancellationToken ct, IEnumerable <string> fareProducts, IEnumerable <string> onlyAgencies, IEnumerable <string> omitAgencies, IEnumerable <TransportMode> onlyModes, IEnumerable <TransportMode> omitModes, double startLatitude, double startLongitude, double endLatitude, double endLongitude, DateTime?time, TimeType timeType = TimeType.DepartAfter, Profile profile = Profile.ClosestToTime, int maxItineraries = 3, string exclude = null)
        {
            var result = new TransportApiResult <Journey>();

            if (maxItineraries < 1 || maxItineraries > 5)
            {
                result.Error = "Invalid value for maxItineraries. Expected a value between or including 1 and 5.";

                return(result);
            }

            var token = await tokenComponent.GetAccessToken();

            if (token == null)
            {
                result.Error = tokenComponent.DefaultErrorMessage;

                return(result);
            }

            string timeIso8601 = null;

            if (time != null)
            {
                timeIso8601 = time.Value.ToString("o");
            }

            JourneyInputModel jsonBody = new JourneyInputModel()
            {
                Time           = timeIso8601,
                TimeType       = timeType.ToString(),
                FareProducts   = fareProducts,
                MaxItineraries = maxItineraries,
                Profile        = profile.ToString(),
                Omit           = new JourneyOmitInputModel()
                {
                    Agencies = omitAgencies,
                    Modes    = omitModes
                },
                Only = new JourneyOnlyInputModel()
                {
                    Agencies = onlyAgencies,
                    Modes    = onlyModes
                },
                Geometry = new GeoJsonLineString()
                {
                    Type        = GeoJsonType.MultiPoint,
                    Coordinates = new List <List <string> >()
                    {
                        new List <string>()
                        {
                            startLongitude.ToString(CultureInfo.InvariantCulture), startLatitude.ToString(CultureInfo.InvariantCulture)
                        },
                        new List <string>()
                        {
                            endLongitude.ToString(CultureInfo.InvariantCulture), endLatitude.ToString(CultureInfo.InvariantCulture)
                        }
                    }
                }
            };

            var client = Client(settings.Timeout);

            string path = "journeys";

            if (!string.IsNullOrWhiteSpace(exclude))
            {
                path += "?exclude=" + exclude;
            }

            var request = PostRequest(path, token);

            request.AddJsonBody(jsonBody);

            try
            {
                IRestResponse <Journey> restResponse = await client.ExecuteTaskAsync <Journey>(request, ct);

                result.StatusCode = restResponse.StatusCode;

                if (restResponse.StatusCode == System.Net.HttpStatusCode.Created)
                {
                    result.IsSuccess = true;
                    result.Data      = restResponse.Data;
                }
                else
                {
                    result.Error = ((RestResponseBase)restResponse).Content;
                }
            }
            catch (Exception e)
            {
                result.Error = e.Message;
            }

            return(result);
        }
コード例 #4
0
        public async Task <TransportApiResult <IEnumerable <FareTable> > > GetFareTables(ITokenComponent tokenComponent, TransportApiClientSettings settings, CancellationToken ct, string id, DateTime?at, int limit = 100, int offset = 0, string exclude = null)
        {
            var result = new TransportApiResult <IEnumerable <FareTable> >();

            if (limit > maxLimit || limit < 0)
            {
                result.Error = $"Invalid limit. Valid values are positive numbers up to {maxLimit}.";

                return(result);
            }

            var token = await tokenComponent.GetAccessToken();

            if (token == null)
            {
                result.Error = tokenComponent.DefaultErrorMessage;

                return(result);
            }

            var client = Client(settings.Timeout);

            var request = GetRequest($"fareproducts/{id}/faretables", token);

            if (at != null)
            {
                request.AddParameter("at", at.Value.ToString("o"));
            }
            if (limit != 100)
            {
                request.AddParameter("limit", limit.ToString(CultureInfo.InvariantCulture));
            }
            if (offset > 0)
            {
                request.AddParameter("offset", offset.ToString(CultureInfo.InvariantCulture));
            }
            if (!string.IsNullOrWhiteSpace(exclude))
            {
                request.AddParameter("exclude", exclude);
            }

            try
            {
                IRestResponse <List <FareTable> > restResponse = await client.ExecuteTaskAsync <List <FareTable> >(request, ct);

                result.StatusCode = restResponse.StatusCode;

                if (restResponse.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    result.IsSuccess = true;
                    result.Data      = restResponse.Data;
                }
                else
                {
                    result.Error = ((RestResponseBase)restResponse).Content;
                }
            }
            catch (Exception e)
            {
                result.Error = e.Message;
            }

            return(result);
        }