protected void Trace(RequestBuilder utils, ResponseInfo response) { if (RestSettings.Instance.RestTracing == true) { StringBuilder sb = new StringBuilder(this.RestTrace); sb.AppendLine(); sb.AppendLine(utils.Dump()); sb.AppendLine(); sb.AppendLine("Response:"); sb.AppendLine(response.ResponseText); sb.AppendLine(); this.RestTrace = sb.ToString(); } }
/// <summary> /// Parses response Json to retrieve pertinent information /// </summary> /// <param name="response">ResponseInfo object</param> private void ParseCreateResponse(ResponseInfo response) { if (response.StatusCode == HttpStatusCode.Created) { JObject json = JObject.Parse(response.ResponseText); this.EnvelopeId = (string)json["envelopeId"]; this.Url = (string)json["uri"]; } }
/// <summary> /// Parses Json response containing errors /// </summary> /// <param name="response">ResponseInfo object</param> protected void ParseErrorResponse(ResponseInfo response) { try { this.RestError = Error.FromJson(response.ResponseText); } catch { this.RestError = new Error { message = response.ResponseText }; } this.RestError.httpStatusCode = response.StatusCode; }
/// <summary> /// Parses Json response to a create action /// </summary> /// <param name="response">ResponseInfo object</param> private void ParseCreateResponse(ResponseInfo response) { if (response.StatusCode == HttpStatusCode.Created) { JObject json = JObject.Parse(response.ResponseText); if (json != null) { string acct = (string)json["accountIdGuid"]; this.AccountIdGuid = new Guid(acct); this.AccountId = (string)json["accountId"]; this.ApiPassword = (string)json["apiPassword"]; this.UserId = (string)json["userId"]; this.BaseUrl = (string)json["baseUrl"]; } } }
/// <summary> /// Parses Json response containing errors /// </summary> /// <param name="response">ResponseInfo object</param> private void ParseErrorResponse(ResponseInfo response) { try { this.RestError = Error.FromJson(response.ResponseText); } catch { this.RestError = new Error { message = response.ResponseText }; } }
/// <summary> /// Parses Json response containing errors /// </summary> /// <param name="response">ResponseInfo object</param> private void ParseConsoleResponse(ResponseInfo response) { if (response.StatusCode == HttpStatusCode.Created) { JObject json = JObject.Parse(response.ResponseText); if (json != null) { this.ConsoleUrl = (string)json["url"]; } } }
/// <summary> /// Parses the Json response for a login action /// </summary> /// <param name="response">ResponseInfo object</param> private void ParseLoginResponse(ResponseInfo response) { if (response.StatusCode == HttpStatusCode.OK) { // this is the new way of managing the login accounts this.LoginAccounts = Logins.FromJson(response.ResponseText); // this is the old way so we don't break old clients JObject json = JObject.Parse(response.ResponseText); if (json != null) { this.ApiPassword = (string)json["apiPassword"]; foreach (JObject j in json["loginAccounts"]) { string userId = (string)j["userId"]; string def = (string)j["isDefault"]; if (string.IsNullOrEmpty(this.MatchingUserId) == false) { if (userId == this.MatchingUserId) { CaptureAccountInfo(j); break; } } else { if (def.Equals("true")) { CaptureAccountInfo(j); break; } } } } } }
/// <summary> /// Creates a new template /// </summary> /// <param name="fileBytes">Byte arrays of the files' content - in correct order.</param> /// <param name="fileNames">File names - in correct order</param> /// <returns>true if successful, false otherwise</returns> public bool CreateTemplate(List <byte[]> fileBytesList, List <string> fileNames, string templateName) { if (this.Login == null) { throw new ArgumentNullException("Login"); } if (string.IsNullOrEmpty(this.Login.BaseUrl) == true) { throw new ArgumentNullException("BaseUrl"); } if (string.IsNullOrEmpty(this.Login.ApiPassword) == true) { throw new ArgumentNullException("ApiPassword"); } if (fileNames.Count != fileBytesList.Count) { throw new ArgumentException("Mismatch between number of files names and files' bytes content - they must be the same"); } RequestBuilder builder = new RequestBuilder(); RequestInfo req = new RequestInfo(); List <RequestBody> requestBodies = new List <RequestBody>(); req.RequestContentType = "multipart/form-data"; req.BaseUrl = this.Login.BaseUrl; req.LoginEmail = this.Login.Email; //req.LoginPassword = this.Login.Password; req.ApiPassword = this.Login.ApiPassword; req.Uri = "/templates?api_password=true"; req.HttpMethod = "POST"; req.IntegratorKey = RestSettings.Instance.IntegratorKey; req.IsMultipart = true; req.MultipartBoundary = new Guid().ToString(); builder.Proxy = this.Proxy; RequestBody rb = new RequestBody(); rb.Headers.Add("Content-Type", "application/json"); rb.Headers.Add("Content-Disposition", "form-data"); rb.Text = this.CreateJson(fileNames); rb.Text += " \"envelopeTemplateDefinition\": {\"name\": \"" + templateName + "\"}"; requestBodies.Add(rb); for (int i = 0; i < fileNames.Count; i++) { var fileName = fileNames[i]; RequestBody reqFile = new RequestBody(); string mime = string.IsNullOrEmpty(this.MimeType) == true ? DefaultMimeType : this.MimeType; reqFile.Headers.Add("Content-Type", mime); reqFile.Headers.Add("Content-Disposition", string.Format("file; filename=\"{0}\"; documentId={1}", fileName, i + 1)); reqFile.FileBytes = fileBytesList[i]; reqFile.SubstituteStrings = false; requestBodies.Add(reqFile); } req.RequestBody = requestBodies.ToArray(); builder.Request = req; ResponseInfo response = builder.MakeRESTRequest(); this.Trace(builder, response); if (response.StatusCode == HttpStatusCode.Created) { JObject json = JObject.Parse(response.ResponseText); this.EnvelopeId = (string)json["templateId"]; return(this.GetSenderView(string.Empty)); } else { this.ParseErrorResponse(response); return(false); } }