public Guid UpdloadFileForSharing(string filename, string apiKey, string projectName /*,string ownerEmail*/) { if (string.IsNullOrWhiteSpace(filename)) { throw new ArgumentNullException("filename"); } if (string.IsNullOrWhiteSpace(apiKey)) { throw new ArgumentNullException("apiKey"); } if (string.IsNullOrWhiteSpace(projectName)) { throw new ArgumentNullException("projectName"); } // if (string.IsNullOrWhiteSpace(ownerEmail)) { throw new ArgumentNullException("ownerEmail"); } if (!File.Exists(filename)) { throw new FileNotFoundException(filename); } string baseUrl = Settings.Default.LocanServiceBaseUrl; string urlForSharing = string.Format( "{0}/{1}", baseUrl, Consts.UrlAddPhrasesForTranslation); Guid userId = this.GetUserIdForApiKey(apiKey); LocanWebFile webFile = this.GetTranslationFile(apiKey, filename, userId, projectName); JavaScriptSerializer jsonSerializer = new JavaScriptSerializer(); byte[] bytes = Encoding.UTF8.GetBytes(jsonSerializer.Serialize(webFile)); using (HttpClient client = new HttpClient()) using (MemoryStream stream = new MemoryStream(bytes)){ StreamContent streamContent = new StreamContent(stream); HttpResponseMessage response = client.Post(urlForSharing, streamContent); response.EnsureSuccessStatusCode(); string guidString = response.Content.ReadAsString(); // Result looks like: <?xml version="1.0" encoding="utf-8"?><guid>2158e8e5-ae6c-4b9a-a7ab-3169fff9750d</guid> XDocument doc = XDocument.Parse(guidString); return(new Guid(doc.Root.Value)); } }
public Guid AddPhrasesForTranslation(HttpRequestMessage request) { // From the POST message we need to pick off the following properties // UserId // FileId // Array of TranslateKey / StringToTranslate / Comment string fileContents = request.Content.ReadAsString(); LocanWebFile webFile = LocanWebFile.BuildFrom(fileContents); LocanDal dal = new LocanDal(); Guid fileId = dal.GetOrCreateIdForFile(webFile.UserId, webFile.Filename, webFile.ProjectName); // delete all old phrases dal.DeleteAllPhrasesfor(fileId); // now add the phrases dal.AddPhrasesToFile(fileId, webFile.Rows); return(fileId); }
internal LocanWebFile GetTranslationFile(string apiKey, string filename, Guid userId, string projectName) { if (string.IsNullOrWhiteSpace(apiKey)) { throw new ArgumentNullException("apiKey"); } if (string.IsNullOrWhiteSpace(filename)) { throw new ArgumentNullException("filename"); } if (string.IsNullOrWhiteSpace(projectName)) { throw new ArgumentNullException("projectName"); } if (!File.Exists(filename)) { throw new FileNotFoundException("File passed to GetTranslationFile not found", filename); } List <LocanRow> rows = new List <LocanRow>(); using (ILocanReader reader = LocanReaderWriterFactory.Instance.GetReader(new { filepath = filename })) { reader.GetRowsToBeTranslated().ToList().ForEach(row => { rows.Add(row as LocanRow); }); } LocanWebFile file = new LocanWebFile { ApiKey = apiKey, Rows = rows, Filename = filename, UserId = userId, ProjectName = projectName }; return(file); }