{"id":21517610,"url":"https://github.com/tek/cattrix","last_synced_at":"2025-04-09T21:42:31.570Z","repository":{"id":57736360,"uuid":"138503624","full_name":"tek/cattrix","owner":"tek","description":"http request and codahale metrics abstraction","archived":false,"fork":false,"pushed_at":"2018-12-06T19:39:39.000Z","size":71,"stargazers_count":6,"open_issues_count":1,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-23T23:34:37.666Z","etag":null,"topics":["cats","cats-effect","cats-free","circe","codahale-metrics","http","http4s","metrics","play-framework"],"latest_commit_sha":null,"homepage":"","language":"Scala","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tek.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-06-24T17:53:02.000Z","updated_at":"2019-06-06T17:14:03.000Z","dependencies_parsed_at":"2022-08-30T12:10:32.737Z","dependency_job_id":null,"html_url":"https://github.com/tek/cattrix","commit_stats":null,"previous_names":["tek/cats-http-metrics"],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tek%2Fcattrix","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tek%2Fcattrix/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tek%2Fcattrix/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tek%2Fcattrix/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tek","download_url":"https://codeload.github.com/tek/cattrix/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248118941,"owners_count":21050748,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cats","cats-effect","cats-free","circe","codahale-metrics","http","http4s","metrics","play-framework"],"created_at":"2024-11-24T00:43:49.932Z","updated_at":"2025-04-09T21:42:31.538Z","avatar_url":"https://github.com/tek.png","language":"Scala","readme":"# Cattrix - http and metrics with cats\n\nA library for generic and abstract interaction with http requests and codahale metrics\n\nModule IDs:\n\n```sbt\n\"io.tryp\" % \"cattrix-http4s\" % \"0.1.5\"\n\"io.tryp\" % \"cattrix-play\" % \"0.1.5\"\n\"io.tryp\" % \"cattrix-metrics\" % \"0.1.5\"\n\"io.tryp\" % \"cattrix-request\" % \"0.1.5\"\n\"io.tryp\" % \"cattrix-codahale\" % \"0.1.5\"\n```\n\n# Metrics\n\nThis module provides a DSL for monadically sequencing metrics around arbitrary computational thunks independent of the metrics reporter.\n\nTake this example:\n\n```scala\nimport cattrix.Metrics\nimport dbframework.DatabaseQuery\n\ndef databaseQuery: IO[DatabaseQuery] = ???\n\nval prog: Metrics.Step[IO, DatabaseQuery] = for {\n  t \u003c- Metrics.timer(\"time\")\n  _ \u003c- Metrics.incCounter(\"active\")\n  query \u003c- Metrics.run(() =\u003e database.query(\"12345\"))\n  _ \u003c- Metrics.decCounter(\"active\")\n  _ \u003c- Metrics.time(t)\n  _ \u003c- Metrics.mark(\"success\")\n} yield query\n```\n\nThis defines a program made out of `cats.free.FreeT`, where `Metrics.run` takes an arbitrary `F[_]: cats.effect.Sync`.\nThe `() =\u003e` is for `Future` support (there is an instance of `Sync[Future]`, but you are strongly discouraged from using it).\n\nThe `FreeT` is designed around the algebra `Metric`, which currently consists of operations to increase/decrease a\ncounter, run a timer, and mark an event.\n\nThe program can be executed by conjuring up a codahale interpreter (or providing an alternative one manually) using the\ntypeclass `Metrics`. This is conveniently hidden by the helper `Metrics.compile`, which takes a data type description of\nthe intended interpreter, assuming that there is an instance of `Metrics` defined for it:\n\n```scala\nval prefix = \"my.service\"\nval metric = \"dbquery\"\nval io: IO[DatabaseQuery] = Metrics.compile(MetricTask(Codahale.as[IO](prefix), metric))(prog)\n```\n\nInstead of `Codahale`, you can pass `NoMetrics` or `CustomMetrics` to do something different with the metrics, or even\n`Codahale(registry, prefix, customHandler)` to inspect the codahale interpreter's internals but do something else with\nthe data.\n\n# Request\n\nOne major application intent of this library is to use it consistently across arbitrary web frameworks to meter http\nrequests.\nThis module provides the interface `Http`, which can use any backend client that implements an instance of\n`HttpRequest` and wraps the requests in a metrics program similar to the one shown in the previous section.\n`Http` can use custom backend-independent data types to represent requests and responses, but you can pass in the native\nvariants as well, if available.\n\nAn example request looks like this:\n\n```scala\nimport cattrix.{Http, HttpConfig, Http4sRequest, Request, RequestTask, Response, Codahale, RequestMetric}\nimport cattrix.Http4sInstances._\n\ndef http: Http[IO, http4s.Request[IO], http4s.Response[IO]] =\n  Http.fromConfig(HttpConfig(Http4sRequest(), Codahale.as[IO](\"my.service\")))\n\nval body = \"\"\"{ \"name\": \"tek\" }\"\"\"\nval request: Request = Request(\"post\", \"http://localhost/item\", Some(body), None, Nil)\nval response: IO[Response] = http.request(RequestTask(request, RequestMetric.named(\"postItem\")))\n```\n\nThe `Request` is translated into `http4s.Request` by the typeclass `HttpRequest`, while the class `HttpResponse` does\nthe opposite for `Response`.\n\n`Http` contains a subinterface `NativeHttp`, with which you can pass in a `http4s.Response` and get back its native\ncounterpart:\n\n```scala\nval response: IO[http4s.Response[IO]] = http.native.get(\"http://localhost/item/1\", \"getItem\")\n```\n\nThis also showcases one of the convenience methods that both interfaces offer.\n\n## JSON\n\n`Http` provides methods for direct json decoding using implicit `io.circe.Decoder` instances:\n\n```scala\nimport cattrix.JsonResponse\nimport io.circe.generic.auto._\n\ncase class Payload(data: String)\n\nval io: IO[String] = http.getAs[Payload](url, metric).map {\n  case JsonResponse.Successful(Payload(data)) =\u003e data\n  case JsonResponse.Unsuccessful(status, body) =\u003e s\"request failed with status $status: $body\"\n  case JsonResponse.DecodingError(error, body) =\u003e s\"couldn't decode body: $error ($body)\"\n}\n```\n\n## Framework Support\n\nCurrently, typeclass instances for `http4s` and `Play` are available in the corresponding packages.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftek%2Fcattrix","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftek%2Fcattrix","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftek%2Fcattrix/lists"}