<commit_before>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Web;
using System.Web.Hosting;
using System.Web.Http;
using SimpleWAWS.Code;

namespace SimpleWAWS.Controllers
{
    [RoutePrefix("api/site")]
    public class SiteController : ApiController
    {
        [Route("{siteId}")]
        [HttpGet]
        public async Task<Site> Get(string siteId)
        {
            var siteManager = await SiteManager.GetInstanceAsync();
            return siteManager.GetSite(siteId);
        }

        [Route("~/api/reset")]
        [HttpGet]
        public async Task Reset()
        {
            var siteManager = await SiteManager.GetInstanceAsync();
            await siteManager.ResetAllFreeSites();
        }

        [Route("getpublishingprofile/{siteId}")]
        public async Task<HttpResponseMessage> GetPublishingProfile(string siteId)
        {
            var response = Request.CreateResponse();
            var siteManager = await SiteManager.GetInstanceAsync();
            var site = siteManager.GetSite(siteId);
            response.Content = await site.GetPublishingProfile();
            response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
            response.Content.Headers.ContentDisposition =
                new ContentDispositionHeaderValue("attachment") { FileName = string.Format("{0}.publishsettings", site.Name) };
            return response;
        }

        [HttpPost]
        public async Task<Site> Post([FromBody] Template template)
        {
            var siteManager = await SiteManager.GetInstanceAsync();
            return
                await
                    siteManager.ActivateSiteAsync(template == null
                        ? null
                        : TemplatesManager.GetTemplates()
                            .SingleOrDefault(t => t.Name == template.Name && t.Language == template.Language));
        }

        [Route("{siteId}")]
        [HttpDelete]
        public async Task Delete(string siteId)
        {
            var siteManager = await SiteManager.GetInstanceAsync();
            await siteManager.DeleteSite(siteId);
        }
    }
}<commit_msg>returning http correct error codes to the client in case of a server error<commit_after>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Web.Hosting;
using System.Web.Http;
using SimpleWAWS.Code;

namespace SimpleWAWS.Controllers
{
    [RoutePrefix("api/site")]
    public class SiteController : ApiController
    {
        [Route("{siteId}")]
        [HttpGet]
        public async Task<HttpResponseMessage> Get(string siteId)
        {
            var siteManager = await SiteManager.GetInstanceAsync();
            return Request.CreateResponse(HttpStatusCode.OK, siteManager.GetSite(siteId));
        }

        [Route("~/api/reset")]
        [HttpGet]
        public async Task<HttpResponseMessage> Reset()
        {
            var siteManager = await SiteManager.GetInstanceAsync();
            await siteManager.ResetAllFreeSites();
            return Request.CreateResponse(HttpStatusCode.Accepted);
        }

        [Route("getpublishingprofile/{siteId}")]
        public async Task<HttpResponseMessage> GetPublishingProfile(string siteId)
        {
            var response = Request.CreateResponse();
            var siteManager = await SiteManager.GetInstanceAsync();
            var site = siteManager.GetSite(siteId);
            response.Content = await site.GetPublishingProfile();
            response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
            response.Content.Headers.ContentDisposition =
                new ContentDispositionHeaderValue("attachment") { FileName = string.Format("{0}.publishsettings", site.Name) };
            return response;
        }

        [HttpPost]
        public async Task<HttpResponseMessage> Post([FromBody] Template template)
        {
            try
            {
                var siteManager = await SiteManager.GetInstanceAsync();
                var site = 
                    await
                        siteManager.ActivateSiteAsync(template == null
                            ? null
                            : TemplatesManager.GetTemplates()
                                .SingleOrDefault(t => t.Name == template.Name && t.Language == template.Language));
                return Request.CreateResponse(HttpStatusCode.OK, site);
            }
            catch (Exception ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.ServiceUnavailable, ex.Message);
            }
        }

        [Route("{siteId}")]
        [HttpDelete]
        public async Task<HttpResponseMessage> Delete(string siteId)
        {
            var siteManager = await SiteManager.GetInstanceAsync();
            await siteManager.DeleteSite(siteId);
            return Request.CreateResponse(HttpStatusCode.Accepted);
        }
    }
}
{'fraction_non_alphanumeric': 0.0945515821007302, 'fraction_numerical': 0.0003744617112900206, 'mean_word_length': 2.2316999395039323, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2220480', 'n_tokens_mistral': 1322, 'n_tokens_neox': 1312, 'n_words': 275}