├── .gitignore ├── .github └── workflows │ └── test.yml ├── src ├── gleam_hackney_ffi.erl └── gleam │ └── hackney.gleam ├── gleam.toml ├── CHANGELOG.md ├── README.md ├── test └── gleam_hackney_test.gleam ├── manifest.toml └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | *.beam 2 | *.ez 3 | build 4 | erl_crash.dump 5 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: test 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | - main 8 | pull_request: 9 | 10 | jobs: 11 | test: 12 | runs-on: ubuntu-latest 13 | steps: 14 | - uses: actions/checkout@v3 15 | - uses: erlef/setup-beam@v1 16 | with: 17 | otp-version: "28" 18 | gleam-version: "1.12.0" 19 | rebar3-version: "3" 20 | - run: gleam test 21 | - run: gleam format --check src test 22 | -------------------------------------------------------------------------------- /src/gleam_hackney_ffi.erl: -------------------------------------------------------------------------------- 1 | -module(gleam_hackney_ffi). 2 | 3 | -export([send/4]). 4 | 5 | send(Method, Url, Headers, Body) -> 6 | Options = [{with_body, true}], 7 | case hackney:request(Method, Url, Headers, Body, Options) of 8 | {ok, Status, ResponseHeaders, ResponseBody} -> 9 | {ok, {response, Status, ResponseHeaders, ResponseBody}}; 10 | 11 | {ok, Status, ResponseHeaders} -> 12 | {ok, {response, Status, ResponseHeaders, <<>>}}; 13 | 14 | {error, Error} -> 15 | {error, {other, Error}} 16 | end. 17 | -------------------------------------------------------------------------------- /gleam.toml: -------------------------------------------------------------------------------- 1 | name = "gleam_hackney" 2 | version = "1.3.2" 3 | gleam = ">= 1.0.0" 4 | 5 | description = "Gleam bindings to the Hackney HTTP client" 6 | licences = ["Apache-2.0"] 7 | 8 | repository = { type = "github", user = "gleam-lang", repo = "hackney" } 9 | links = [ 10 | { title = "Website", href = "https://gleam.run" }, 11 | { title = "Sponsor", href = "https://github.com/sponsors/lpil" }, 12 | ] 13 | 14 | [dependencies] 15 | gleam_stdlib = ">= 0.45.0 and < 2.0.0" 16 | gleam_http = ">= 3.0.0 and < 5.0.0" 17 | hackney = ">= 1.18.0 and < 2.0.0" 18 | 19 | [dev-dependencies] 20 | gleeunit = ">= 1.0.0 and < 2.0.0" 21 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## v1.3.2 - 2025-09-03 4 | 5 | - Updated for latest `gleam_stdlib`. 6 | 7 | ## v1.3.1 - 2025-02-06 8 | 9 | - Relaxed the `gleam_http` constraint to permit v4. 10 | 11 | ## v1.3.0 - 2024-12-07 12 | 13 | - Updated for `gleam_stdlib` v0.45.0. 14 | 15 | ## v1.2.0 - 2023-11-06 16 | 17 | - Updated for Gleam v0.32.0. 18 | 19 | ## v1.1.0 - 2023-08-03 20 | 21 | - Updated for Gleam v0.30.0. 22 | 23 | ## v1.0.0 - 2023-03-02 24 | 25 | - Updated for Gleam v0.27.0. 26 | 27 | ## v0.2.1 - 2022-05-24 28 | 29 | - Fixed some warnings with current Gleam. 30 | 31 | ## v0.2.0 - 2022-01-31 32 | 33 | - Updated for `gleam_http` v3. 34 | 35 | ## v0.1.0 - 2021-12-26 36 | 37 | - Initial release. 38 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Hackney 2 | 3 | [![Package Version](https://img.shields.io/hexpm/v/gleam_hackney)](https://hex.pm/packages/gleam_hackney) 4 | [![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/gleam_hackney/) 5 | 6 | Bindings to Erlang's `hackney` HTTP client. 7 | 8 | Most the time [`gleam_httpc`](https://github.com/gleam-lang/httpc) is a better 9 | choice as it uses the built-in Erlang HTTP client, but this package may be 10 | useful in some specific situations. 11 | 12 | ```shell 13 | gleam add gleam_hackney@1 gleam_http 14 | ``` 15 | ```gleam 16 | import gleam/result 17 | import gleam/hackney 18 | import gleam/http/request 19 | import gleam/http/response 20 | 21 | pub fn main() { 22 | // Prepare a HTTP request record 23 | let assert Ok(request) = 24 | request.to("https://test-api.service.hmrc.gov.uk/hello/world") 25 | 26 | // Send the HTTP request to the server 27 | use response <- result.try( 28 | request 29 | |> request.prepend_header("accept", "application/vnd.hmrc.1.0+json") 30 | |> hackney.send 31 | ) 32 | 33 | // We get a response record back 34 | assert response.status == 200 35 | 36 | assert response.get_header(response, "content-type") 37 | == Ok("application/json") 38 | 39 | assert response.body 40 | == "{\"message\":\"Hello World\"}" 41 | 42 | Ok(response) 43 | } 44 | ``` 45 | -------------------------------------------------------------------------------- /src/gleam/hackney.gleam: -------------------------------------------------------------------------------- 1 | import gleam/bit_array 2 | import gleam/bytes_tree.{type BytesTree} 3 | import gleam/dynamic.{type Dynamic} 4 | import gleam/http.{type Method} 5 | import gleam/http/request.{type Request} 6 | import gleam/http/response.{type Response, Response} 7 | import gleam/list 8 | import gleam/result 9 | import gleam/string 10 | import gleam/uri 11 | 12 | pub type Error { 13 | InvalidUtf8Response 14 | // TODO: refine error type 15 | Other(Dynamic) 16 | } 17 | 18 | @external(erlang, "gleam_hackney_ffi", "send") 19 | fn ffi_send( 20 | a: Method, 21 | b: String, 22 | c: List(http.Header), 23 | d: BytesTree, 24 | ) -> Result(Response(BitArray), Error) 25 | 26 | // TODO: test 27 | pub fn send_bits( 28 | request: Request(BytesTree), 29 | ) -> Result(Response(BitArray), Error) { 30 | use response <- result.try( 31 | request 32 | |> request.to_uri 33 | |> uri.to_string 34 | |> ffi_send(request.method, _, request.headers, request.body), 35 | ) 36 | let headers = list.map(response.headers, normalise_header) 37 | Ok(Response(..response, headers: headers)) 38 | } 39 | 40 | pub fn send(req: Request(String)) -> Result(Response(String), Error) { 41 | use resp <- result.try( 42 | req 43 | |> request.map(bytes_tree.from_string) 44 | |> send_bits, 45 | ) 46 | 47 | case bit_array.to_string(resp.body) { 48 | Ok(body) -> Ok(response.set_body(resp, body)) 49 | Error(_) -> Error(InvalidUtf8Response) 50 | } 51 | } 52 | 53 | fn normalise_header(header: http.Header) -> http.Header { 54 | #(string.lowercase(header.0), header.1) 55 | } 56 | -------------------------------------------------------------------------------- /test/gleam_hackney_test.gleam: -------------------------------------------------------------------------------- 1 | import gleam/hackney 2 | import gleam/http.{Get, Head, Options} 3 | import gleam/http/request 4 | import gleam/http/response 5 | import gleeunit 6 | 7 | pub fn main() { 8 | // Run the tests 9 | gleeunit.main() 10 | } 11 | 12 | pub fn request_test() { 13 | let req = 14 | request.new() 15 | |> request.set_method(Get) 16 | |> request.set_host("test-api.service.hmrc.gov.uk") 17 | |> request.set_path("/hello/world") 18 | |> request.prepend_header("accept", "application/vnd.hmrc.1.0+json") 19 | 20 | let assert Ok(resp) = hackney.send(req) 21 | let assert 200 = resp.status 22 | let assert Ok("application/json") = response.get_header(resp, "content-type") 23 | let assert "{\"message\":\"Hello World\"}" = resp.body 24 | } 25 | 26 | pub fn get_request_discards_body_test() { 27 | let req = 28 | request.new() 29 | |> request.set_method(Get) 30 | |> request.set_host("api.github.com") 31 | |> request.set_path("/zen") 32 | |> request.set_body("This gets dropped") 33 | 34 | let assert Ok(resp) = hackney.send(req) 35 | let assert 200 = resp.status 36 | let assert Ok("text/plain;charset=utf-8") = 37 | response.get_header(resp, "content-type") 38 | let assert True = resp.body != "" 39 | } 40 | 41 | pub fn head_request_discards_body_test() { 42 | let req = 43 | request.new() 44 | |> request.set_method(Head) 45 | |> request.set_host("postman-echo.com") 46 | |> request.set_path("/get") 47 | |> request.set_body("This gets dropped") 48 | 49 | let assert Ok(resp) = hackney.send(req) 50 | let assert 200 = resp.status 51 | let assert Ok("application/json; charset=utf-8") = 52 | response.get_header(resp, "content-type") 53 | let assert "" = resp.body 54 | } 55 | 56 | pub fn options_request_discards_body_test() { 57 | let req = 58 | request.new() 59 | |> request.set_method(Options) 60 | |> request.set_host("postman-echo.com") 61 | |> request.set_path("/get") 62 | |> request.set_body("This gets dropped") 63 | 64 | let assert Ok(resp) = hackney.send(req) 65 | let assert 200 = resp.status 66 | let assert Ok("text/html; charset=utf-8") = 67 | response.get_header(resp, "content-type") 68 | let assert "GET,HEAD,PUT,POST,DELETE,PATCH" = resp.body 69 | } 70 | -------------------------------------------------------------------------------- /manifest.toml: -------------------------------------------------------------------------------- 1 | # This file was generated by Gleam 2 | # You typically do not need to edit this file 3 | 4 | packages = [ 5 | { name = "certifi", version = "2.12.0", build_tools = ["rebar3"], requirements = [], otp_app = "certifi", source = "hex", outer_checksum = "EE68D85DF22E554040CDB4BE100F33873AC6051387BAF6A8F6CE82272340FF1C" }, 6 | { name = "gleam_http", version = "4.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "0A62451FC85B98062E0907659D92E6A89F5F3C0FBE4AB8046C99936BF6F91DBC" }, 7 | { name = "gleam_stdlib", version = "0.54.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "723BA61A2BAE8D67406E59DD88CEA1B3C3F266FC8D70F64BE9FEC81B4505B927" }, 8 | { name = "gleeunit", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "0E6C83834BA65EDCAAF4FE4FB94AC697D9262D83E6F58A750D63C9F6C8A9D9FF" }, 9 | { name = "hackney", version = "1.20.1", build_tools = ["rebar3"], requirements = ["certifi", "idna", "metrics", "mimerl", "parse_trans", "ssl_verify_fun", "unicode_util_compat"], otp_app = "hackney", source = "hex", outer_checksum = "FE9094E5F1A2A2C0A7D10918FEE36BFEC0EC2A979994CFF8CFE8058CD9AF38E3" }, 10 | { name = "idna", version = "6.1.1", build_tools = ["rebar3"], requirements = ["unicode_util_compat"], otp_app = "idna", source = "hex", outer_checksum = "92376EB7894412ED19AC475E4A86F7B413C1B9FBB5BD16DCCD57934157944CEA" }, 11 | { name = "metrics", version = "1.0.1", build_tools = ["rebar3"], requirements = [], otp_app = "metrics", source = "hex", outer_checksum = "69B09ADDDC4F74A40716AE54D140F93BEB0FB8978D8636EADED0C31B6F099F16" }, 12 | { name = "mimerl", version = "1.3.0", build_tools = ["rebar3"], requirements = [], otp_app = "mimerl", source = "hex", outer_checksum = "A1E15A50D1887217DE95F0B9B0793E32853F7C258A5CD227650889B38839FE9D" }, 13 | { name = "parse_trans", version = "3.4.1", build_tools = ["rebar3"], requirements = [], otp_app = "parse_trans", source = "hex", outer_checksum = "620A406CE75DADA827B82E453C19CF06776BE266F5A67CFF34E1EF2CBB60E49A" }, 14 | { name = "ssl_verify_fun", version = "1.1.7", build_tools = ["mix", "rebar3", "make"], requirements = [], otp_app = "ssl_verify_fun", source = "hex", outer_checksum = "FE4C190E8F37401D30167C8C405EDA19469F34577987C76DDE613E838BBC67F8" }, 15 | { name = "unicode_util_compat", version = "0.7.0", build_tools = ["rebar3"], requirements = [], otp_app = "unicode_util_compat", source = "hex", outer_checksum = "25EEE6D67DF61960CF6A794239566599B09E17E668D3700247BC498638152521" }, 16 | ] 17 | 18 | [requirements] 19 | gleam_http = { version = ">= 3.0.0 and < 5.0.0" } 20 | gleam_stdlib = { version = ">= 0.45.0 and < 2.0.0" } 21 | gleeunit = { version = ">= 1.0.0 and < 2.0.0" } 22 | hackney = { version = ">= 1.18.0 and < 2.0.0" } 23 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | Copyright 2021 - present, Louis Pilfold . 179 | 180 | Licensed under the Apache License, Version 2.0 (the "License"); 181 | you may not use this file except in compliance with the License. 182 | You may obtain a copy of the License at 183 | 184 | http://www.apache.org/licenses/LICENSE-2.0 185 | 186 | Unless required by applicable law or agreed to in writing, software 187 | distributed under the License is distributed on an "AS IS" BASIS, 188 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 189 | See the License for the specific language governing permissions and 190 | limitations under the License. 191 | 192 | --------------------------------------------------------------------------------