/// <summary> /// Uploads a file to zendesk and returns the corresponding token id. /// To upload another file to an existing token just pass in the existing token. /// </summary> /// <param name="file"></param> /// <param name="token"></param> /// <returns></returns> Upload UploadAttachment(ZenFile file, string token = "") { var requestUrl = ZendeskUrl; if (!requestUrl.EndsWith("/")) requestUrl += "/"; requestUrl += string.Format("uploads.json?filename={0}", file.FileName); if (!string.IsNullOrEmpty(token)) requestUrl += string.Format("&token={0}", token); WebRequest req = WebRequest.Create(requestUrl); req.ContentType = file.ContentType; req.Method = "POST"; req.Credentials = new System.Net.NetworkCredential(User, Password); req.Headers["Authorization"] = GetPasswordOrTokenAuthHeader(); var dataStream = req.GetWebRequestStream(); dataStream.Write(file.FileData, 0, file.FileData.Length); WebResponse response = req.GetWebResponse(); dataStream = response.GetResponseStream(); var reader = new StreamReader(dataStream); string responseFromServer = reader.ReadToEnd(); return responseFromServer.ConvertToObject<UploadResult>().Upload; }
/// <summary> /// Uploads a file to zendesk and returns the corresponding token id. /// To upload another file to an existing token just pass in the existing token. /// </summary> /// <param name="file"></param> /// <param name="token"></param> /// <param name="timeout"></param> /// <returns></returns> Upload UploadAttachment(ZenFile file, string token, int? timeout = null) { var resource = string.Format("uploads.json?filename={0}", file.FileName); if (!token.IsNullOrWhiteSpace()) { resource += string.Format("&token={0}", token); } var requestResult = RunRequest<UploadResult>(resource, RequestMethod.Post, file, timeout); return requestResult.Upload; }
/// <summary> /// Uploads a file to zendesk and returns the corresponding token id. /// To upload another file to an existing token just pass in the existing token. /// </summary> /// <param name="file"></param> /// <param name="token"></param> /// <param name="timeout"></param> /// <returns></returns> Upload UploadAttachment(ZenFile file, string token, int? timeout) { var requestUrl = ZendeskUrl; if (!requestUrl.EndsWith("/")) requestUrl += "/"; requestUrl += string.Format("uploads.json?filename={0}", file.FileName); if (!string.IsNullOrEmpty(token)) requestUrl += string.Format("&token={0}", token); WebRequest req = WebRequest.Create(requestUrl); req.ContentType = file.ContentType; req.Method = "POST"; req.ContentLength = file.FileData.Length; req.Headers["Authorization"] = GetPasswordOrTokenAuthHeader(); //If timeout has value set a specific Timeout in the WebRequest if (timeout.HasValue) req.Timeout = timeout.Value; //var credentials = new System.Net.CredentialCache // { // { // new System.Uri(ZendeskUrl), "Basic", // new System.Net.NetworkCredential(User, Password) // } // }; //req.Credentials = credentials; req.PreAuthenticate = true; //req.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequired; var dataStream = req.GetRequestStream(); dataStream.Write(file.FileData, 0, file.FileData.Length); dataStream.Dispose(); WebResponse response = req.GetResponse(); dataStream = response.GetResponseStream(); var reader = new StreamReader(dataStream); string responseFromServer = reader.ReadToEnd(); dataStream.Dispose(); response.Close(); return responseFromServer.ConvertToObject<UploadResult>().Upload; }
/// <summary> /// Uploads a file to zendesk and returns the corresponding token id. /// To upload another file to an existing token just pass in the existing token. /// </summary> /// <param name="file"></param> /// <param name="token"></param> /// <returns></returns> Upload UploadAttachment(ZenFile file, string token = "") { var requestUrl = ZendeskUrl; if (!requestUrl.EndsWith("/")) requestUrl += "/"; requestUrl += string.Format("uploads.json?filename={0}", file.FileName); if (!string.IsNullOrEmpty(token)) requestUrl += string.Format("&token={0}", token); WebRequest req = WebRequest.Create(requestUrl); req.ContentType = file.ContentType; req.Method = "POST"; req.ContentLength = file.FileData.Length; var credentials = new System.Net.CredentialCache { { new System.Uri(ZendeskUrl), "Basic", new System.Net.NetworkCredential(User, Password) } }; req.Credentials = credentials; req.PreAuthenticate = true; //req.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequired; var dataStream = req.GetRequestStream(); dataStream.Write(file.FileData, 0, file.FileData.Length); dataStream.Close(); WebResponse response = req.GetResponse(); dataStream = response.GetResponseStream(); var reader = new StreamReader(dataStream); string responseFromServer = reader.ReadToEnd(); return responseFromServer.ConvertToObject<UploadResult>().Upload; }
public async Task CanSetUserPhotoAsync() { var file = new ZenFile() { ContentType = "image/jpeg", FileName = "gracehoppertocat3.jpg", FileData = File.ReadAllBytes(TestContext.CurrentContext.TestDirectory + "\\gracehoppertocat3.jpg") }; var user = await api.Users.SetUserPhotoAsync(Settings.UserId, file); Assert.That(user.User.Photo.ContentUrl, Is.Not.Null); Assert.That(user.User.Photo.Size, Is.EqualTo(6553)); }
public Upload UploadAttachment(ZenFile file) { return UploadAttachment(file, ""); }
public async Task<Upload> UploadAttachmentAsync(ZenFile file) { return await UploadAttachmentAsync(file, ""); }
/// <summary> /// Uploads a file to zendesk and returns the corresponding token id. /// To upload another file to an existing token just pass in the existing token. /// </summary> /// <param name="file"></param> /// <param name="token"></param> /// <returns></returns> public async Task<Upload> UploadAttachmentAsync(ZenFile file, string token = "") { var requestUrl = ZendeskUrl; if (!requestUrl.EndsWith("/")) requestUrl += "/"; requestUrl += string.Format("uploads.json?filename={0}", file.FileName); if (!string.IsNullOrEmpty(token)) requestUrl += string.Format("&token={0}", token); HttpWebRequest req = WebRequest.Create(requestUrl) as HttpWebRequest; req.ContentType = file.ContentType; //req.Credentials = new System.Net.NetworkCredential(User, Password); req.Headers["Authorization"] = GetPasswordOrTokenAuthHeader(); req.Method = "POST"; //GET POST PUT DELETE req.Accept = "application/json, application/xml, text/json, text/x-json, text/javascript, text/xml"; var requestStream = Task.Factory.FromAsync( req.BeginGetRequestStream, asyncResult => req.EndGetRequestStream(asyncResult), (object)null); var dataStream = await requestStream.ContinueWith(t => t.Result.WriteAsync(file.FileData, 0, file.FileData.Length)); Task.WaitAll(dataStream); Task<WebResponse> task = Task.Factory.FromAsync( req.BeginGetResponse, asyncResult => req.EndGetResponse(asyncResult), (object)null); return await task.ContinueWith(t => { var httpWebResponse = t.Result as HttpWebResponse; return ReadStreamFromResponse(httpWebResponse).ConvertToObject<UploadResult>().Upload; }); }
public Task<IndividualUserResponse> SetUserPhotoAsync(long userId, ZenFile photo) { return GenericPutAsync<IndividualUserResponse>(string.Format("users/{0}.json", userId), photo, "user[photo][uploaded_data]"); }
/// <summary> /// Uploads a file to zendesk and returns the corresponding token id. /// To upload another file to an existing token just pass in the existing token. /// </summary> /// <param name="file"></param> /// <param name="token"></param> /// <param name="timeout"></param> /// <returns></returns> Upload UploadAttachment(ZenFile file, string token, int? timeout) { var requestUrl = ZendeskUrl + string.Format("uploads.json?filename={0}", file.FileName); if (!string.IsNullOrEmpty(token)) requestUrl += string.Format("&token={0}", token); WebRequest req = WebRequest.Create(requestUrl); req.ContentType = file.ContentType; req.Method = "POST"; req.ContentLength = file.FileData.Length; req.Headers["Authorization"] = GetPasswordOrTokenAuthHeader(); //If timeout has value set a specific Timeout in the WebRequest if (timeout.HasValue) req.Timeout = timeout.Value; req.PreAuthenticate = true; var dataStream = req.GetRequestStream(); dataStream.Write(file.FileData, 0, file.FileData.Length); dataStream.Dispose(); WebResponse response = req.GetResponse(); dataStream = response.GetResponseStream(); var reader = new StreamReader(dataStream); string responseFromServer = reader.ReadToEnd(); dataStream.Dispose(); response.Close(); return responseFromServer.ConvertToObject<UploadResult>().Upload; }
public Upload UploadAttachment(ZenFile file, int? timeout) { return UploadAttachment(file, "", timeout); }
public async Task<Upload> UploadAttachmentAsync(ZenFile file, int? timeout = null) { return await UploadAttachmentAsync(file, "", timeout); }
/// <summary> /// Uploads a file to zendesk and returns the corresponding token id. /// To upload another file to an existing token just pass in the existing token. /// </summary> /// <param name="file"></param> /// <param name="token"></param> /// <param name="timeout"></param> /// <returns></returns> public async Task<Upload> UploadAttachmentAsync(ZenFile file, string token = "", int? timeout = null) { string resource = string.Format("uploads.json?filename={0}", file.FileName); if (!token.IsNullOrWhiteSpace()) { resource += string.Format("&token={0}", token); } UploadResult result = await RunRequestAsync<UploadResult>(resource, RequestMethod.Post, file, timeout); return result.Upload; }