Example #1
0
        public static TranslationExport Export(OneSkyService service, int projectId, string sourceFilename, CultureInfo culture, Stream destinationStream)
        {
            //https://github.com/onesky/api-documentation-platform/blob/master/resources/translation.md
            Debug.Assert(!String.IsNullOrWhiteSpace(sourceFilename));
            Debug.Assert(culture != null);

            string url = service.AddAuthenticationParameters(String.Format("https://platform.api.onesky.io/1/projects/{0}/translations", projectId));

            url += "&locale=" + LocaleCodeHelper.ConvertToLocaleCode(culture.Name) + "&source_file_name=" + sourceFilename;

            var req = (HttpWebRequest)WebRequest.Create(url);

            req.Method = "GET";
            using (HttpWebResponse Response = (HttpWebResponse)req.GetResponse())
            {
                if (Response.StatusCode == HttpStatusCode.Accepted)
                {
                    return(TranslationExport.Accepted);
                }
                else if (Response.StatusCode == HttpStatusCode.NoContent)
                {
                    return(TranslationExport.NoContent);
                }

                using (Stream rawResponseStream = Response.GetResponseStream())
                {
                    rawResponseStream.CopyTo(destinationStream);
                }
                return(TranslationExport.Completed);
            }
        }
        public static UploadFileResponse HttpWebRequestUploadFile(OneSkyService service, string url, string file, string paramName, Stream fileStream, string contentType)
        {
            string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");

            byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");

            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);

            req.ContentType = "multipart/form-data; boundary=" + boundary;
            req.Method      = "POST";
            req.KeepAlive   = true;

            using (var DataStream = new BinaryWriter(req.GetRequestStream(), System.Text.Encoding.UTF8))
            {
                DataStream.Write(boundarybytes, 0, boundarybytes.Length);

                string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
                string header         = string.Format(headerTemplate, paramName, file, contentType);
                byte[] headerbytes    = System.Text.Encoding.UTF8.GetBytes(header);
                DataStream.Write(headerbytes, 0, headerbytes.Length);


                byte[] buffer    = new byte[4096];
                int    bytesRead = 0;
                while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
                {
                    DataStream.Write(buffer, 0, bytesRead);
                }

                byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
                DataStream.Write(trailer, 0, trailer.Length);
                DataStream.Flush();
                DataStream.Close();
            }

            try
            {
                using (HttpWebResponse Response = (HttpWebResponse)req.GetResponse())
                {
                    using (Stream rawResponseStream = Response.GetResponseStream())
                    {
                        var responseText = service.StreamToString(rawResponseStream);
                        if (!string.IsNullOrWhiteSpace(responseText))
                        {
                            using (var responseStream = service.StringToStream(responseText))
                            {
                                var jsonSerializer = new DataContractJsonSerializer(typeof(UploadFileResponse));
                                return((UploadFileResponse)jsonSerializer.ReadObject(responseStream));
                            }
                        }
                    }
                }
            }
            catch (WebException)
            {
            }
            return(null);
        }
Example #3
0
        public static ListProjectLanguagesResponse ListLanguages(OneSkyService service, int projectId)
        {
            //https://github.com/onesky/api-documentation-platform/blob/master/resources/project.md

            string url = service.AddAuthenticationParameters(String.Format("https://platform.api.onesky.io/1/projects/{0}/languages", projectId));

            using (var client = new WebClient())
                using (var stream = client.OpenRead(url))
                {
                    var jsonSerializer = new DataContractJsonSerializer(typeof(ListProjectLanguagesResponse));
                    return((ListProjectLanguagesResponse)jsonSerializer.ReadObject(stream));
                }
        }
        public static ListUploadedFilesResponse List(OneSkyService service, int projectId, int startPage, int itemsPerPage)
        {
            //https://github.com/onesky/api-documentation-platform/blob/master/resources/file.md#upload---upload-a-file
            Debug.Assert(startPage >= 1);
            Debug.Assert(itemsPerPage >= 1);

            string url = service.AddAuthenticationParameters(String.Format("https://platform.api.onesky.io/1/projects/{0}/files", projectId));

            url += "&page=" + startPage + "&per_page=" + itemsPerPage;

            using (var client = new WebClient())
                using (var stream = client.OpenRead(url))
                {
                    var jsonSerializer = new DataContractJsonSerializer(typeof(ListUploadedFilesResponse));
                    return((ListUploadedFilesResponse)jsonSerializer.ReadObject(stream));
                }
        }
Example #5
0
        public static CreateProjectGroupResponse Create(OneSkyService service, string name, CultureInfo culture)
        {
            //https://github.com/onesky/api-documentation-platform/blob/master/resources/project_group.md
            string url        = "https://platform.api.onesky.io/1/project-groups";
            string parameters = service.GetAuthenticationParameters() + "&name=" + name + "&locale=" + LocaleCodeHelper.ConvertToLocaleCode(culture.Name);

            url += "?" + parameters;

            using (var client = new WebClient())
            {
                var jsonSerializer = new DataContractJsonSerializer(typeof(CreateProjectGroupResponse));
                using (var stream = service.StringToStream(client.UploadString(url, parameters)))
                {
                    return((CreateProjectGroupResponse)jsonSerializer.ReadObject(stream));
                }
            }
        }
Example #6
0
        public static CreateProjectResponse Create(OneSkyService service, int projectGroupId, string name, string description, string projectType)
        {
            //https://github.com/onesky/api-documentation-platform/blob/master/resources/project.md

            string url        = String.Format("https://platform.api.onesky.io/1/project-groups/{0}/projects", projectGroupId);
            string parameters = service.GetAuthenticationParameters() + "&name=" + name + "&description=" + description + "&project_type=" + projectType;

            url += "?" + parameters;

            using (var client = new WebClient())
            {
                var jsonSerializer = new DataContractJsonSerializer(typeof(CreateProjectResponse));
                using (var stream = service.StringToStream(client.UploadString(url, parameters)))
                {
                    return((CreateProjectResponse)jsonSerializer.ReadObject(stream));
                }
            }
        }
Example #7
0
        public static bool Delete(OneSkyService service, int projectId)
        {
            //https://github.com/onesky/api-documentation-platform/blob/master/resources/project.md

            string url = service.AddAuthenticationParameters(string.Format("https://platform.api.onesky.io/1/projects/{0}", projectId));

            try
            {
                using (var client = new WebClient())
                {
                    client.UploadString(url, "DELETE", "");
                }
                return(true);
            }
            catch (WebException)
            {
                return(false);
            }
        }
        public static Task <UploadFileResponse> Upload(OneSkyService service, int projectId, string filename, Stream stream, string cultureName)
        {
            //https://github.com/onesky/api-documentation-platform/blob/master/resources/file.md#upload---upload-a-file
            Debug.Assert(stream != null);
            Debug.Assert(cultureName != null);

            string url = service.AddAuthenticationParameters(String.Format("https://platform.api.onesky.io/1/projects/{0}/files", projectId));

            string fileFormat = GetFileFormat(Path.GetExtension(filename));

            if (fileFormat == null)
            {
                return(null);
            }

            url += "&file_format=" + fileFormat + "&locale=" + cultureName + "&is_keeping_all_strings=false";

            return(Task.Factory.StartNew(() => {
                return HttpWebRequestUploadFile(service, url, filename, "file", stream, fileFormat);
            }));
        }
Example #9
0
        public static bool Update(OneSkyService service, int projectId, string name, string description)
        {
            //https://github.com/onesky/api-documentation-platform/blob/master/resources/project.md

            string url        = String.Format("https://platform.api.onesky.io/1/projects/{0}", projectId);
            string parameters = service.GetAuthenticationParameters() + "&name=" + name + "&description=" + description;

            url += "?" + parameters;

            try
            {
                using (var client = new WebClient())
                {
                    client.UploadString(url, "PUT", parameters);
                }
                return(true);
            }
            catch (WebException)
            {
                return(false);
            }
        }
Example #10
0
        public static Task Status(OneSkyService service, int projectId, string filename, CultureInfo culture)
        {
            return(Task.Factory.StartNew((obj) =>
            {
                //https://github.com/onesky/api-documentation-platform/blob/master/resources/translation.md
                Debug.Assert(!String.IsNullOrWhiteSpace(filename));
                Debug.Assert(culture != null);

                string url = service.AddAuthenticationParameters(String.Format("https://platform.api.onesky.io/1/projects/{0}/translations/status", projectId));
                url += "&locale=" + LocaleCodeHelper.ConvertToLocaleCode(culture.Name) + "&file_name=" + filename;

                var state = (TranslationExportStatus)obj;
                TranslationStatusResponse response;

                using (var client = new WebClient())
                    using (var stream = client.OpenRead(url))
                    {
                        var jsonSerializer = new DataContractJsonSerializer(typeof(TranslationStatusResponse));
                        response = (TranslationStatusResponse)jsonSerializer.ReadObject(stream);
                    }

                state.Progress = response.Data.Progress;

                while (response.Meta.Status == 200 && response.Data.Progress != "100%")
                {
                    Thread.Sleep(5000); // Wait 5 seconds
                    state.Progress = response.Data.Progress;

                    using (var client = new WebClient())
                        using (var stream = client.OpenRead(url))
                        {
                            var jsonSerializer = new DataContractJsonSerializer(typeof(TranslationStatusResponse));
                            response = (TranslationStatusResponse)jsonSerializer.ReadObject(stream);
                        }
                }
            }, new TranslationExportStatus()));
        }