{"action":"created","issue":{"url":"https://api.github.com/repos/twitter/finagle/issues/385","labels_url":"https://api.github.com/repos/twitter/finagle/issues/385/labels{/name}","comments_url":"https://api.github.com/repos/twitter/finagle/issues/385/comments","events_url":"https://api.github.com/repos/twitter/finagle/issues/385/events","html_url":"https://github.com/twitter/finagle/issues/385","id":88360334,"number":385,"title":"Service chaining","user":{"login":"raelg","id":344980,"avatar_url":"https://avatars.githubusercontent.com/u/344980?v=3","gravatar_id":"","url":"https://api.github.com/users/raelg","html_url":"https://github.com/raelg","followers_url":"https://api.github.com/users/raelg/followers","following_url":"https://api.github.com/users/raelg/following{/other_user}","gists_url":"https://api.github.com/users/raelg/gists{/gist_id}","starred_url":"https://api.github.com/users/raelg/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/raelg/subscriptions","organizations_url":"https://api.github.com/users/raelg/orgs","repos_url":"https://api.github.com/users/raelg/repos","events_url":"https://api.github.com/users/raelg/events{/privacy}","received_events_url":"https://api.github.com/users/raelg/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":21,"created_at":"2015-06-15T08:47:04Z","updated_at":"2015-06-25T00:35:47Z","closed_at":null,"body":"Hi,\r\n\r\nI think the following method is missing from the Service class, which will enable straight-forward chained service calls. \r\n\r\n```scala\r\n def andThen[Rep2](service: Service[Rep, Rep2]) : Service[Req, Rep2] = {\r\n    new Service[Req, Rep2] {\r\n      override def apply(request: Req): Future[Rep2] = {\r\n        Service.this.apply(request).flatMap{ rep =>\r\n          service.apply(rep)\r\n        }\r\n      }\r\n    }\r\n  }\r\n``` \r\n\r\ne.g, this will enable the following service hierarchy: \r\n\r\n```scala\r\nval importantService: Service[Request, JsonModel] = {...} // invokes the service and returns a JsonModel \r\nval jsonService: Service[JsonModel, Response] = {...} // converts the JsonModel to a valid response\r\nval myService: Service[Request, Response] = importantService andThen jsonService \r\n```\r\n\r\nThis simplifies the responsibility and testability of each service, while enabling reuse.\r\n\r\nThis would also enable a Service to be positioned before a Filter, as in the example below:\r\n\r\ne.g, \r\n\r\n```scala\r\nval importantService: Service[Request, [Option[JsonModel]] = {...} // invokes the service and returns a optional JsonModel \r\nval optionalMapperFilter: Filter[Option[JsonModel], Response, JsonModel, Response]  = {..} // unrwap the option and returns 404 if None, or invoke the service with the value otherwise \r\nval jsonService: Service[JsonModel, Response] = {...} // convert the JsonModel to a valid response\r\nval myService: Service[Request, Response] = importantService andThen (optionalMapperFilter andThen jsonService)\r\n```    \r\n    \r\n    "},"comment":{"url":"https://api.github.com/repos/twitter/finagle/issues/comments/115054987","html_url":"https://github.com/twitter/finagle/issues/385#issuecomment-115054987","issue_url":"https://api.github.com/repos/twitter/finagle/issues/385","id":115054987,"user":{"login":"dschobel","id":58369,"avatar_url":"https://avatars.githubusercontent.com/u/58369?v=3","gravatar_id":"","url":"https://api.github.com/users/dschobel","html_url":"https://github.com/dschobel","followers_url":"https://api.github.com/users/dschobel/followers","following_url":"https://api.github.com/users/dschobel/following{/other_user}","gists_url":"https://api.github.com/users/dschobel/gists{/gist_id}","starred_url":"https://api.github.com/users/dschobel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dschobel/subscriptions","organizations_url":"https://api.github.com/users/dschobel/orgs","repos_url":"https://api.github.com/users/dschobel/repos","events_url":"https://api.github.com/users/dschobel/events{/privacy}","received_events_url":"https://api.github.com/users/dschobel/received_events","type":"User","site_admin":false},"created_at":"2015-06-25T00:35:47Z","updated_at":"2015-06-25T00:35:47Z","body":"Consensus seems to be that no one feels strongly that this belongs in ```Service```, if no one objects I'll close this."}}