/// <summary>
        /// Instantiate a Box specific exception from a given HTTP response
        /// </summary>
        /// <param name="message">The message from the SDK about what happened</param>
        /// <param name="response">The HTTP response that generated the exception</param>
        public static BoxException GetResponseException <T>(string message, IBoxResponse <T> response) where T : class
        {
            BoxError error = null;

            if (!string.IsNullOrWhiteSpace(response.ContentString))
            {
                var converter = new BoxJsonConverter();

                try
                {
                    error = converter.Parse <BoxError>(response.ContentString);
                }
                catch (Exception e)
                {
                    Debug.WriteLine(string.Format("Unable to parse error message: {0} ({1})", response.ContentString, e.Message));
                }
            }
            var ex = new BoxException(GetErrorMessage(message, response, error))
            {
                StatusCode      = response.StatusCode,
                ResponseHeaders = response.Headers,
                Error           = error
            };

            return(ex);
        }
Exemple #2
0
        protected virtual List <T> ReadFile <T, M>(string path) where T : class, new()
        {
            var fileFormat = this.ProcessFileFormatFromPath(path);

            if (fileFormat == _settings.FILE_FORMAT_JSON)
            {
                var jsonString = File.ReadAllText(path);
                var converter  = new BoxJsonConverter();
                var collection = converter.Parse <BoxCollection <T> >(jsonString);
                return(collection.Entries.ToList());
            }
            else if (fileFormat == _settings.FILE_FORMAT_CSV)
            {
                using (var fs = File.OpenText(path))
                    using (var csv = new CsvReader(fs))
                    {
                        csv.Configuration.RegisterClassMap(typeof(M));
                        return(csv.GetRecords <T>().ToList());
                    }
            }
            else
            {
                throw new Exception($"File format {fileFormat} is not currently supported.");
            }
        }
        protected virtual List <BoxMetadataTemplate> ReadMetadataTemplateJsonFile(string filePathTemplates)
        {
            var jsonString = File.ReadAllText(filePathTemplates);
            var converter  = new BoxJsonConverter();
            var collection = converter.Parse <BoxEnterpriseMetadataTemplateCollection <BoxMetadataTemplate> >(jsonString);

            return(collection.Entries.ToList());
        }
        static async Task ProcessMessagesAsync(Message message, CancellationToken token)
        {
            // Process the message.
            BoxJsonConverter bjc = new BoxJsonConverter();

            var sequence     = message.SystemProperties.SequenceNumber;
            var boxEventJson = Encoding.UTF8.GetString(message.Body);
            var boxEvent     = bjc.Parse <BoxEnterpriseEvent>(boxEventJson);

            Console.WriteLine($"Received message: SequenceNumber={sequence}, Box Event Type={boxEvent.EventType}");

            // Complete the message so that it is not received again.
            // This can be done only if the subscriptionClient is created in ReceiveMode.PeekLock mode (which is the default).
            await subscriptionClient.CompleteAsync(message.SystemProperties.LockToken);

            // Note: Use the cancellationToken passed as necessary to determine if the subscriptionClient has already been closed.
            // If subscriptionClient has already been closed, you can choose to not call CompleteAsync() or AbandonAsync() etc.
            // to avoid unnecessary exceptions.
        }
        protected internal static BoxError GetResponseError <T>(IBoxResponse <T> response) where T : class
        {
            BoxError error = null;

            if (!string.IsNullOrWhiteSpace(response.ContentString))
            {
                var converter = new BoxJsonConverter();

                try
                {
                    error = converter.Parse <BoxError>(response.ContentString);
                }
                catch (Exception e)
                {
                    Debug.WriteLine(string.Format("Unable to parse error message: {0} ({1})", response.ContentString, e.Message));
                }
            }

            return(error);
        }