public ApiResponse(ApiRequest request)
 {
     try
     {
         var response = GetResponse(() => { return request.GetResponse(); });
     }
     catch (WebException ex)
     {
         var response = GetResponse(() => { return ex.Response; });
     }
 }
        public void Version()
        {
            var apiRequest = new ApiRequest(
                method: HttpMethod.GET,
                url: Constants.VersionUrl,
                apiKey: Constants.ApiKey);

            var apiResponse = new ApiResponse(apiRequest);

            Assert.IsNotNull(apiResponse.Body);
        }
        public void ReadContainer()
        {
            var apiRequest = new ApiRequest(
                method: HttpMethod.GET,
                url: Constants.RootContainerUrl,
                apiKey: Constants.ApiKey,
                accessToken: Constants.AccessToken);

            var apiResponse = new ApiResponse(apiRequest);

            Assert.IsNotNull(apiResponse.Body);
        }
        public static ApiResponse GetEventsResponse()
        {
            var apiRequest = new ApiRequest(
                method: HttpMethod.GET,
                url: string.Format("{0}?&order=ascending&offset=0&limit={1}", Constants.EventsUrl, 6),
                apiKey: Constants.ApiKey,
                accessToken: Constants.AccessToken);

            var apiResponse = new ApiResponse(apiRequest);

            return apiResponse;
        }
        public static ApiResponse GetNewsItemResponse()
        {
            var apiRequest = new ApiRequest(
                method: HttpMethod.GET,
                url: string.Format("{0}?parentid={1}&order=descending&offset=0&limit={2}", Constants.BlogPostsUrl, 9, 6),
                apiKey: Constants.ApiKey,
                accessToken: Constants.AccessToken);

            var apiResponse = new ApiResponse(apiRequest);

            return apiResponse;
        }
        public void Authenticate()
        {
            var apiRequest = new ApiRequest(
                method: HttpMethod.GET,
                url: Constants.AuthenticationUrl,
                apiKey: Constants.ApiKey,
                username: Constants.Username,
                password: Constants.Password);

            var apiResponse = new ApiResponse(apiRequest);

            Assert.IsNotNull(apiResponse.Body);
        }
        public void CreateChildContainer()
        {
            var apiRequest = new ApiRequest(
                method: HttpMethod.POST,
                url: Constants.RootContainerUrl,
                apiKey: Constants.ApiKey,
                accessToken: Constants.AccessToken,
                contentType: Constants.ContentTypeXml,
                body: "<CreateChildContainerRequest><Container><Name>Test Child Container</Name><Status>Published</Status></Container></CreateChildContainerRequest>");

            var apiResponse = new ApiResponse(apiRequest);

            Assert.IsNotNull(apiResponse.Body);
        }
        public void CreateBlog()
        {
            string body = string.Format(
                "<CreateBlogRequest><Blog><Author>{0}</Author><Name>Test Blog</Name><ParentId>{1}</ParentId><Status>Published</Status></Blog></CreateBlogRequest>",
                Constants.AdminUserId,
                ChildContainerId);

            var apiRequest = new ApiRequest(
                method: HttpMethod.POST,
                url: Constants.BlogsUrl,
                apiKey: Constants.ApiKey,
                accessToken: Constants.AccessToken,
                contentType: Constants.ContentTypeXml,
                body: body);

            var apiResponse = new ApiResponse(apiRequest);

            Assert.IsNotNull(apiResponse.Body);
        }
        public void CreateEvent()
        {
            string body = string.Format(
                "<CreateEventRequest><Event><Author>{0}</Author><Description><![CDATA[<p>Test Event Description</p>]]></Description><EndDate><DateTime>2012-06-01T12:00:00</DateTime><OffsetMinutes>0</OffsetMinutes></EndDate><Location><![CDATA[<p>Test Location</p>]]></Location><ParentId>{1}</ParentId><Recurrence>None</Recurrence><StartDate><DateTime>2012-06-01T11:00:00</DateTime><OffsetMinutes>0</OffsetMinutes></StartDate><Status>Published</Status><Title>Test Event</Title><Url>http://www.ingagenetworks.com/</Url></Event></CreateEventRequest>",
                Constants.AdminUserId,
                Constants.RootContainerId);

            var apiRequest = new ApiRequest(
                method: HttpMethod.POST,
                url: Constants.EventsUrl,
                apiKey: Constants.ApiKey,
                accessToken: Constants.AccessToken,
                contentType: Constants.ContentTypeXml,
                body: body);

            var apiResponse = new ApiResponse(apiRequest);

            Assert.IsNotNull(apiResponse.Body);
        }
        public void ListBlogPosts()
        {
            var apiRequest = new ApiRequest(
                method: HttpMethod.GET,
                url: string.Format("{0}?parentid={1}&order=descending", Constants.BlogPostsUrl, BlogId),
                apiKey: Constants.ApiKey,
                accessToken: Constants.AccessToken);

            var apiResponse = new ApiResponse(apiRequest);

            Assert.IsNotNull(apiResponse.Body);
        }
        public void CreateBlogPost()
        {
            string body = string.Format(
                "<CreateBlogPostRequest><BlogPost><Author>{0}</Author><BlogId>{1}</BlogId><Body><![CDATA[<p>Blog post body</p>]]></Body><Status>Published</Status><Title>Test Blog Post</Title></BlogPost></CreateBlogPostRequest>",
                Constants.AdminUserId,
                BlogId);

            var apiRequest = new ApiRequest(
                method: HttpMethod.POST,
                url: Constants.BlogPostsUrl,
                apiKey: Constants.ApiKey,
                accessToken: Constants.AccessToken,
                contentType: Constants.ContentTypeXml,
                body: body);

            var apiResponse = new ApiResponse(apiRequest);

            Assert.IsNotNull(apiResponse.Body);
        }
        public ActionResult JsonCreateBlogPost(BlogPost model)
        {
            if (ModelState.IsValid)
            {
                var imageHtml = HtmlCleaner.GetFirstImgNodeHtml(model.Body);
                var imageUrl = HtmlCleaner.GetFirstImgNodeSrc(model.Body);

                if (string.IsNullOrWhiteSpace(model.Title))
                {
                    model.Title = HtmlCleaner.GetFirstH1NodeText(model.Body);
                }

                model.Title = model.Title.CondenseWhiteSpaceRuns();
                var firstPass = HtmlCleaner.RemoveNodes(model.Body, "//div[@class='quote']");
                var cleanHtml = HtmlCleaner.CleanHtml(firstPass, new string []{}, new string[]{}, new[] { "script", "a", "em" }).Replace("&nbsp;", " ");
                cleanHtml = cleanHtml.CondenseWhiteSpaceRuns();
                model.Body = "<div>" + imageHtml + cleanHtml.TrimTextToMaxLength(270) + "</div>";

                model.Title = model.Title.TrimTextToMaxLength(97);
                model.Title = model.Title.ReplaceInvalidCharacters();
                model.Body = model.Body.ReplaceInvalidCharacters();
                model.ImageUrl = imageUrl;

                var body = string.Format(
                    "<CreateBlogPostRequest><BlogPost><Author>{0}</Author><BlogId>{1}</BlogId><Body><![CDATA[{2}]]></Body><Status>Published</Status><Title>{3}</Title></BlogPost></CreateBlogPostRequest>",
                    Constants.AdminUserId,
                    Constants.BlogId,
                    model.Body,
                    model.Title);

                var apiRequest = new ApiRequest(
                    method: HttpMethod.POST,
                    url: Constants.BlogPostsUrl,
                    apiKey: Constants.ApiKey,
                    accessToken: Constants.AccessToken,
                    accept: Constants.ContentTypeJson,
                    contentType: Constants.ContentTypeXml,
                    body: body);

                var apiResponse = new ApiResponse(apiRequest);
                var responseMessage = apiResponse.Body.DeserializeJson<ResponseMessage>();
                responseMessage.StatusCode = apiResponse.StatusCodeValue;

                UpdateClients();
                
                return Json(responseMessage);
            }

            // If we got this far, something failed
            return Json(new { errors = GetErrorsFromModelState() });
        }
        public ActionResult JsonCreateEvent(Event model)
        {
            if (ModelState.IsValid)
            {
                DateTime startDate;

                if (!DateTime.TryParse(model.Date, out startDate))
                    startDate = DateTime.Now;

                var endDate = startDate.AddHours(1);

                string body = string.Format(
                    "<CreateEventRequest><Event><Author>{0}</Author><Description><![CDATA[<p>{1}</p>]]></Description><EndDate><DateTime>{2:yyyy-MM-ddTHH:mm:ss}</DateTime><OffsetMinutes>-240</OffsetMinutes></EndDate><Location><![CDATA[<p>{3}</p>]]></Location><ParentId>{4}</ParentId><Recurrence>None</Recurrence><StartDate><DateTime>{5:yyyy-MM-ddTHH:mm:ss}</DateTime><OffsetMinutes>-240</OffsetMinutes></StartDate><Status>Published</Status><Title>{6}</Title><Url>http://www.ingagenetworks.com/</Url></Event></CreateEventRequest>",
                    Constants.AdminUserId,
                    model.Title,
                    endDate,
                    model.Location,
                    Constants.RootContainerId,
                    startDate,
                    model.Title);

                var apiRequest = new ApiRequest(
                    method: HttpMethod.POST,
                    url: Constants.EventsUrl,
                    apiKey: Constants.ApiKey,
                    accessToken: Constants.AccessToken,
                    contentType: Constants.ContentTypeXml,
                    body: body);

                var apiResponse = new ApiResponse(apiRequest);
                var responseMessage = new ResponseMessage { Message = apiResponse.Body, StatusCode = apiResponse.StatusCodeValue };
                
                UpdateClients();

                return Json(responseMessage);
            }

            // If we got this far, something failed
            return Json(new { errors = GetErrorsFromModelState() });
        }