Esempio n. 1
0
        private T Deserialize <T>(string response) where T : class, new()
        {
            Type type = typeof(T);

            if (mimeFormat == MimeFormat.json)
            {
                var jsonRoot = (string)null;
                if (type == typeof(IssueCategory))
                {
                    jsonRoot = RedmineKeys.ISSUE_CATEGORY;
                }
                if (type == typeof(IssueRelation))
                {
                    jsonRoot = RedmineKeys.RELATION;
                }
                if (type == typeof(TimeEntry))
                {
                    jsonRoot = RedmineKeys.TIME_ENTRY;
                }
                if (type == typeof(ProjectMembership))
                {
                    jsonRoot = RedmineKeys.MEMBERSHIP;
                }
                if (type == typeof(WikiPage))
                {
                    jsonRoot = RedmineKeys.WIKI_PAGE;
                }

                return(RedmineSerialization.JsonDeserialize <T>(response, jsonRoot));
            }

            return(RedmineSerialization.FromXML <T>(response));
        }
Esempio n. 2
0
        private T Deserialize <T>(string response) where T : class, new()
        {
            Type type = typeof(T);

            if (mimeFormat == MimeFormat.json)
            {
                var jsonRoot = (string)null;
                if (type == typeof(IssueCategory))
                {
                    jsonRoot = "issue_category";
                }
                if (type == typeof(IssueRelation))
                {
                    jsonRoot = "relation";
                }
                if (type == typeof(TimeEntry))
                {
                    jsonRoot = "time_entry";
                }

                return(RedmineSerialization.JsonDeserialize <T>(response, jsonRoot));
            }

            return(RedmineSerialization.FromXML <T>(response));
        }
Esempio n. 3
0
        private IList <T> DeserializeList <T>(string response, string jsonRoot, out int totalCount) where T : class, new()
        {
            Type type = typeof(T);

            if (mimeFormat == MimeFormat.json)
            {
                if (type == typeof(IssuePriority))
                {
                    jsonRoot = RedmineKeys.ISSUE_PRIORITIES;
                }
                if (type == typeof(TimeEntryActivity))
                {
                    jsonRoot = RedmineKeys.TIME_ENTRY_ACTIVITIES;
                }
                return(RedmineSerialization.JsonDeserializeToList <T>(response, jsonRoot, out totalCount));
            }

            using (var text = new StringReader(response))
            {
                using (var xmlReader = new XmlTextReader(text))
                {
                    xmlReader.WhitespaceHandling = WhitespaceHandling.None;
                    xmlReader.Read();
                    xmlReader.Read();

                    totalCount = xmlReader.ReadAttributeAsInt("total_count");

                    return(xmlReader.ReadElementContentAsCollection <T>());
                }
            }
        }
Esempio n. 4
0
        private IList <T> DeserializeList <T>(
            string response,
            string jsonRoot,
            out int totalCount) where T : class, new()
        {
            var type = typeof(T);

            if (_mimeFormat == MimeFormat.JSON)
            {
                if (type == typeof(IssuePriority))
                {
                    jsonRoot = "issue_priorities";
                }
                if (type == typeof(TimeEntryActivity))
                {
                    jsonRoot = "time_entry_activities";
                }
                return(RedmineSerialization.JsonDeserializeToList <T>(
                           response,
                           jsonRoot,
                           out totalCount));
            }
            using (var text = new StringReader(response))
            {
                using (var xmlReader = new XmlTextReader(text))
                {
                    xmlReader.WhitespaceHandling = WhitespaceHandling.None;
                    xmlReader.Read();
                    xmlReader.Read();
                    totalCount = xmlReader.ReadAttributeAsInt("total_count");
                    return(xmlReader.ReadElementContentAsCollection <T>());
                }
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Creates a new Redmine object.
        /// </summary>
        /// <typeparam name="T">The type of object to create.</typeparam>
        /// <param name="obj">The object to create.</param>
        /// <param name="ownerId"></param>
        /// <remarks>When trying to create an object with invalid or missing attribute parameters, you will get a 422 Unprocessable Entity response. That means that the object could not be created.</remarks>
        /// <exception cref="Redmine.Net.Api.RedmineException"></exception>
        /// <code>
        /// <example>
        ///     var project = new Project();
        ///     project.Name = "test";
        ///     project.Identifier = "the project identifier";
        ///     project.Description = "the project description";
        ///     redmineManager.CreateObject(project);
        /// </example>
        /// </code>
        public T CreateObject <T>(T obj, string ownerId) where T : class, new()
        {
            var type = typeof(T);

            if (!urls.ContainsKey(type))
            {
                return(null);
            }

            var result = Serialize(obj);

            if (string.IsNullOrEmpty(result))
            {
                return(null);
            }

            using (var wc = CreateWebClient(null))
            {
                try
                {
                    string response;
                    if (type == typeof(Version) || type == typeof(IssueCategory) || type == typeof(ProjectMembership))
                    {
                        if (string.IsNullOrEmpty(ownerId))
                        {
                            throw new RedmineException("The owner id(project id) is mandatory!");
                        }
                        response = wc.UploadString(string.Format(EntityWithParentFormat, host, "projects", ownerId, urls[type], mimeFormat), result);
                    }
                    else
                    if (type == typeof(IssueRelation))
                    {
                        if (string.IsNullOrEmpty(ownerId))
                        {
                            throw new RedmineException("The owner id(issue id) is mandatory!");
                        }
                        response = wc.UploadString(string.Format(EntityWithParentFormat, host, "issues", ownerId, urls[type], mimeFormat), result);
                    }
                    else
                    {
                        response = wc.UploadString(string.Format(Format, host, urls[type], mimeFormat), result);
                    }
                    //  #if RUNNING_ON_35_OR_ABOVE
                    if (mimeFormat == MimeFormat.json)
                    {
                        return(type == typeof(IssueCategory)
                            ? RedmineSerialization.JsonDeserialize <T>(response, "issue_category")
                            : RedmineSerialization.JsonDeserialize <T>(response, type == typeof(IssueRelation) ? "relation" : null));
                    }
                    // #endif
                    return(RedmineSerialization.FromXML <T>(response));
                }
                catch (WebException webException)
                {
                    HandleWebException(webException, type.Name);
                }
            }
            return(null);
        }
Esempio n. 6
0
 private string Serialize <T>(T obj) where T : class, new()
 {
     if (mimeFormat == MimeFormat.json)
     {
         return(RedmineSerialization.JsonSerializer(obj));
     }
     return(RedmineSerialization.ToXML(obj));
 }
Esempio n. 7
0
        private void ShowAsyncResult(string response, Type responseType, RedmineMethod method, string jsonRoot)
        {
            var aev = new AsyncEventArgs();

            try
            {
                // #if RUNNING_ON_35_OR_ABOVE
                if (mimeFormat == MimeFormat.json)
                {
                    if (method == RedmineMethod.GetObjectList)
                    {
                        int totalItems;
                        aev.Result     = RedmineSerialization.JsonDeserializeToList(response, jsonRoot, responseType, out totalItems);
                        aev.TotalItems = totalItems;
                    }
                    else
                    {
                        aev.Result = RedmineSerialization.JsonDeserialize(response, responseType, null);
                    }
                }
                //#
                else
                if (method == RedmineMethod.GetObjectList)
                {
                    using (var text = new StringReader(response))
                    {
                        using (var xmlReader = new XmlTextReader(text))
                        {
                            xmlReader.WhitespaceHandling = WhitespaceHandling.None;
                            xmlReader.Read();
                            xmlReader.Read();

                            aev.TotalItems = xmlReader.ReadAttributeAsInt("total_count");

                            aev.Result = xmlReader.ReadElementContentAsCollection(responseType);
                        }
                    }
                }
                else
                {
                    aev.Result = RedmineSerialization.FromXML(response, responseType);
                }
                //#endif
            }
            catch (ThreadAbortException ex)
            {
                aev.Error = ex.Message;
            }
            catch (Exception ex)
            {
                aev.Error = ex.Message;
            }
            if (DownloadCompleted != null)
            {
                DownloadCompleted(this, aev);
            }
        }
Esempio n. 8
0
 private T Deserialize <T>(string response) where T : class, new()
 {
     //  #if RUNNING_ON_35_OR_ABOVE
     if (mimeFormat == MimeFormat.json)
     {
         return(RedmineSerialization.JsonDeserialize <T>(response, null));
     }
     // #endif
     return(RedmineSerialization.FromXML <T>(response));
 }
 private string Serialize <T>(T obj) where T : class, new()
 {
     //  #if RUNNING_ON_35_OR_ABOVE
     if (mimeFormat == MimeFormat.json)
     {
         return(RedmineSerialization.JsonSerializer(obj));
     }
     //#else
     return(RedmineSerialization.ToXML(obj));
     //  #endif
 }
        private T Deserialize <T>(string response) where T : class, new()
        {
            Type type = typeof(T);

            if (mimeFormat == MimeFormat.json)
            {
                return(type == typeof(IssueCategory)
                    ? RedmineSerialization.JsonDeserialize <T>(response, "issue_category")
                    : RedmineSerialization.JsonDeserialize <T>(response, type == typeof(IssueRelation) ? "relation" : null));
            }

            return(RedmineSerialization.FromXML <T>(response));
        }
        private T Deserialize <T>(string response) where T : class, new()
        {
            Type type = typeof(T);

            if (mimeFormat == MimeFormat.json)
            {
                return(type == typeof(IssueCategory)
                    ? RedmineSerialization.JsonDeserialize <T>(response, "issue_category")
                    : RedmineSerialization.JsonDeserialize <T>(response, type == typeof(IssueRelation) ? "relation" : null));
            }

            return(RedmineSerialization.FromXML <T>(response));

            //using (var stringReader = new StringReader(response))
            //{
            //    var deserializer = new RedmineSerialization.XmlStreamingDeserializer<T>(stringReader);
            //    return deserializer.Deserialize();
            //}
        }
Esempio n. 12
0
        public T DeserializeResult <T>(
            string result,
            out int totalItems,
            string jsonRoot = null)
        {
            totalItems = 0;
            if (string.IsNullOrWhiteSpace(result))
            {
                return(default(T));
            }
            var type = typeof(T);

            if (type == typeof(List <T>))
            {
                if (_mimeFormat == MimeFormat.JSON)
                {
                    return
                        ((T)
                         RedmineSerialization.JsonDeserializeToList(
                             result,
                             jsonRoot,
                             type,
                             out totalItems));
                }
                using (var text = new StringReader(result))
                {
                    using (var xmlReader = new XmlTextReader(text))
                    {
                        xmlReader.WhitespaceHandling = WhitespaceHandling.None;
                        xmlReader.Read();
                        xmlReader.Read();
                        totalItems = xmlReader.ReadAttributeAsInt("total_count");
                        return((T)(object)xmlReader.ReadElementContentAsCollection(type));
                    }
                }
            }
            if (_mimeFormat == MimeFormat.JSON)
            {
                return((T)RedmineSerialization.JsonDeserialize(result, type, jsonRoot));
            }
            return((T)RedmineSerialization.FromXml(result, type));
        }
        private IList <T> DeserializeList <T>(string response, string jsonRoot, out int totalCount) where T : class, new()
        {
            if (mimeFormat == MimeFormat.json)
            {
                return(RedmineSerialization.JsonDeserializeToList <T>(response, jsonRoot, out totalCount));
            }

            using (var text = new StringReader(response))
            {
                using (var xmlReader = new XmlTextReader(text))
                {
                    xmlReader.WhitespaceHandling = WhitespaceHandling.None;
                    xmlReader.Read();
                    xmlReader.Read();

                    totalCount = xmlReader.ReadAttributeAsInt("total_count");

                    return(xmlReader.ReadElementContentAsCollection <T>());
                }
            }
        }
Esempio n. 14
0
        /// <summary>
        /// Returns the user whose credentials are used to access the API.
        /// </summary>
        /// <param name="parameters">The accepted parameters are: memberships and groups (added in 2.1).</param>
        /// <returns></returns>
        /// <exception cref="System.InvalidOperationException"> An error occurred during deserialization. The original exception is available
        /// using the System.Exception.InnerException property.</exception>
        public User GetCurrentUser(NameValueCollection parameters = null)
        {
            using (var wc = CreateWebClient(parameters))
            {
                try
                {
                    var response = wc.DownloadString(string.Format(RequestFormat, host, urls[typeof(User)], CurrentUserUri, mimeFormat));

                    //#if RUNNING_ON_35_OR_ABOVE
                    if (mimeFormat == MimeFormat.json)
                    {
                        return(RedmineSerialization.JsonDeserialize <User>(response, null));
                    }
                    //#endif
                    return(RedmineSerialization.FromXML <User>(response));
                }
                catch (WebException webException)
                {
                    HandleWebException(webException, "WikiPage");
                }
                return(null);
            }
        }
Esempio n. 15
0
        /// <summary>
        /// Upload data on server.
        /// </summary>
        /// <param name="data">Data which will be uploaded on server</param>
        /// <returns>Returns 'Upload' object with inialized 'Token' by server response.</returns>
        public Upload UploadData(byte[] data)
        {
            using (var wc = CreateUploadWebClient(null))
            {
                try
                {
                    var response       = wc.UploadData(string.Format(Format, host, "uploads", mimeFormat), data);
                    var responseString = Encoding.ASCII.GetString(response);
#if RUNNING_ON_35_OR_ABOVE
                    if (mimeFormat == MimeFormat.json)
                    {
                        return(RedmineSerialization.JsonDeserialize <Upload>(responseString));
                    }
#endif
                    return(RedmineSerialization.FromXML <Upload>(responseString));
                }
                catch (WebException webException)
                {
                    HandleWebException(webException, "Upload");
                }
            }

            return(null);
        }
Esempio n. 16
0
 /// <summary>
 /// Downloads the user whose credentials are used to access the API. This method does not block the calling thread.
 /// </summary>
 /// <returns>Returns the Guid associated with the async request.</returns>
 /// <exception cref="System.InvalidOperationException"> An error occurred during deserialization. The original exception is available
 /// using the System.Exception.InnerException property.</exception>
 /// <summary>
 /// Returns the details of a wiki page or the details of an old version of a wiki page if the <b>version</b> parameter is set.
 /// </summary>
 /// <param name="projectId">The project id or identifier.</param>
 /// <param name="parameters">
 ///     attachments
 ///     The accepted parameters are: memberships and groups (added in 2.1).
 /// </param>
 /// <param name="pageName">The wiki page name.</param>
 /// <param name="version">The version of the wiki page.</param>
 /// <returns></returns>
 public WikiPage GetWikiPage(string projectId, NameValueCollection parameters, string pageName, uint version = 0)
 {
     using (var wc = CreateWebClient(parameters))
     {
         try
         {
             var response = wc.DownloadString(version == 0
                                                         ? string.Format(WikiPageFormat, host, projectId, pageName, mimeFormat)
                                                         : string.Format(WikiVersionFormat, host, projectId, pageName, version, mimeFormat));
             //   #if RUNNING_ON_35_OR_ABOVE
             if (mimeFormat == MimeFormat.json)
             {
                 return(RedmineSerialization.JsonDeserialize <WikiPage>(response, null));
             }
             // #endif
             return(RedmineSerialization.FromXML <WikiPage>(response));
         }
         catch (WebException webException)
         {
             HandleWebException(webException, "WikiPage");
         }
         return(null);
     }
 }
Esempio n. 17
0
 private T Deserialize <T>(string response) where T : class, new()
 {
     return(RedmineSerialization.FromXML <T>(response));
 }
Esempio n. 18
0
 private string Serialize <T>(T obj) where T : class, new()
 {
     return(RedmineSerialization.ToXML(obj));
 }