예제 #1
0
        public static XmlDocument GetXmlTransaction(string uri, bool forceNoCache = false,
                                                    bool silent = false, bool waitWindow = true)
        {
            //allows multi-threaded operations
            if (waitWindow)
            {
                //offload to another thread if specified
                return((XmlDocument)WaitWindow.WaitWindow.Show(GetXmlTransaction, @"Fetching from API", uri,
                                                               forceNoCache, silent));
            }

            //Create the cache folder structure
            CachingHelpers.CacheStructureBuilder();

            //check if it's already cached
            if (XmlCaching.XmlInCache(uri) && !forceNoCache)
            {
                try
                {
                    //load from the cache
                    var xmlCache = XmlCaching.XmlFromCache(uri);

                    //return the cached XML if not null, otherwise force a re-download
                    return(xmlCache ?? GetXmlTransaction(uri, true, silent, false));
                }
                catch (Exception ex)
                {
                    //record the error
                    LoggingHelpers.RecordException(ex.Message, "CacheLoadError");

                    //force a re-download
                    return(GetXmlTransaction(uri, true, silent, false));
                }
            }

            //default secret account token
            var secret = !string.IsNullOrWhiteSpace(ObjectProvider.Settings.ConnectionInfo.PlexAccountToken)
                ? ObjectProvider.Settings.ConnectionInfo.PlexAccountToken
                : ObjectProvider.User.authenticationToken;

            //allows specific server connection matching for the correct token
            var uriToken = string.IsNullOrEmpty(secret)
                ? Methods.MatchUriToToken(uri, ObjectProvider.PlexServers)
                : secret;

            //the API URI is combined with the token to yield the fully-qualified URI
            var fullUri = $@"{uri}{uriToken}";

            try
            {
                //get the resource
                var xmlString = ResourceGrab.GrabString(fullUri);

                //validation
                if (!string.IsNullOrWhiteSpace(xmlString))
                {
                    //conversion
                    var xmlResponse = xmlString.ToXmlDocument();

                    //log outcome
                    LoggingHelpers.RecordTransaction(fullUri, ResourceGrab.LastStatusCode);

                    //ensure file is cached
                    XmlCaching.XmlToCache(xmlResponse, uri);

                    //return XML document
                    return(xmlResponse);
                }
            }
            catch (ThreadAbortException)
            {
                //literally nothing; this gets raised when a cancellation happens.
            }
            catch (Exception ex)
            {
                LoggingHelpers.RecordException(ex.Message, "XMLTransactionError");
                LoggingHelpers.RecordTransaction(fullUri, "Undetermined");
                if (!silent)
                {
                    UIMessages.Error("Error Occurred in XML Transaction\n\n" + ex, @"Network Error");
                }
            }

            //default
            return(new XmlDocument());
        }