Example #1
0
        public ApiResponse <T> Put <T>(
            string resource,
            object data = null,
            IDictionary <string, string> requestParameters = null,
            IDictionary <string, string> routeParameters   = null,
            IDictionary <string, string> putParameters     = null,
            DataSerializationFormat dataFormat             = DataSerializationFormat.Json) where T : new()
        {
            EnsureAuthenticated();

            var request = BuildPutPostRequest(
                string.Format("{0}/{1}", YouTrackRestResourceBase, resource),
                routeParameters,
                requestParameters,
                putParameters,
                data,
                Method.PUT,
                dataFormat);

            if (data != null)
            {
                request.AddBody(data);
            }

            var response = GetClient().Execute <T>(request);

            EnsureExpectedResponseStatus(response, HttpStatusCode.OK, HttpStatusCode.Accepted, HttpStatusCode.Created);

            return(response.AsApiResponse());
        }
Example #2
0
        public ApiResponse Post(
            string resource,
            object data = null,
            IDictionary <string, string> postParameters    = null,
            IDictionary <string, string> requestParameters = null,
            IDictionary <string, string> routeParameters   = null,
            DataSerializationFormat dataFormat             = DataSerializationFormat.Json)
        {
            EnsureAuthenticated();

            var request = BuildPutPostRequest(
                string.Format("{0}/{1}", YouTrackRestResourceBase, resource),
                routeParameters,
                requestParameters,
                postParameters,
                data,
                dataFormat: dataFormat);

            var response = GetClient().Execute(request);

            EnsureExpectedResponseStatus(response, HttpStatusCode.OK, HttpStatusCode.Created, HttpStatusCode.Accepted);

            return(response.AsApiResponse());
        }
Example #3
0
        private static RestRequest BuildPutPostRequest(
            string resource,
            IDictionary <string, string> routeParameters,
            IDictionary <string, string> requestParameters,
            IDictionary <string, string> postParameters,
            object postBody,
            Method requestMethod = Method.POST,
            DataSerializationFormat dataFormat = DataSerializationFormat.Json)
        {
            if (postBody != null && postParameters != null && postParameters.Any())
            {
                throw new ArgumentException("Pass only non-null postBody or postParameters ... NOT BOTH", "postBody");
            }

            var requestResourceBuilder = new StringBuilder(resource);

            if (requestParameters != null && requestParameters.Any())
            {
                requestResourceBuilder.Append("?");
            }
            var firstParam = true;

            if (requestParameters != null)
            {
                foreach (var kvp in requestParameters)
                {
                    requestResourceBuilder.AppendFormat("{0}{1}={{{1}}}", !firstParam ? "&" : string.Empty, kvp.Key);
                    firstParam = false;
                }
            }

            var request = new RestRequest(requestResourceBuilder.ToString(), requestMethod)
            {
                RequestFormat  = dataFormat == DataSerializationFormat.Json ? DataFormat.Json : DataFormat.Xml,
                JsonSerializer = new NewtonsoftJsonSerializer()
            };

            if (request.RequestFormat == DataFormat.Xml)
            {
                request.XmlSerializer.ContentType = "application/xml; charset=utf-8";
            }

            if (requestParameters != null)
            {
                foreach (var kvp in requestParameters)
                {
                    request.AddUrlSegment(kvp.Key, kvp.Value);
                }
            }

            if (routeParameters != null)
            {
                foreach (var routeParam in routeParameters)
                {
                    request.AddUrlSegment(routeParam.Key, routeParam.Value);
                }
            }

            if (postBody != null)
            {
                request.AddBody(postBody);
            }
            else if (postParameters != null)
            {
                foreach (var p in postParameters)
                {
                    request.AddParameter(p.Key, p.Value);
                }
            }

            return(request);
        }