/// <summary> /// Deserialize the JSON string into a proper object. /// </summary> /// <param name="response">The HTTP response.</param> /// <param name="type">Object type.</param> /// <returns>Object representation of the JSON string.</returns> public object Deserialize(DocuSignResponse response, Type type) { if (type == typeof(byte[])) // return byte array { return(Encoding.UTF8.GetBytes(response.Content)); } if (type == typeof(Stream)) { if (response.Headers != null) { var filePath = String.IsNullOrEmpty(Configuration.TempFolderPath) ? Path.GetTempPath() : Configuration.TempFolderPath; var regex = new Regex(@"Content-Disposition=.*filename=['""]?([^'""\s]+)['""]?$"); foreach (var header in response.Headers) { var match = regex.Match(header.ToString()); if (match.Success) { string fileName = filePath + SanitizeFilename(match.Groups[1].Value.Replace("\"", "").Replace("'", "")); File.WriteAllBytes(fileName, Encoding.UTF8.GetBytes(response.Content)); return(new FileStream(fileName, FileMode.Open)); } } } var stream = new MemoryStream(Encoding.UTF8.GetBytes(response.Content)); return(stream); } if (type.Name.StartsWith("System.Nullable`1[[System.DateTime")) // return a datetime object { return(DateTime.Parse(response.Content, null, System.Globalization.DateTimeStyles.RoundtripKind)); } if (type == typeof(String) || type.Name.StartsWith("System.Nullable")) // return primitive type { return(ConvertType(response.Content, type)); } // at this point, it must be a model (json) try { return(JsonConvert.DeserializeObject(response.Content, type, serializerSettings)); } catch (Exception e) { throw new ApiException(500, e.Message); } }
/// <summary> /// Allows for extending response processing for <see cref="DocuSignClient"/> generated code. /// </summary> /// <param name="request">The <see cref="DocuSignRequest"/> request object</param> /// <param name="response">The <see cref="DocuSignResponse"/> response object</param> public virtual void InterceptResponse(DocuSignRequest request, DocuSignResponse response) { // Override this to add telemetry }
/// <summary> /// Initializes a new instance of the <see cref="ApiException"/> class. /// </summary> /// <param name="errorCode">HTTP status code.</param> /// <param name="message">Error message.</param> /// <param name="errorContent">Error content.</param> /// <param name="response">DocuSignResponse object.</param> public ApiException(int errorCode, string message, dynamic errorContent = null, DocuSignResponse response = null) : base(message) { this.ErrorCode = errorCode; this.ErrorContent = errorContent; if (response != null && response.Headers != null) { this.ErrorMessage = response.ErrorMessage; this.Headers = response.Headers.ToDictionary(x => x.Key, x => x.Value.ToString()); } }