public static LiveTrafficDataResponse.Response RetrieveLiveTrafficData(string apiKey, string URL)
        {
            LiveTrafficDataResponse.Response responseObject = null;

            LiveTrafficDataAPIClient apiClient = new LiveTrafficDataAPIClient();

            apiClient.apiKey = apiKey;
            apiClient.apiURL = URL;

            try
            {
                var response = apiClient.RequestDataFromAPI();

                if (response != null)
                {
                    // this will be false if an invalid api key is sent through to the api
                    if (response.IsSuccessStatusCode)
                    {
                        string data = response.Content.ReadAsStringAsync().Result;
                        responseObject = (LiveTrafficDataResponse.Response)JsonConvert.DeserializeObject(data, typeof(LiveTrafficDataResponse.Response));
                    }
                }
            }
            catch (Exception ex)
            {
                responseObject = null;
            }
            return(responseObject);
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            ConsoleMessaging.DisplayMessageToUser("Attempting to retrieve camera data from the api.");
            LiveTrafficDataResponse.Response cameraData = LiveTrafficData.RetrieveLiveTrafficData("j1zHbAwDsYNo4sR9FpInRVzIX8698p9JxomI", "https://api.transport.nsw.gov.au/v1/live/cameras");

            // check if null. null means an error occured when talking to the api or deserialising
            // the data
            if (cameraData != null)
            {
                ConsoleMessaging.DisplayMessageToUser("Successfully retrieved camera data from the api.");
                ConsoleMessaging.DisplayMessageToUser("Now converting api data to a csv file.");
                string filePath = CSVFile.GenerateFile(cameraData, "output.csv");
                if (!String.IsNullOrEmpty(filePath))
                {
                    ConsoleMessaging.DisplayMessageToUser("File generated successfully. It can be found at " + filePath);
                }
                else
                {
                    ConsoleMessaging.DisplayMessageToUser("File failed to be created.");
                }
            }
            else
            {
                ConsoleMessaging.DisplayMessageToUser("Something went wrong when retrieving the camera data.");
            }
        }
Ejemplo n.º 3
0
        public static string GenerateFile(LiveTrafficDataResponse.Response apiData, string fileName)
        {
            try
            {
                if (apiData != null && !String.IsNullOrEmpty(fileName))
                {
                    directoryPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "LiveTrafficData");
                    fullFilePath  = Path.Combine(directoryPath, fileName);

                    if (!Directory.Exists(directoryPath))
                    {
                        HelperMethods.CreateDirectory(directoryPath);
                    }

                    if (!File.Exists(fullFilePath))
                    {
                        HelperMethods.CreateFile(fullFilePath);
                    }
                    if (!GenerateCSVFile(apiData))
                    {
                        fullFilePath = null;
                    }
                }
            }
            catch (Exception ex)
            {
                fullFilePath = null;
            }
            return(fullFilePath);
        }
Ejemplo n.º 4
0
        private static bool GenerateCSVFile(LiveTrafficDataResponse.Response apiData)
        {
            bool success = true;

            if (apiData != null && apiData.features != null)
            {
                try
                {
                    string delimiter = ",";
                    using (StreamWriter writer = new StreamWriter(fullFilePath, false))
                    {
                        // write the headers
                        string headerRow = "Region" + delimiter + "Title" + delimiter + "View" + delimiter
                                           + "Direction";

                        writer.WriteLine(headerRow);

                        // write the actual content

                        foreach (LiveTrafficDataResponse.FeatureObject item in apiData.features)
                        {
                            string contentRow = item.properties.region + delimiter + item.properties.title +
                                                delimiter + item.properties.view + delimiter + item.properties.direction;

                            writer.WriteLine(contentRow);
                        }
                    }
                }
                catch (Exception ex)
                {
                    success = false;
                }
            }
            else
            {
                success = false;
            }
            return(success);
        }