示例#1
0
        /// <summary>
        /// Generic API request for generic objects
        /// </summary>
        /// <typeparam name="T">Object type to return</typeparam>
        /// <param name="uri">FRC API endpoint</param>
        /// <param name="useIfModifiedSince">boolean for whether or not to use the If-Modified-Since header</param>
        /// <returns></returns>
        private T handleAPIRequest <T>(string uri, bool useIfModifiedSince = true)
        {
            T apiObj;

            try
            {
                if (!System.IO.File.Exists(HelperDataStructures.convertUriToFileName(uri, ".bin")))
                {
                    //if there isn't a cached file, we need to force an API update to create one.
                    useIfModifiedSince = false;
                }
                string api_response = sendAndGetRawResponse(uri, useIfModifiedSince);

                if (api_response != null)
                {
                    apiObj = JsonConvert.DeserializeObject <T>(api_response);
                    HelperDataStructures.WriteObjectToFile <T>(HelperDataStructures.convertUriToFileName(uri, ".bin"), apiObj);
                    return(apiObj);
                }
                else
                {
                    return(default(T));
                }
            }
            catch (WebException ex)
            {
                if (((HttpWebResponse)((WebException)ex.InnerException).Response).StatusCode == HttpStatusCode.NotModified)
                {
                    //no changes were made, so load from cache
                    try
                    {
                        apiObj = HelperDataStructures.ReadObjectFromFile <T>(HelperDataStructures.convertUriToFileName(uri, ".bin"));
                        return(apiObj);
                    }
                    catch (System.IO.FileNotFoundException fnfe)
                    {
                        throw new Exception("No cached object found.", fnfe);
                    }
                }
                else
                {
                    throw ex;
                }
            }
        }