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);
		}
Ejemplo n.º 2
0
 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);
		}
Ejemplo n.º 5
0
        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();
        }
Ejemplo n.º 6
0
        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();
        }
Ejemplo n.º 7
0
 public bool CanRespond(ApiRequest request)
 {
     return request.Url.StartsWith("projects");
 }
Ejemplo n.º 8
0
 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");
 }
Ejemplo n.º 9
0
 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);
 }
Ejemplo n.º 10
0
		public bool SendResponse(ApiRequest request)
		{
			return false;
		}
Ejemplo n.º 11
0
 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();
     }
 }
Ejemplo n.º 12
0
        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());
        }
Ejemplo n.º 13
0
 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();
     }
 }
Ejemplo n.º 14
0
 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();
     }
 }
Ejemplo n.º 15
0
        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();
        }
Ejemplo n.º 16
0
        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();
        }
Ejemplo n.º 17
0
		public bool CanRespond(ApiRequest request)
		{
			return request.Url.StartsWith("auth");
		}