public override bool SendResponse(Request e)
		{
			var request = new ApiRequest
				{
					Client = new ApiSocketWrapper(e.Client),
					HttpMethod = e.HttpMethod,
					Url = e.Url,
					Headers = e.Headers,
					GetArguments = e.GetArguments,
					Body = new ApiReadBodyWrapper(e.Body)
				};
			return _apiService.SendResponse(request);
		}
 public bool SendResponse(ApiRequest request)
 {
     _garbage.Collect();
     try
     {
         var url = new UrlSplitter(request.Url);
         HandleStuff(request, url);
     }
     catch (Exception ex)
     {
         request.Client.Send500_Failure(ex.ToString());
     }
     _garbage.Collect();
     return true;
 }
		public void BeforeEachTest()
		{
			_readBody = new Mock<IApiReadBody>();
			_socket = new Mock<IApiSocket>();
			_config = new Mock<IConfigurationService>();
			_garbage = new Mock<IGarbage>();

			_req = new ApiRequest
				{
					Headers = new Hashtable(),
					GetArguments = new Hashtable(),
					Body = _readBody.Object,
					HttpMethod = "GET",
					Url = "auth/",
					Client = _socket.Object,
				};
			_sut = new AuthApiService(_config.Object, _garbage.Object);
		}
		public void BeforeEachTest()
		{
			_projectOne = new ProjectModel("proj-1", "Project One", "Subtitle One", 1, BuildServiceProvider.AppVeyor);
			_projectTwo = new ProjectModel("proj-2", "Project Two", "Subtitle Two", 2, BuildServiceProvider.TeamCity);
			_jsonOne = JsonSerializer.SerializeObject(ConfigHashifier.Hashify(_projectOne));

			_readBody = new FakeApiReadBody();
			_socket = new Mock<IApiSocket>();
			_config = new Mock<IConfigurationService>();
			_garbage = new Mock<IGarbage>();

			_req = new ApiRequest
				{
					Headers = new Hashtable(),
					GetArguments = new Hashtable(),
					Body = _readBody,
					HttpMethod = "GET",
					Url = "projects/",
					Client = _socket.Object,
				};
			_sut = new ConfigApiService(_config.Object, _garbage.Object);
		}
        private void HandleDefault(ApiRequest request)
        {
            if (request.HttpMethod == "GET")
            {
                var projects = _configurationService.GetProjects();
                var bytes = ConfigHashifier.Bytify(projects);
                request.Client.Send200_OK("application/json", bytes.Length);
                request.Client.Send(bytes, bytes.Length);
                return;
            }

            if (request.HttpMethod == "PUT")
            {
                var proj = new ProjectModel();
                UnpackProject(request, proj);
                proj.Slug = "";
                _configurationService.SaveProject(proj);
                request.Client.Send200_OK("application/json");
                return;
            }

            request.Client.Send405_MethodNotAllowed();
        }
        private void HandleStuff(ApiRequest request, UrlSplitter url)
        {
            if (url.Endpoint == "projects")
            {
                if (url.Id == "")
                {
                    HandleDefault(request);
                    return;
                }

                if (url.Option == "")
                {
                    HandleProject(url.Id, request);
                    return;
                }

                if (url.Option == "build" && url.Moar == "")
                {
                    HandleBuild(url.Id, request);
                    return;
                }
            }
            request.Client.Send404_NotFound();
        }
 public bool CanRespond(ApiRequest request)
 {
     return request.Url.StartsWith("projects");
 }
 private void PutOneBuild(string slug, ApiRequest request)
 {
     var buffer = new byte[BufferSize];
     var countBytes = ShortBodyReader.ReadBody(request.Body, buffer);
     var chars = Encoding.UTF8.GetChars(buffer, 0, countBytes);
     var json = new string(chars);
     var build = JsonSerializer.DeserializeString(json) as Hashtable;
     _configurationService.SaveBuildParams(slug, build);
     request.Client.Send200_OK("application/json");
 }
 private void GetOneBuid(string slug, ApiRequest request)
 {
     var build = _configurationService.GetBuildParams(slug);
     var json = JsonSerializer.SerializeObject(build);
     var bytes = Encoding.UTF8.GetBytes(json);
     request.Client.Send200_OK("application/json", bytes.Length);
     request.Client.Send(bytes, bytes.Length);
 }
		public bool SendResponse(ApiRequest request)
		{
			return false;
		}
 private void DeleteOneProject(string slug, ApiRequest request)
 {
     try
     {
         _configurationService.DeleteProject(slug);
         request.Client.Send200_OK("application/json");
     }
     catch (ProjectDoesNotExistException)
     {
         request.Client.Send404_NotFound();
     }
     catch (Exception)
     {
         request.Client.Send400_BadRequest();
     }
 }
        private static void UnpackProject(ApiRequest request, ProjectModel proj)
        {
            var buffer = new byte[BufferSize];
            var countBytes = ShortBodyReader.ReadBody(request.Body, buffer);
            var chars = Encoding.UTF8.GetChars(buffer, 0, countBytes);
            var json = new string(chars);
            var project = JsonSerializer.DeserializeString(json) as Hashtable;
            if (project == null) return;

            proj.Title = project["title"] as string;
            proj.Subtitle = project["subtitle"] as string;
            proj.Rank = Int32.Parse(project["rank"].ToString());
            proj.Provider = (BuildServiceProvider) Int32.Parse(project["provider"].ToString());
        }
 private void PutOneProject(string slug, ApiRequest request)
 {
     try
     {
         var proj = _configurationService.GetProject(slug);
         UnpackProject(request, proj);
         _configurationService.SaveProject(proj);
         request.Client.Send200_OK("application/json");
     }
     catch (ProjectDoesNotExistException)
     {
         request.Client.Send404_NotFound();
     }
     catch (Exception)
     {
         request.Client.Send400_BadRequest();
     }
 }
 private void GetOneProject(string slug, ApiRequest request)
 {
     try
     {
         var proj = _configurationService.GetProject(slug);
         var bytes = ConfigHashifier.Bytify(proj);
         request.Client.Send200_OK("application/json", bytes.Length);
         request.Client.Send(bytes, bytes.Length);
     }
     catch (ProjectDoesNotExistException)
     {
         request.Client.Send404_NotFound();
     }
 }
        private void HandleProject(string slug, ApiRequest request)
        {
            if (request.HttpMethod == "GET")
            {
                GetOneProject(slug, request);
                return;
            }

            if (request.HttpMethod == "PUT")
            {
                PutOneProject(slug, request);
                return;
            }

            if (request.HttpMethod == "DELETE")
            {
                DeleteOneProject(slug, request);
                return;
            }

            request.Client.Send405_MethodNotAllowed();
        }
        private void HandleBuild(string slug, ApiRequest request)
        {
            if (request.HttpMethod == "GET")
            {
                GetOneBuid(slug, request);
                return;
            }

            if (request.HttpMethod == "PUT")
            {
                PutOneBuild(slug, request);
                return;
            }
            request.Client.Send405_MethodNotAllowed();
        }
		public bool CanRespond(ApiRequest request)
		{
			return request.Url.StartsWith("auth");
		}