예제 #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Applikasjonen viser bysykkelstasjonene i Oslo. For hver stasjon vises siste status for ledige sykler og låser.");
            Console.WriteLine();

            Console.WriteLine("Lim inn gyldig API nøkkelen for å få tilgang og trykk ENTER:");
            string apiKey = Console.ReadLine();

            Console.WriteLine("Henter bysykkelstasjonene i Oslo...");
            Console.WriteLine();

            var bysykkelApi          = new BysykkelWebApi(apiKey, "https://oslobysykkel.no/api/v1/");
            BikeStationResult result = bysykkelApi.GetBikeStationsAsync().Result;

            foreach (var bikeStation in result.BikeStations)
            {
                Console.WriteLine(bikeStation);
            }

            if (result.BikeStations.Count == 0)
            {
                Console.WriteLine("Ingen oversikt over bysyklene er mulig å vise for øyeblikket.");
                if (!string.IsNullOrEmpty(result.Message))
                {
                    Console.WriteLine($"\tFeilårsak: {result.Message}");
                }
            }
            else
            {
                Console.WriteLine($"Hentet status for {result.BikeStations.Count} sykkelstasjoner, {result.UpdatedAt.ToLocalTime()}");
            }

            Console.ReadLine();
        }
예제 #2
0
        public async Task <BikeStationResult> GetBikeStationsAsync()
        {
            var bikeStationResult = new BikeStationResult();

            try
            {
                var httpClient = new HttpClient()
                {
                    BaseAddress = new Uri(_apiBaseAddress)
                };
                httpClient.DefaultRequestHeaders.Add("Client-Identifier", _apiKey);

                HttpResponseMessage response = await httpClient.GetAsync("stations");

                if (response.IsSuccessStatusCode == false)
                {
                    bikeStationResult.Message = response.StatusCode.ToString();
                    return(bikeStationResult);
                }

                string stationsResult = await response.Content.ReadAsStringAsync();

                StationsInOperation stationsInOperation = GetData <StationsInOperation>(stationsResult);

                response = await httpClient.GetAsync("stations/availability");

                string stationsAvailabilityResult = await response.Content.ReadAsStringAsync();

                StationsAvailability stationsAvailability = GetData <StationsAvailability>(stationsAvailabilityResult);

                return(BikeStationMatcher.MergeStations(stationsInOperation, stationsAvailability));
            }
            catch (Exception)
            {
                bikeStationResult.Message = "En uventet feil skjedde!";
                return(bikeStationResult);
            }
        }