├── .credo.exs ├── .formatter.exs ├── .github ├── dependabot.yml └── workflows │ └── elixir.yml ├── .gitignore ├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── lib ├── twirp.ex └── twirp │ ├── client │ ├── adapter.ex │ ├── finch.ex │ ├── hackney.ex │ └── http.ex │ ├── encoder.ex │ ├── error.ex │ ├── plug.ex │ ├── protoc │ ├── cli.ex │ ├── generator.ex │ ├── generator │ │ └── service.ex │ └── template.ex │ ├── service.ex │ └── telemetry.ex ├── mix.exs ├── mix.lock ├── priv └── templates │ └── service.ex.eex └── test ├── support ├── service.pb.ex ├── service.proto └── service_twirp.ex ├── test_helper.exs ├── twirp ├── client │ └── hackney_test.exs ├── client_test.exs ├── encoder_test.exs ├── error_test.exs ├── plug_test.exs └── service_test.exs └── twirp_test.exs /.credo.exs: -------------------------------------------------------------------------------- 1 | # This file contains the configuration for Credo and you are probably reading 2 | # this after creating it with `mix credo.gen.config`. 3 | # 4 | # If you find anything wrong or unclear in this file, please report an 5 | # issue on GitHub: https://github.com/rrrene/credo/issues 6 | # 7 | %{ 8 | # 9 | # You can have as many configs as you like in the `configs:` field. 10 | configs: [ 11 | %{ 12 | # 13 | # Run any exec using `mix credo -C `. If no exec name is given 14 | # "default" is used. 15 | # 16 | name: "default", 17 | # 18 | # These are the files included in the analysis: 19 | files: %{ 20 | # 21 | # You can give explicit globs or simply directories. 22 | # In the latter case `**/*.{ex,exs}` will be used. 23 | # 24 | included: ["lib/", "test/"], 25 | excluded: [~r"/_build/", ~r"/deps/", ~r"/node_modules/"] 26 | }, 27 | # 28 | # Load and configure plugins here: 29 | # 30 | plugins: [], 31 | # 32 | # If you create your own checks, you must specify the source files for 33 | # them here, so they can be loaded by Credo before running the analysis. 34 | # 35 | requires: [], 36 | # 37 | # If you want to enforce a style guide and need a more traditional linting 38 | # experience, you can change `strict` to `true` below: 39 | # 40 | strict: false, 41 | # 42 | # If you want to use uncolored output by default, you can change `color` 43 | # to `false` below: 44 | # 45 | color: true, 46 | # 47 | # You can customize the parameters of any check by adding a second element 48 | # to the tuple. 49 | # 50 | # To disable a check put `false` as second element: 51 | # 52 | # {Credo.Check.Design.DuplicatedCode, false} 53 | # 54 | checks: [ 55 | # 56 | ## Consistency Checks 57 | # 58 | {Credo.Check.Consistency.ExceptionNames, []}, 59 | {Credo.Check.Consistency.LineEndings, []}, 60 | {Credo.Check.Consistency.MultiAliasImportRequireUse, false}, 61 | {Credo.Check.Consistency.ParameterPatternMatching, []}, 62 | {Credo.Check.Consistency.SpaceAroundOperators, false}, 63 | {Credo.Check.Consistency.SpaceInParentheses, false}, 64 | {Credo.Check.Consistency.TabsOrSpaces, []}, 65 | {Credo.Check.Consistency.UnusedVariableNames, false}, 66 | 67 | # 68 | ## Design Checks 69 | # 70 | # You can customize the priority of any check 71 | # Priority values are: `low, normal, high, higher` 72 | # 73 | {Credo.Check.Design.AliasUsage, 74 | [priority: :low, if_nested_deeper_than: 4, if_called_more_often_than: 2]}, 75 | {Credo.Check.Design.DuplicatedCode, false}, 76 | {Credo.Check.Design.TagTODO, false}, 77 | {Credo.Check.Design.TagFIXME, false}, 78 | 79 | # 80 | ## Readability Checks 81 | # 82 | {Credo.Check.Readability.AliasAs, false}, 83 | {Credo.Check.Readability.AliasOrder, false}, 84 | {Credo.Check.Readability.FunctionNames, []}, 85 | {Credo.Check.Readability.LargeNumbers, []}, 86 | {Credo.Check.Readability.MaxLineLength, false}, 87 | {Credo.Check.Readability.ModuleAttributeNames, []}, 88 | {Credo.Check.Readability.ModuleDoc, []}, 89 | {Credo.Check.Readability.ModuleNames, []}, 90 | {Credo.Check.Readability.MultiAlias, false}, 91 | {Credo.Check.Readability.ParenthesesInCondition, []}, 92 | {Credo.Check.Readability.ParenthesesOnZeroArityDefs, []}, 93 | {Credo.Check.Readability.PredicateFunctionNames, false}, 94 | {Credo.Check.Readability.PreferImplicitTry, []}, 95 | {Credo.Check.Readability.RedundantBlankLines, [max_blank_lines: 1]}, 96 | {Credo.Check.Readability.Semicolons, []}, 97 | {Credo.Check.Readability.SinglePipe, false}, 98 | {Credo.Check.Readability.SpaceAfterCommas, false}, 99 | {Credo.Check.Readability.Specs, false}, 100 | {Credo.Check.Readability.StringSigils, []}, 101 | {Credo.Check.Readability.TrailingBlankLine, []}, 102 | {Credo.Check.Readability.TrailingWhiteSpace, []}, 103 | # TODO: enable by default in Credo 1.1 104 | {Credo.Check.Readability.UnnecessaryAliasExpansion, false}, 105 | {Credo.Check.Readability.VariableNames, []}, 106 | 107 | # 108 | # Controversial and experimental checks (opt-in, just replace `false` with `[]`) 109 | # 110 | 111 | # 112 | ## Refactoring Opportunities 113 | # 114 | {Credo.Check.Refactor.ABCSize, false}, 115 | {Credo.Check.Refactor.AppendSingleItem, false}, 116 | {Credo.Check.Refactor.CaseTrivialMatches, false}, 117 | {Credo.Check.Refactor.CondStatements, false}, 118 | {Credo.Check.Refactor.CyclomaticComplexity, [max_complexity: 18]}, 119 | {Credo.Check.Refactor.DoubleBooleanNegation, false}, 120 | {Credo.Check.Refactor.FunctionArity, []}, 121 | {Credo.Check.Refactor.LongQuoteBlocks, false}, 122 | {Credo.Check.Refactor.MapInto, false}, 123 | {Credo.Check.Refactor.MatchInCondition, false}, 124 | {Credo.Check.Refactor.ModuleDependencies, false}, 125 | {Credo.Check.Refactor.NegatedConditionsInUnless, []}, 126 | {Credo.Check.Refactor.NegatedConditionsWithElse, []}, 127 | {Credo.Check.Refactor.Nesting, false}, 128 | {Credo.Check.Refactor.PipeChainStart, false}, 129 | {Credo.Check.Refactor.UnlessWithElse, []}, 130 | {Credo.Check.Refactor.VariableRebinding, false}, 131 | {Credo.Check.Refactor.WithClauses, []}, 132 | 133 | # 134 | ## Warnings 135 | # 136 | {Credo.Check.Warning.BoolOperationOnSameValues, []}, 137 | {Credo.Check.Warning.ExpensiveEmptyEnumCheck, []}, 138 | {Credo.Check.Warning.IExPry, []}, 139 | {Credo.Check.Warning.IoInspect, []}, 140 | {Credo.Check.Warning.LazyLogging, false}, 141 | {Credo.Check.Warning.MapGetUnsafePass, false}, 142 | {Credo.Check.Warning.OperationOnSameValues, []}, 143 | {Credo.Check.Warning.OperationWithConstantResult, false}, 144 | {Credo.Check.Warning.RaiseInsideRescue, []}, 145 | {Credo.Check.Warning.UnsafeToAtom, false}, 146 | {Credo.Check.Warning.UnusedEnumOperation, []}, 147 | {Credo.Check.Warning.UnusedFileOperation, []}, 148 | {Credo.Check.Warning.UnusedKeywordOperation, []}, 149 | {Credo.Check.Warning.UnusedListOperation, []}, 150 | {Credo.Check.Warning.UnusedPathOperation, []}, 151 | {Credo.Check.Warning.UnusedRegexOperation, []}, 152 | {Credo.Check.Warning.UnusedStringOperation, []}, 153 | {Credo.Check.Warning.UnusedTupleOperation, []}, 154 | ] 155 | } 156 | ] 157 | } 158 | -------------------------------------------------------------------------------- /.formatter.exs: -------------------------------------------------------------------------------- 1 | locals_without_parens = [rpc: 4, package: 1, service: 1] 2 | [ 3 | inputs: [], 4 | locals_without_parens: locals_without_parens, 5 | export: [ 6 | locals_without_parens: locals_without_parens 7 | ] 8 | ] 9 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: mix 4 | directory: "/" 5 | schedule: 6 | interval: daily 7 | time: "10:00" 8 | open-pull-requests-limit: 10 9 | ignore: 10 | - dependency-name: ex_doc 11 | versions: 12 | - 0.24.0 13 | - 0.24.1 14 | - dependency-name: hackney 15 | versions: 16 | - 1.17.1 17 | - 1.17.2 18 | - 1.17.3 19 | - dependency-name: finch 20 | versions: 21 | - 0.6.1 22 | - 0.6.2 23 | - dependency-name: credo 24 | versions: 25 | - 1.5.4 26 | -------------------------------------------------------------------------------- /.github/workflows/elixir.yml: -------------------------------------------------------------------------------- 1 | name: Elixir CI 2 | 3 | on: 4 | push: 5 | branches: [ master ] 6 | pull_request: 7 | branches: [ master ] 8 | 9 | env: 10 | MIX_ENV: test 11 | 12 | jobs: 13 | deps: 14 | name: Install Dependencies 15 | runs-on: ubuntu-latest 16 | strategy: 17 | matrix: 18 | elixir: [1.11] 19 | otp: [23.3] 20 | steps: 21 | - name: checkout 22 | uses: actions/checkout@v2 23 | with: 24 | fetch-depth: 0 25 | - name: setup 26 | uses: erlef/setup-beam@v1 27 | with: 28 | elixir-version: ${{ matrix.elixir }} 29 | otp-version: ${{ matrix.otp }} 30 | - name: Retrieve Cached Dependencies 31 | uses: actions/cache@v2 32 | id: mix-cache 33 | with: 34 | path: | 35 | deps 36 | _build 37 | priv/plts 38 | key: ${{ runner.os }}-${{ matrix.otp }}-${{ matrix.elixir }}-${{ hashFiles('mix.lock') }} 39 | - name: Install deps 40 | if: steps.mix-cache.outputs.cache-hit != 'true' 41 | run: | 42 | mkdir -p priv/plts 43 | mix local.rebar --force 44 | mix local.hex --force 45 | mix deps.get 46 | mix deps.compile 47 | mix dialyzer --plt 48 | 49 | analyze: 50 | name: Analysis 51 | needs: deps 52 | runs-on: ubuntu-latest 53 | strategy: 54 | matrix: 55 | elixir: [1.11] 56 | otp: [23.3] 57 | steps: 58 | - uses: actions/checkout@v2 59 | with: 60 | fetch-depth: 0 61 | - name: Setup elixir 62 | uses: erlef/setup-beam@v1 63 | with: 64 | elixir-version: ${{ matrix.elixir }} 65 | otp-version: ${{ matrix.otp }} 66 | - name: Retrieve Cached Dependencies 67 | uses: actions/cache@v2 68 | id: mix-cache 69 | with: 70 | path: | 71 | deps 72 | _build 73 | priv/plts 74 | key: ${{ runner.os }}-${{ matrix.otp }}-${{ matrix.elixir }}-${{ hashFiles('mix.lock') }} 75 | - name: Run Credo 76 | run: mix credo 77 | - name: Run Dialyzer 78 | run: mix dialyzer --no-check --halt-exit-status 79 | 80 | tests: 81 | name: Tests 82 | needs: deps 83 | runs-on: ubuntu-latest 84 | strategy: 85 | matrix: 86 | elixir: [1.11] 87 | otp: [23.3] 88 | steps: 89 | - uses: actions/checkout@v2 90 | with: 91 | fetch-depth: 0 92 | - uses: erlef/setup-beam@v1 93 | with: 94 | elixir-version: ${{ matrix.elixir }} 95 | otp-version: ${{ matrix.otp }} 96 | - name: Retrieve Cached Dependencies 97 | uses: actions/cache@v2 98 | id: mix-cache 99 | with: 100 | path: | 101 | deps 102 | _build 103 | priv/plts 104 | key: ${{ runner.os }}-${{ matrix.otp }}-${{ matrix.elixir }}-${{ hashFiles('mix.lock') }} 105 | - name: Run Tests 106 | run: mix test 107 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # The directory Mix will write compiled artifacts to. 2 | /_build/ 3 | 4 | # If you run "mix test --cover", coverage assets end up here. 5 | /cover/ 6 | 7 | # The directory Mix downloads your dependencies sources to. 8 | /deps/ 9 | 10 | # Where third-party dependencies like ExDoc output generated docs. 11 | /doc/ 12 | 13 | # Ignore .fetch files in case you like to edit your project deps locally. 14 | /.fetch 15 | 16 | # If the VM crashes, it generates a dump, let's ignore it too. 17 | erl_crash.dump 18 | 19 | # Also ignore archive artifacts (built via "mix archive.build"). 20 | *.ez 21 | 22 | # Ignore package tarball (built via "mix hex.build"). 23 | twirp-*.tar 24 | 25 | protoc-gen-twirp_elixir 26 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # CHANGELOG 2 | 3 | ## 0.7.4 4 | * Fix bug in decoding response bodies 5 | * Fix decoding nested strings 6 | * Use the latest finch 7 | 8 | ## 0.7.3 9 | * Gracefully handle all client errors 10 | 11 | ## 0.5.0 12 | 13 | * Convert twirp errors to exceptions 14 | * Improve client to use latest finch 15 | 16 | ## 0.4.1 17 | 18 | * Fix bug with producing stop telemetry. 19 | 20 | ## 0.4.0 21 | 22 | ## 0.3.1 (December 05, 2019) 23 | 24 | * [ccd8fc5](https://github.com/keathley/twirp/commit/ccd8fc5637eb03e8bf40d84e2c956bb6cd35d274) Strip struct before encoding as JSON. - Jeffery Utter 25 | 26 | ## 0.3.0 (December 04, 2019) 27 | 28 | * [0a85ecc](https://github.com/keathley/twirp/commit/0a85eccc588ea72dfb117bd599c80c1254c414e8) Support hooks around twirp services - Chris Keathley 29 | 30 | ## 0.2.1 (December 02, 2019) 31 | 32 | * [5b446e3](https://github.com/keathley/twirp/commit/5b446e3b98241ae01680b3fcd1b2e38e22008050) Add support for hackney pools - Chris Keathley 33 | * [5fc9eb5](https://github.com/keathley/twirp/commit/5fc9eb5ee5832e4847937ac8aac3fc0851dba880) Add comments to generated files - Chris Keathley 34 | * [c0eb0f6](https://github.com/keathley/twirp/commit/c0eb0f627a2c448dfe540c04b3b0ee96fa5206c4) Clients should define callbacks. - Chris Keathley 35 | * [d70ef62](https://github.com/keathley/twirp/commit/d70ef62c2dbcb00b718c51e14a4193093e0243d1) Handle pre-parsed json - Chris Keathley 36 | * [b9e46f7](https://github.com/keathley/twirp/commit/b9e46f715ebe6a9482cf637ddd2a688228a9a61b) Return errors when a handler raises an exception - Chris Keathley 37 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 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 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2019 Chris Keathley 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Twirp 2 | 3 | Twirp provides an Elixir implementation of the [Twirp RPC framework](https://github.com/twitchtv/twirp) developed by 4 | Twitch. The protocol defines semantics for routing and serialization of RPCs 5 | based on protobufs. 6 | 7 | [https://hexdocs.pm/twirp](https://hexdocs.pm/twirp). 8 | 9 | ## Installation 10 | 11 | Add Twirp to your list of dependencies: 12 | 13 | ```elixir 14 | def deps do 15 | [ 16 | {:twirp, "~> 0.8"} 17 | ] 18 | end 19 | ``` 20 | 21 | If you want to be able to generate services and clients based on proto files 22 | you'll need the protoc compiler (`brew install protobuf` on MacOS) and both the 23 | twirp and elixir protobuf plugins: 24 | 25 | $ mix escript.install hex protobuf 26 | $ mix escript.install hex twirp 27 | 28 | Both of these escripts will need to be in your `$PATH` in order for protoc to 29 | find them. 30 | 31 | ## Example 32 | 33 | The canonical Twirp example is a Haberdasher service. Here's the protobuf 34 | description for the service. 35 | 36 | ```protobuf 37 | syntax = "proto3"; 38 | 39 | package example; 40 | 41 | // Haberdasher service makes hats for clients. 42 | service Haberdasher { 43 | // MakeHat produces a hat of mysterious, randomly-selected color! 44 | rpc MakeHat(Size) returns (Hat); 45 | } 46 | 47 | // Size of a Hat, in inches. 48 | message Size { 49 | int32 inches = 1; // must be > 0 50 | } 51 | 52 | // A Hat is a piece of headwear made by a Haberdasher. 53 | message Hat { 54 | int32 inches = 1; 55 | string color = 2; // anything but "invisible" 56 | string name = 3; // i.e. "bowler" 57 | } 58 | ``` 59 | 60 | We'll assume for now that this proto file lives in `priv/protos/service.proto` 61 | 62 | ### Code generation 63 | 64 | We can now use `protoc` to generate the files we need. You can run this command 65 | from the root directory of your project. 66 | 67 | $ protoc --proto_path=./priv/protos --elixir_out=./lib/example --twirp_elixir_out=./lib/example ./priv/protos/service.proto 68 | 69 | After running this command there should be 2 files located in `lib/example`. 70 | 71 | The message definitions: 72 | 73 | ```elixir 74 | defmodule Example.Size do 75 | @moduledoc false 76 | use Protobuf, syntax: :proto3 77 | 78 | @type t :: %__MODULE__{ 79 | inches: integer 80 | } 81 | 82 | defstruct [:inches] 83 | 84 | field :inches, 1, type: :int32 85 | end 86 | 87 | defmodule Example.Hat do 88 | @moduledoc false 89 | use Protobuf, syntax: :proto3 90 | 91 | @type t :: %__MODULE__{ 92 | inches: integer, 93 | color: String.t(), 94 | name: String.t() 95 | } 96 | defstruct [:inches, :color, :name] 97 | 98 | field :inches, 1, type: :int32 99 | field :color, 2, type: :string 100 | field :name, 3, type: :string 101 | end 102 | ``` 103 | 104 | The service and client definition: 105 | 106 | ```elixir 107 | defmodule Example.HaberdasherService do 108 | @moduledoc false 109 | use Twirp.Service 110 | 111 | package "example" 112 | service "Haberdasher" 113 | 114 | rpc :MakeHat, Example.Size, Example.Hat, :make_hat 115 | end 116 | 117 | defmodule Example.HaberdasherClient do 118 | @moduledoc false 119 | # client implementation... 120 | end 121 | ``` 122 | 123 | ### Implementing the server 124 | 125 | Now that we've generated the service definition we can implement a "handler" 126 | module that will implement each "method". 127 | 128 | ```elixir 129 | defmodule Example.HaberdasherHandler do 130 | @colors ~w|white black brown red blue| 131 | @names ["bowler", "baseball cap", "top hat", "derby"] 132 | 133 | def make_hat(_ctx, size) do 134 | if size.inches <= 0 do 135 | Twirp.Error.invalid_argument("I can't make a hat that small!") 136 | else 137 | %Example.Hat{ 138 | inches: size.inches, 139 | color: Enum.random(@colors), 140 | name: Enum.random(@names) 141 | } 142 | end 143 | end 144 | end 145 | ``` 146 | 147 | Separating the service and handler like this may seem a little odd but there are 148 | good reasons to do this. The most important is that it allows the service to be 149 | autogenerated again in the future. The second reason is that it allows us to 150 | easily mock service implementations for testing. 151 | 152 | ### Running the server 153 | 154 | To serve traffic Twirp provides a Plug. We use this plug to attach our service 155 | definition with our handler. 156 | 157 | ```elixir 158 | defmodule Example.Router do 159 | use Plug.Router 160 | 161 | plug Twirp.Plug, service: Example.HaberdasherService, handler: Example.HaberdasherHandler 162 | end 163 | ``` 164 | 165 | ```elixir 166 | defmodule Example.Application do 167 | use Application 168 | 169 | def start(_type, _args) do 170 | children = [ 171 | Plug.Cowboy.child_spec(scheme: :http, plug: Example.Router, options: [port: 4040]), 172 | ] 173 | 174 | opts = [strategy: :one_for_one, name: Example.Supervisor] 175 | Supervisor.start_link(children, opts) 176 | end 177 | end 178 | ``` 179 | 180 | If you start your application your plug will now be available on port 4040. 181 | 182 | ### Using the client 183 | 184 | Client definitions are generated alongside the service definition. This allows 185 | you to generate clients for your services in other applications. You can make 186 | RPC calls like so: 187 | 188 | ```elixir 189 | defmodule AnotherService.GetHats do 190 | alias Example.HaberdasherClient, as: Client 191 | alias Example.{Size, Hat} 192 | 193 | def make_a_hat(inches) do 194 | case Client.make_hat(Size.new(inches: inches)) do 195 | {:ok, %Hat{}=hat} -> 196 | hat 197 | 198 | {:error, %Twirp.Error{msg: msg}} -> 199 | Logger.error(msg) 200 | end 201 | end 202 | end 203 | ``` 204 | 205 | ### Running with Phoenix 206 | 207 | The plug can also be attached within a Phoenix Router. Example below would be accessible at `/rpc/hat` 208 | 209 | URL: `/rpc/hat/twirp/example.Haberdasher/MakeHat` or `/{prefix?}/twirp/{package}.{service}/{method}` 210 | 211 | ```elixir 212 | defmodule ExampleWeb.Router do 213 | use ExampleWeb, :router 214 | 215 | scope "/rpc" do 216 | forward "/hat", Twirp.Plug, 217 | service: Example.HaberdasherService, handler: Example.HaberdasherHandler 218 | end 219 | end 220 | ``` 221 | 222 | ### Client adapters 223 | 224 | Twirp supports either Finch or Hackney as the underlying http client. Finch is the 225 | default. If you want to configure the adapter you can pass the `adapter` option 226 | when you call `start_link`. 227 | 228 | ```elixir 229 | Client.start_link( 230 | url: "https://some.url", 231 | adapter: {Twirp.Client.Hackney, [pool_opts: [timeout: 30_000, max_connections: 100]]} 232 | ) 233 | ``` 234 | 235 | ## Should I use this? 236 | 237 | This implementation is still early but I believe that it should be ready for use. 238 | One of the benefits of twirp is that the implementation is quite easy to understand. 239 | At the moment there's only about ~600 LOC in the entire lib directory so if something 240 | goes wrong it shouldn't be hard to look through it and understand the issue. 241 | 242 | -------------------------------------------------------------------------------- /lib/twirp.ex: -------------------------------------------------------------------------------- 1 | defmodule Twirp do 2 | @moduledoc """ 3 | Twirp provides an elixir implementation of the [twirp rpc framework](https://github.com/twitchtv/twirp) 4 | developed by Twitch. The protocol defines semantics for routing and 5 | serialization of RPCs based on protobufs. 6 | 7 | ## Example 8 | 9 | The canonical Twirp example is a Haberdasher service. Here's the protobuf 10 | description for the service. 11 | 12 | ```protobuf 13 | syntax = "proto3"; 14 | 15 | package example; 16 | 17 | // Haberdasher service makes hats for clients. 18 | service Haberdasher { 19 | // MakeHat produces a hat of mysterious, randomly-selected color! 20 | rpc MakeHat(Size) returns (Hat); 21 | } 22 | 23 | // Size of a Hat, in inches. 24 | message Size { 25 | int32 inches = 1; // must be > 0 26 | } 27 | 28 | // A Hat is a piece of headwear made by a Haberdasher. 29 | message Hat { 30 | int32 inches = 1; 31 | string color = 2; // anything but "invisible" 32 | string name = 3; // i.e. "bowler" 33 | } 34 | ``` 35 | 36 | We'll assume for now that this proto file lives in `priv/protos/service.proto` 37 | 38 | ### Code generation 39 | 40 | We can now use `protoc` to generate the files we need. You can run this command 41 | from the root directory of your project. 42 | 43 | $ protoc --proto_path=./priv/protos --elixir_out=./lib/example --twirp_elixir_out=./lib/example ./priv/protos/service.proto 44 | 45 | After running this command there should be 2 files located in `lib/example`. 46 | 47 | The message definitions: 48 | 49 | ```elixir 50 | defmodule Example.Size do 51 | @moduledoc false 52 | use Protobuf, syntax: :proto3 53 | 54 | @type t :: %__MODULE__{ 55 | inches: integer 56 | } 57 | 58 | defstruct [:inches] 59 | 60 | field :inches, 1, type: :int32 61 | end 62 | 63 | defmodule Example.Hat do 64 | @moduledoc false 65 | use Protobuf, syntax: :proto3 66 | 67 | @type t :: %__MODULE__{ 68 | inches: integer, 69 | color: String.t(), 70 | name: String.t() 71 | } 72 | defstruct [:inches, :color, :name] 73 | 74 | field :inches, 1, type: :int32 75 | field :color, 2, type: :string 76 | field :name, 3, type: :string 77 | end 78 | ``` 79 | 80 | The service and client definition: 81 | 82 | ```elixir 83 | defmodule Example.HaberdasherService do 84 | @moduledoc false 85 | use Twirp.Service 86 | 87 | package "example" 88 | service "Haberdasher" 89 | 90 | rpc :MakeHat, Example.Size, Example.Hat, :make_hat 91 | end 92 | 93 | defmodule Example.HaberdasherClient do 94 | @moduledoc false 95 | use Twirp.Client, service: Example.HaberdasherService 96 | end 97 | ``` 98 | 99 | ### Implementing the server 100 | 101 | Now that we've generated the service definition we can implement a "handler" 102 | module that will implement each "method". 103 | 104 | ```elixir 105 | defmodule Example.HaberdasherHandler do 106 | @colors ~w|white black brown red blue| 107 | @names ["bowler", "baseball cap", "top hat", "derby"] 108 | 109 | def make_hat(_ctx, size) do 110 | if size <= 0 do 111 | Twirp.Error.invalid_argument("I can't make a hat that small!") 112 | else 113 | %Haberdasher.Hat{ 114 | inches: size.inches, 115 | color: Enum.random(@colors), 116 | name: Enum.random(@names) 117 | } 118 | end 119 | end 120 | end 121 | ``` 122 | 123 | Separating the service and handler like this may seem a little odd but there are 124 | good reasons to do this. The most important is that it allows the service to be 125 | autogenerated again in the future. The second reason is that it allows us to 126 | easily mock service implementations for testing. 127 | 128 | ### Running the server 129 | 130 | To serve traffic Twirp provides a Plug. We use this plug to attach our service 131 | definition with our handler. 132 | 133 | ```elixir 134 | defmodule Example.Router do 135 | use Plug.Router 136 | 137 | plug Twirp.Plug, 138 | service: Haberdasher.HatMakerService, 139 | handler: Haberdasher.HatMakerHandler 140 | end 141 | ``` 142 | 143 | ```elixir 144 | defmodule Example.Application do 145 | use Application 146 | 147 | def start(_type, _args) do 148 | children = [ 149 | Plug.Cowboy.child_spec(scheme: :http, plug: Example.Router, options: [port: 4040]), 150 | ] 151 | 152 | opts = [strategy: :one_for_one, name: Example.Supervisor] 153 | Supervisor.start_link(children, opts) 154 | end 155 | end 156 | ``` 157 | 158 | If you start your application your plug will now be available on port 4040. 159 | 160 | ### Using the client 161 | 162 | Client definitions are generated alongside the service definition. This allows 163 | you to generate clients for your services in other applications. You can make 164 | RPC calls like so: 165 | 166 | ```elixir 167 | defmodule AnotherService.GetHats do 168 | alias Example.HaberdasherClient, as: Client 169 | alias Example.{Size, Hat} 170 | 171 | def make_a_hat(inches) do 172 | case Client.make_hat(Size.new(inches: inches)) do 173 | {:ok, %Hat{}=hat} -> 174 | hat 175 | 176 | {:error, %Twirp.Error{msg: msg}} -> 177 | Logger.error(msg) 178 | end 179 | end 180 | end 181 | ``` 182 | """ 183 | end 184 | -------------------------------------------------------------------------------- /lib/twirp/client/adapter.ex: -------------------------------------------------------------------------------- 1 | defmodule Twirp.Client.Adapter do 2 | @moduledoc false 3 | end 4 | 5 | defmodule Twirp.Client.AdapterError do 6 | defexception [:message] 7 | 8 | @impl true 9 | def exception(adapter) do 10 | name = adapter_name(adapter) 11 | msg = """ 12 | It looks like you're trying to use #{name} as your Twirp adapter, 13 | but haven't added #{name} to your dependencies. 14 | Please add :#{adapter} to your dependencies: 15 | """ 16 | 17 | %__MODULE__{message: msg} 18 | end 19 | 20 | defp adapter_name(:finch), do: "Finch" 21 | defp adapter_name(:hackney), do: "Hackney" 22 | end 23 | -------------------------------------------------------------------------------- /lib/twirp/client/finch.ex: -------------------------------------------------------------------------------- 1 | defmodule Twirp.Client.Finch do 2 | @moduledoc false 3 | alias Twirp.Client.AdapterError 4 | 5 | require Logger 6 | 7 | def start_link(opts) do 8 | if Code.ensure_loaded?(Finch) do 9 | opts = Keyword.new(opts) 10 | Finch.start_link(opts) 11 | else 12 | raise AdapterError, :finch 13 | end 14 | end 15 | 16 | def request(client, ctx, path, payload) do 17 | # The connect_timeout here doesn't exist in finch as of version 0.6. 18 | # I'm including it here so that it'll work once this timeout gets added 19 | # in. 20 | opts = [ 21 | pool_timeout: ctx[:connect_deadline] || 1_000, 22 | connect_timeout: ctx[:connect_deadline] || 1_000, 23 | receive_timeout: ctx.deadline, 24 | ] 25 | request = Finch.build(:post, path, ctx.headers, payload) 26 | Finch.request(request, client, opts) 27 | end 28 | end 29 | -------------------------------------------------------------------------------- /lib/twirp/client/hackney.ex: -------------------------------------------------------------------------------- 1 | defmodule Twirp.Client.Hackney do 2 | @moduledoc false 3 | alias Twirp.Client.AdapterError 4 | 5 | def start_link(opts) do 6 | if Code.ensure_loaded?(:hackney) do 7 | try do 8 | pool_opts = opts[:pool_opts] || [{:timeout, 30_000}, {:max_connections, 100}] 9 | :hackney_pool.start_pool(opts.name, pool_opts) 10 | :ignore 11 | catch 12 | _, _ -> 13 | # This can fail if the pool is already started. 14 | :ignore 15 | end 16 | else 17 | raise AdapterError, :hackney 18 | end 19 | end 20 | 21 | def request(client, ctx, path, payload) do 22 | options = [ 23 | pool: client, 24 | connect_timeout: ctx[:connect_deadline] || 1_000, 25 | checkout_timeout: ctx[:connect_deadline] || 1_000, 26 | recv_timeout: ctx.deadline, 27 | ] 28 | 29 | with {:ok, status, headers, ref} <- :hackney.request(:post, path, ctx.headers, payload, options), 30 | {:ok, body} <- :hackney.body(ref) do 31 | {:ok, %{status: status, headers: format_headers(headers), body: body}} 32 | else 33 | {:error, :timeout} -> 34 | {:error, %{reason: :timeout}} 35 | 36 | {:error, :checkout_timeout} -> 37 | {:error, %{reason: :timeout}} 38 | 39 | {:error, :econnrefused} -> 40 | {:error, %{reason: :econnrefused}} 41 | 42 | error -> 43 | error 44 | end 45 | end 46 | 47 | defp format_headers(headers) do 48 | for {key, value} <- headers do 49 | {String.downcase(to_string(key)), to_string(value)} 50 | end 51 | end 52 | end 53 | -------------------------------------------------------------------------------- /lib/twirp/client/http.ex: -------------------------------------------------------------------------------- 1 | defmodule Twirp.Client.HTTP do 2 | @moduledoc false 3 | # This module handles the internals of making RPC calls. We delegate to this 4 | # from the actual client module cuz otherwise the client module is a pita 5 | # to understand due to the macros and functions its creating. 6 | alias Twirp.Encoder 7 | alias Twirp.Error 8 | 9 | def start_link(mod, opts) do 10 | mod.start_link(opts) 11 | end 12 | 13 | def call(mod, client, ctx, rpc) do 14 | path = "#{rpc.service_url}/#{rpc.method}" 15 | content_type = ctx.content_type 16 | encoded_payload = Encoder.encode(rpc.req, rpc.input_type, content_type) 17 | 18 | case mod.request(client, ctx, path, encoded_payload) do 19 | {:error, %{reason: :timeout}} -> 20 | meta = %{"error_type" => "timeout"} 21 | msg = "Deadline to receive data from the service was exceeded" 22 | {:error, Error.deadline_exceeded(msg, meta)} 23 | 24 | {:error, %{reason: reason}} -> 25 | meta = %{"error_type" => "#{reason}"} 26 | {:error, Error.unavailable("Service is down", meta)} 27 | 28 | {:error, e} -> 29 | meta = %{"error_type" => "#{inspect(e)}"} 30 | {:error, Error.internal("Unhandled client error", meta)} 31 | 32 | {:ok, %{status: status} = resp} when status != 200 -> 33 | {:error, build_error(resp, rpc)} 34 | 35 | {:ok, %{status: 200} = resp} -> 36 | handle_success(resp, rpc, content_type) 37 | end 38 | end 39 | 40 | defp handle_success(resp, rpc, content_type) do 41 | resp_content_type = resp_header(resp, "content-type") 42 | 43 | if resp_content_type && String.starts_with?(resp_content_type, content_type) do 44 | Encoder.decode(resp.body, rpc.output_type, content_type) 45 | else 46 | {:error, 47 | Error.internal( 48 | ~s|Expected response Content-Type "#{content_type}" but found #{resp_content_type || "nil"}| 49 | )} 50 | end 51 | end 52 | 53 | defp build_error(resp, _rpc) do 54 | status = resp.status 55 | 56 | cond do 57 | http_redirect?(status) -> 58 | location = resp_header(resp, "location") 59 | 60 | meta = %{ 61 | "http_error_from_intermediary" => "true", 62 | "not_a_twirp_error_because" => "Redirects not allowed on Twirp requests", 63 | "status_code" => Integer.to_string(status), 64 | "location" => location 65 | } 66 | 67 | msg = "Unexpected HTTP Redirect from location=#{location}" 68 | Error.internal(msg, meta) 69 | 70 | true -> 71 | case Encoder.decode_json(resp.body) do 72 | {:ok, %{"code" => code, "msg" => msg} = error} -> 73 | if Error.valid_code?(code) do 74 | # Its safe to convert to an atom here since all the codes are already 75 | # created and loaded. If we explode we explode. 76 | Error.new(String.to_existing_atom(code), msg, error["meta"] || %{}) 77 | else 78 | Error.internal("Invalid Twirp error code: #{code}", %{ 79 | "invalid_code" => code, 80 | "body" => resp.body 81 | }) 82 | end 83 | 84 | {:ok, _} -> 85 | msg = "Response is JSON but it has no \"code\" attribute" 86 | intermediary_error(status, msg, resp.body) 87 | 88 | {:error, _} -> 89 | intermediary_error(status, "Response is not JSON", resp.body) 90 | end 91 | end 92 | end 93 | 94 | defp intermediary_error(status, reason, body) do 95 | meta = %{ 96 | "http_error_from_intermediary" => "true", 97 | "not_a_twirp_error_because" => reason, 98 | "status_code" => Integer.to_string(status), 99 | "body" => body 100 | } 101 | 102 | case status do 103 | 400 -> Error.internal("internal", meta) 104 | 401 -> Error.unauthenticated("unauthenticated", meta) 105 | 403 -> Error.permission_denied("permission denied", meta) 106 | 404 -> Error.bad_route("bad route", meta) 107 | s when s in [429, 502, 503, 504] -> Error.unavailable("unavailable", meta) 108 | _ -> Error.unknown("unknown", meta) 109 | end 110 | end 111 | 112 | defp http_redirect?(status) do 113 | 300 <= status && status <= 399 114 | end 115 | 116 | defp resp_header(resp, header) do 117 | case Enum.find(resp.headers, fn {h, _} -> h == header end) do 118 | {^header, value} -> 119 | value 120 | 121 | _ -> 122 | nil 123 | end 124 | end 125 | end 126 | -------------------------------------------------------------------------------- /lib/twirp/encoder.ex: -------------------------------------------------------------------------------- 1 | defmodule Twirp.Encoder do 2 | @moduledoc false 3 | 4 | # Encodes and Decodes messages based on the requests content-type header. 5 | # For json we delegate to Jason. for protobuf responses we use the input or 6 | # output types. 7 | 8 | @json "application/json" 9 | @proto "application/protobuf" 10 | 11 | @valid_types [@json, @proto] 12 | 13 | def valid_type?([]), do: false 14 | def valid_type?([type]) when type in @valid_types, do: true 15 | def valid_type?(type) when type in @valid_types, do: true 16 | def valid_type?(_), do: false 17 | 18 | def type(:proto), do: @proto 19 | def type(:json), do: @json 20 | 21 | def proto?(content_type), do: content_type == @proto 22 | 23 | def json?(content_type), do: content_type == @json 24 | 25 | def decode(bytes, input, @json <> _) when is_binary(bytes) do 26 | # TODO - Write tests for atoms! failing and for decoding failing 27 | # TODO - Do better validation of json input 28 | case Jason.decode(bytes, keys: :atoms!) do 29 | {:ok, body} -> 30 | {:ok, input.new(body)} 31 | 32 | {:error, e} -> 33 | {:error, e} 34 | end 35 | end 36 | 37 | def decode(map, input, @json <> _) do 38 | struct = 39 | map 40 | |> to_atom_keys 41 | |> input.new 42 | 43 | {:ok, struct} 44 | rescue 45 | e -> 46 | {:error, e} 47 | end 48 | 49 | def decode(bytes, input, @proto <> _) do 50 | payload = input.decode(bytes) 51 | 52 | {:ok, payload} 53 | catch 54 | :error, reason -> 55 | {:error, reason} 56 | end 57 | 58 | def decode_json(bytes) do 59 | Jason.decode(bytes) 60 | end 61 | 62 | def encode(payload, _output, @json <> _) do 63 | payload 64 | |> strip_structs() 65 | |> Jason.encode!() 66 | end 67 | 68 | def encode(payload, output, @proto <> _) do 69 | output.encode(payload) 70 | end 71 | 72 | defp strip_structs(list) when is_list(list) do 73 | Enum.map(list, &strip_structs/1) 74 | end 75 | 76 | defp strip_structs(map) when is_map(map) do 77 | map 78 | |> Map.drop([:__struct__]) 79 | |> Enum.into(%{}, fn {k, v} -> {k, strip_structs(v)} end) 80 | end 81 | 82 | defp strip_structs(any), do: any 83 | 84 | defp to_atom_keys(map) when is_map(map) do 85 | for {key, value} <- map, into: %{} do 86 | k = if is_binary(key), do: String.to_existing_atom(key), else: key 87 | v = to_atom_keys(value) 88 | {k, v} 89 | end 90 | end 91 | 92 | defp to_atom_keys(list) when is_list(list) do 93 | for item <- list do 94 | to_atom_keys(item) 95 | end 96 | end 97 | 98 | defp to_atom_keys(other) do 99 | other 100 | end 101 | end 102 | -------------------------------------------------------------------------------- /lib/twirp/error.ex: -------------------------------------------------------------------------------- 1 | defmodule Twirp.Error do 2 | @moduledoc """ 3 | This module defines the different error codes as specified in 4 | https://twitchtv.github.io/twirp/docs/spec_v5.html#error-codes. 5 | 6 | We provide a function for each error code to that its easy for users to return 7 | errors in their handlers. 8 | """ 9 | 10 | import Norm 11 | 12 | @error_code_to_http_status %{ 13 | canceled: 408, # RequestTimeout 14 | invalid_argument: 400, # BadRequest 15 | deadline_exceeded: 408, # RequestTimeout 16 | not_found: 404, # Not Found 17 | bad_route: 404, # Not Found 18 | already_exists: 409, # Conflict 19 | permission_denied: 403, # Forbidden 20 | unauthenticated: 401, # Unauthorized 21 | resource_exhausted: 429, # Too Many Requests 22 | failed_precondition: 412, # Precondition Failed 23 | aborted: 409, # Conflict 24 | out_of_range: 400, # Bad Request 25 | 26 | internal: 500, # Internal Server Error 27 | unknown: 500, # Internal Server Error 28 | unimplemented: 501, # Not Implemented 29 | unavailable: 503, # Service Unavailable 30 | data_loss: 500, # Internal Server Error 31 | } 32 | 33 | @error_codes Map.keys(@error_code_to_http_status) 34 | @error_code_strings for code <- @error_codes, do: Atom.to_string(code) 35 | 36 | defexception ~w|code msg meta|a 37 | 38 | @type t :: %__MODULE__{ 39 | code: atom(), 40 | msg: binary(), 41 | meta: %{atom() => binary()} 42 | } 43 | 44 | for code <- @error_codes do 45 | def unquote(code)(msg, meta \\ %{}) do 46 | new(unquote(code), msg, meta) 47 | end 48 | end 49 | 50 | def valid_code?(code) when is_atom(code), do: code in @error_codes 51 | def valid_code?(code) when is_binary(code), do: code in @error_code_strings 52 | 53 | def code_to_status(code) when code in @error_codes do 54 | @error_code_to_http_status[code] 55 | end 56 | 57 | def s do 58 | schema(%__MODULE__{ 59 | code: spec(is_atom() and (& &1 in @error_codes)), 60 | msg: spec(is_binary()), 61 | meta: map_of(spec(is_binary()), spec(is_binary())), 62 | }) 63 | end 64 | 65 | def new(code, msg, meta \\ %{}) do 66 | conform!(%__MODULE__{code: code, msg: msg, meta: meta}, s()) 67 | end 68 | 69 | @impl true 70 | def message(%__MODULE__{msg: msg}), do: msg 71 | 72 | defimpl Jason.Encoder do 73 | def encode(struct, opts) do 74 | map = if struct.meta == %{} do 75 | Map.take(struct, [:code, :msg]) 76 | else 77 | Map.take(struct, [:code, :msg, :meta]) 78 | end 79 | 80 | Jason.Encode.map(map, opts) 81 | end 82 | end 83 | end 84 | -------------------------------------------------------------------------------- /lib/twirp/plug.ex: -------------------------------------------------------------------------------- 1 | defmodule Twirp.Plug do 2 | @moduledoc """ 3 | Provides a plug that takes service and handler module. If the request is 4 | directed at the "twirp" endpoint then the plug will intercept the conn and 5 | process it. Otherwise it allows the conn to pass through. This is a deviation 6 | from the twirp specification but it allows users to include twirp services 7 | into their existing plug stacks. 8 | 9 | You can use the plug like so: 10 | 11 | ```elixir 12 | plug Twirp.Plug, 13 | service: MyService, 14 | handler: MyHandler, 15 | ``` 16 | """ 17 | @content_type "content-type" 18 | 19 | alias Twirp.Encoder 20 | alias Twirp.Error 21 | alias Twirp.Telemetry 22 | 23 | import Plug.Conn 24 | import Norm 25 | 26 | def env_s do 27 | schema(%{ 28 | content_type: spec(is_binary()), 29 | method_name: spec(is_atom()), 30 | handler_fn: spec(is_atom()), 31 | input: spec(is_map()), 32 | input_type: spec(is_atom()), 33 | output_type: spec(is_atom()), 34 | http_response_headers: map_of(spec(is_binary()), spec(is_binary())), 35 | }) 36 | end 37 | 38 | def hook_result_s do 39 | alt( 40 | env: selection(env_s()), 41 | error: schema(%Twirp.Error{}) 42 | ) 43 | end 44 | 45 | def init(args) do 46 | handler = 47 | args 48 | |> Keyword.fetch!(:handler) 49 | 50 | Code.ensure_compiled(handler) 51 | 52 | service_def = 53 | args 54 | |> Keyword.fetch!(:service) 55 | |> apply(:definition, []) 56 | |> Norm.conform!(Twirp.Service.s()) 57 | 58 | hooks = %{ 59 | before: Keyword.get(args, :before, []), 60 | on_success: Keyword.get(args, :on_success, []), 61 | on_error: Keyword.get(args, :on_error, []), 62 | on_exception: Keyword.get(args, :on_exception, []), 63 | } 64 | 65 | rpc_defs = 66 | for rpc <- service_def.rpcs, 67 | do: {"#{rpc.method}", rpc}, 68 | into: %{} 69 | 70 | service_def = 71 | service_def 72 | |> Map.put(:rpcs, rpc_defs) 73 | |> Map.put(:full_name, Twirp.Service.full_name(service_def)) 74 | 75 | {service_def, handler, hooks} 76 | end 77 | 78 | def call(%{path_info: ["twirp", full_name, method]}=conn, {%{full_name: full_name}=service, handler, hooks}) do 79 | call(%{conn | path_info: [full_name, method]}, {service, handler, hooks}) 80 | end 81 | 82 | def call(%{path_info: [full_name, method]}=conn, {%{full_name: full_name}=service, handler, hooks}) do 83 | env = %{} 84 | metadata = %{} 85 | start = Telemetry.start(:call, metadata) 86 | 87 | try do 88 | with {:ok, env} <- validate_req(conn, method, service), 89 | {:ok, env, conn} <- get_input(env, conn), 90 | {:ok, env} <- call_before_hooks(env, conn, hooks), 91 | {:ok, output} <- call_handler(handler, env) 92 | do 93 | # We're safe to just get the output because call_handler has handled 94 | # the error case for us 95 | resp = Encoder.encode(output, env.output_type, env.content_type) 96 | 97 | env = Map.put(env, :output, resp) 98 | call_on_success_hooks(env, hooks) 99 | 100 | metadata = 101 | metadata 102 | |> Map.put(:content_type, env.content_type) 103 | |> Map.put(:method, env.method_name) 104 | 105 | Telemetry.stop(:call, start, metadata) 106 | 107 | conn 108 | |> put_resp_content_type(env.content_type) 109 | |> send_resp(200, resp) 110 | |> halt() 111 | else 112 | {:error, env, error} -> 113 | metadata = 114 | metadata 115 | |> Map.put(:content_type, env.content_type) 116 | |> Map.put(:method, env.method_name) 117 | |> Map.put(:error, error) 118 | Telemetry.stop(:call, start, metadata) 119 | call_on_error_hooks(hooks, env, error) 120 | send_error(conn, error) 121 | end 122 | rescue 123 | exception -> 124 | try do 125 | call_on_exception_hooks(hooks, env, exception) 126 | Telemetry.exception(:call, start, :error, exception, __STACKTRACE__, metadata) 127 | error = Error.internal(Exception.message(exception)) 128 | call_on_error_hooks(hooks, env, error) 129 | send_error(conn, error) 130 | rescue 131 | hook_e -> 132 | Telemetry.exception(:call, start, :error, hook_e, __STACKTRACE__, metadata) 133 | error = Error.internal(Exception.message(hook_e)) 134 | call_on_error_hooks(hooks, env, error) 135 | send_error(conn, error) 136 | end 137 | end 138 | end 139 | 140 | def call(conn, _opts) do 141 | conn 142 | end 143 | 144 | def validate_req(conn, method, %{rpcs: rpcs}) do 145 | content_type = content_type(conn) 146 | env = %{ 147 | content_type: content_type, 148 | http_response_headers: %{}, 149 | method_name: method, 150 | } 151 | 152 | cond do 153 | conn.method != "POST" -> 154 | {:error, env, bad_route("HTTP request must be POST", conn)} 155 | 156 | !Encoder.valid_type?(content_type) -> 157 | {:error, env, bad_route("Unexpected Content-Type: #{content_type || "nil"}", conn)} 158 | 159 | rpcs[method] == nil -> 160 | {:error, env, bad_route("Invalid rpc method: #{method}", conn)} 161 | 162 | true -> 163 | rpc = rpcs[method] 164 | 165 | env = Map.merge(env, %{ 166 | content_type: content_type, 167 | http_response_headers: %{}, 168 | method_name: rpc.method, 169 | input_type: rpc.input, 170 | output_type: rpc.output, 171 | handler_fn: rpc.handler_fn, 172 | }) 173 | 174 | {:ok, conform!(env, env_s())} 175 | end 176 | end 177 | 178 | defp get_input(env, conn) do 179 | with {:ok, body, conn} <- get_body(conn, env) do 180 | case Encoder.decode(body, env.input_type, env.content_type) do 181 | {:ok, decoded} -> 182 | {:ok, Map.put(env, :input, decoded), conn} 183 | 184 | _error -> 185 | msg = "Invalid request body for rpc method: #{env.method_name}" 186 | error = bad_route(msg, conn) 187 | {:error, env, error} 188 | end 189 | end 190 | end 191 | 192 | defp get_body(conn, env) do 193 | # If we're in a phoenix endpoint or an established plug router than the 194 | # user is probably already using a plug parser and the body will be 195 | # empty. We need to check to see if we have body params which is an 196 | # indication that our json has already been parsed. Limiting this to 197 | # only json payloads since the user most likely doesn't have a protobuf 198 | # parser already set up and I want to limit this potentially surprising 199 | # behaviour. 200 | if Encoder.json?(env.content_type) and body_params?(conn) do 201 | {:ok, conn.body_params, conn} 202 | else 203 | case apply(Plug.Conn, :read_body, [conn]) do 204 | {:ok, body, conn} -> 205 | {:ok, body, conn} 206 | 207 | _ -> 208 | {:error, env, Error.internal("req_body has already been read or is too large to read")} 209 | end 210 | end 211 | end 212 | 213 | defp body_params?(conn) do 214 | case conn.body_params do 215 | %Plug.Conn.Unfetched{} -> false 216 | _ -> true 217 | end 218 | end 219 | 220 | defp call_handler(handler, %{output_type: output_type}=env) do 221 | env = conform!(env, selection(env_s())) 222 | 223 | if function_exported?(handler, env.handler_fn, 2) do 224 | case apply(handler, env.handler_fn, [env, env.input]) do 225 | %Error{}=error -> 226 | {:error, env, error} 227 | 228 | %{__struct__: s}=resp when s == output_type -> 229 | {:ok, resp} 230 | 231 | other -> 232 | msg = "Handler method #{env.handler_fn} expected to return one of #{env.output_type} or Twirp.Error but returned #{inspect other}" 233 | {:error, env, Error.internal(msg)} 234 | end 235 | else 236 | {:error, env, Error.unimplemented("Handler function #{env.handler_fn} is not implemented")} 237 | end 238 | end 239 | 240 | def call_before_hooks(env, conn, hooks) do 241 | result = Enum.reduce_while(hooks.before, env, fn f, updated_env -> 242 | result = f.(conn, updated_env) 243 | 244 | case conform!(result, hook_result_s()) do 245 | {:error, err} -> {:halt, {:error, updated_env, err}} 246 | {:env, next_env} -> {:cont, next_env} 247 | end 248 | end) 249 | 250 | case result do 251 | {:error, env, err} -> 252 | {:error, env, err} 253 | 254 | env -> 255 | {:ok, env} 256 | end 257 | end 258 | 259 | def call_on_success_hooks(env, hooks) do 260 | for hook <- hooks.on_success do 261 | hook.(env) 262 | end 263 | end 264 | 265 | def call_on_error_hooks(hooks, env, error) do 266 | for hook <- hooks.on_error do 267 | hook.(env, error) 268 | end 269 | end 270 | 271 | def call_on_exception_hooks(hooks, env, exception) do 272 | for hook <- hooks.on_exception do 273 | hook.(env, exception) 274 | end 275 | end 276 | 277 | defp content_type(conn) do 278 | Enum.at(get_req_header(conn, @content_type), 0) 279 | end 280 | 281 | defp send_error(conn, error) do 282 | content_type = Encoder.type(:json) 283 | body = Encoder.encode(error, nil, content_type) 284 | 285 | conn 286 | |> put_resp_content_type(content_type) 287 | |> send_resp(Error.code_to_status(error.code), body) 288 | |> halt() 289 | end 290 | 291 | defp bad_route(msg, conn) do 292 | Error.bad_route(msg, %{"twirp_invalid_route" => "#{conn.method} #{conn.request_path}"}) 293 | end 294 | end 295 | -------------------------------------------------------------------------------- /lib/twirp/protoc/cli.ex: -------------------------------------------------------------------------------- 1 | defmodule Twirp.Protoc.CLI do 2 | @moduledoc false 3 | # Almost all of this generation stuff is lifted from the elixr protobuf library. 4 | # I don't love the way its implemented but it was the fastest path forward for 5 | # supporting generation of services. I'm going to revisit in the future 6 | # because I barely understand how this code works. 7 | 8 | def main(_) do 9 | # https://groups.google.com/forum/#!topic/elixir-lang-talk/T5enez_BBTI 10 | :io.setopts(:standard_io, encoding: :latin1) 11 | bin = IO.binread(:all) 12 | request = Protobuf.Decoder.decode(bin, Google.Protobuf.Compiler.CodeGeneratorRequest) 13 | 14 | ctx = 15 | %Protobuf.Protoc.Context{} 16 | |> Protobuf.Protoc.CLI.parse_params(request.parameter || "") 17 | |> Protobuf.Protoc.CLI.find_types(request.proto_file) 18 | 19 | files = 20 | request.proto_file 21 | |> Enum.filter(fn desc -> Enum.member?(request.file_to_generate, desc.name) end) 22 | |> Enum.map(&convert_to_maps/1) 23 | |> Enum.map(&add_comments_to_methods/1) 24 | |> Enum.map(fn desc -> Twirp.Protoc.Generator.generate(ctx, desc) end) 25 | 26 | response = Google.Protobuf.Compiler.CodeGeneratorResponse.new(file: files) 27 | IO.binwrite(Protobuf.Encoder.encode(response)) 28 | end 29 | 30 | defp add_comments_to_methods(desc) do 31 | import Access 32 | 33 | # Protobuf elixir has no way to find the actual field numbers. But we need 34 | # them in order to find the correct service and rpc definition. It just so 35 | # happens that the "service" field on the descriptor is number 6 and the method descriptor 36 | # is number 2. So we explicitly check for that and then move on. 37 | comments = 38 | desc.source_code_info.location 39 | |> Enum.reject(fn loc -> loc.leading_comments == nil end) 40 | |> Enum.filter(fn loc -> match?([6, _, 2, _], loc.path) end) 41 | |> Enum.map(fn %{path: [6, service, 2, method], leading_comments: comments} -> {service, method, comments} end) 42 | 43 | Enum.reduce(comments, desc, fn {service, method, comment}, desc -> 44 | desc 45 | |> put_in([:service, at(service), :method, at(method), :comments], comment) 46 | end) 47 | end 48 | 49 | defp convert_to_maps(desc) do 50 | services = Enum.map(desc.service, fn s -> 51 | methods = Enum.map(s.method, fn m -> 52 | Map.from_struct(m) 53 | end) 54 | 55 | s 56 | |> Map.put(:method, methods) 57 | |> Map.from_struct 58 | end) 59 | 60 | desc 61 | |> Map.put(:service, services) 62 | |> Map.from_struct() 63 | end 64 | end 65 | -------------------------------------------------------------------------------- /lib/twirp/protoc/generator.ex: -------------------------------------------------------------------------------- 1 | defmodule Twirp.Protoc.Generator do 2 | @moduledoc false 3 | # What even does this code do? 4 | 5 | alias Twirp.Protoc.Generator.Service, as: ServiceGenerator 6 | 7 | def generate(ctx, desc) do 8 | name = new_file_name(desc.name) 9 | 10 | Google.Protobuf.Compiler.CodeGeneratorResponse.File.new( 11 | name: name, 12 | content: generate_content(ctx, desc) 13 | ) 14 | end 15 | 16 | defp new_file_name(name) do 17 | String.replace_suffix(name, ".proto", "_twirp.ex") 18 | end 19 | 20 | def generate_content(ctx, desc) do 21 | module_prefix = (desc.options && Map.get(desc.options, :elixir_module_prefix)) || (desc.package || "") 22 | ctx = %{ 23 | ctx 24 | | package: desc.package || "", 25 | syntax: syntax(desc.syntax), 26 | module_prefix: module_prefix 27 | } 28 | 29 | ctx = %{ctx | dep_type_mapping: get_dep_type_mapping(ctx, desc.dependency, desc.name)} 30 | 31 | list = ServiceGenerator.generate_list(ctx, desc.service) 32 | 33 | list 34 | |> List.flatten() 35 | |> Enum.join("\n") 36 | |> format_code() 37 | end 38 | 39 | @doc false 40 | def get_dep_pkgs(%{pkg_mapping: mapping, package: pkg}, deps) do 41 | pkgs = deps |> Enum.map(fn dep -> mapping[dep] end) 42 | pkgs = if pkg && String.length(pkg) > 0, do: [pkg | pkgs], else: pkgs 43 | Enum.sort(pkgs, &(byte_size(&2) <= byte_size(&1))) 44 | end 45 | 46 | def get_dep_type_mapping(%{global_type_mapping: global_mapping}, deps, file_name) do 47 | mapping = 48 | Enum.reduce(deps, %{}, fn dep, acc -> 49 | Map.merge(acc, global_mapping[dep]) 50 | end) 51 | 52 | Map.merge(mapping, global_mapping[file_name]) 53 | end 54 | 55 | defp syntax("proto3"), do: :proto3 56 | defp syntax(_), do: :proto2 57 | 58 | def format_code(code) do 59 | formated = 60 | if Code.ensure_loaded?(Code) && function_exported?(Code, :format_string!, 2) do 61 | code 62 | |> Code.format_string!(locals_without_parens: [rpc: 4, package: 1, service: 1]) 63 | |> IO.iodata_to_binary() 64 | else 65 | code 66 | end 67 | 68 | if formated == "" do 69 | formated 70 | else 71 | formated <> "\n" 72 | end 73 | end 74 | end 75 | -------------------------------------------------------------------------------- /lib/twirp/protoc/generator/service.ex: -------------------------------------------------------------------------------- 1 | defmodule Twirp.Protoc.Generator.Service do 2 | @moduledoc false 3 | # Build a service file 4 | 5 | alias Protobuf.Protoc.Generator.Util 6 | 7 | def generate_list(ctx, descs) do 8 | Enum.map(descs, fn desc -> generate(ctx, desc) end) 9 | end 10 | 11 | def generate(ctx, desc) do 12 | # service can't be nested 13 | mod_name = Util.mod_name(ctx, [Macro.camelize(desc.name)]) 14 | methods = Enum.map(desc.method, fn m -> generate_service_method(ctx, m) end) 15 | 16 | Twirp.Protoc.Template.service(mod_name, "#{ctx.package}", desc.name, methods) 17 | end 18 | 19 | defp generate_service_method(ctx, m) do 20 | input = Util.type_from_type_name(ctx, m.input_type) 21 | output = Util.type_from_type_name(ctx, m.output_type) 22 | handler_fn = Macro.underscore(m.name) 23 | comments = String.trim(m[:comments] || "") 24 | 25 | %{name: m.name, input: input, output: output, handler_fn: handler_fn, comments: comments} 26 | end 27 | end 28 | -------------------------------------------------------------------------------- /lib/twirp/protoc/template.ex: -------------------------------------------------------------------------------- 1 | defmodule Twirp.Protoc.Template do 2 | @moduledoc false 3 | # Sets up the service template. I'm not even sure all of this ceremony is 4 | # worth it. 5 | 6 | @svc_tmpl Path.expand("./templates/service.ex.eex", :code.priv_dir(:twirp)) 7 | 8 | require EEx 9 | 10 | EEx.function_from_file(:def, :service, @svc_tmpl, [:mod_name, :package, :service_name, :methods]) 11 | end 12 | -------------------------------------------------------------------------------- /lib/twirp/service.ex: -------------------------------------------------------------------------------- 1 | defmodule Twirp.Service do 2 | @moduledoc """ 3 | Provides a DSL and some convenience functions for defining twirp service 4 | definitions. 5 | """ 6 | import Norm 7 | 8 | defmacro __using__(_opts) do 9 | quote do 10 | import Twirp.Service 11 | 12 | @rpcs [] 13 | 14 | @before_compile Twirp.Service 15 | end 16 | end 17 | 18 | def s do 19 | schema(%{ 20 | package: spec(is_binary()), 21 | service: spec(is_binary()), 22 | rpcs: coll_of(rpc_s()) 23 | }) 24 | end 25 | 26 | def rpc_s do 27 | schema(%{ 28 | method: spec(is_atom()), 29 | input: spec(is_atom()), 30 | output: spec(is_atom()), 31 | handler_fn: spec(is_atom()), 32 | }) 33 | end 34 | 35 | def full_name(service) do 36 | "#{service.package}.#{service.service}" 37 | end 38 | 39 | defmacro package(str) do 40 | quote do 41 | @package unquote(str) 42 | end 43 | end 44 | 45 | defmacro service(str) do 46 | quote do 47 | @service unquote(str) 48 | end 49 | end 50 | 51 | defmacro rpc(method, input, output, f) do 52 | quote do 53 | rpc = %{ 54 | method: unquote(method), 55 | input: unquote(input), 56 | output: unquote(output), 57 | handler_fn: unquote(f) 58 | } 59 | 60 | @rpcs [rpc | @rpcs] 61 | end 62 | end 63 | 64 | # Build everything we need to be able to dispatch our rpcs correctly 65 | @doc false 66 | defmacro __before_compile__(_env) do 67 | quote do 68 | def definition do 69 | conform!(%{package: @package, service: @service, rpcs: @rpcs}, s()) 70 | end 71 | end 72 | end 73 | end 74 | -------------------------------------------------------------------------------- /lib/twirp/telemetry.ex: -------------------------------------------------------------------------------- 1 | defmodule Twirp.Telemetry do 2 | @moduledoc """ 3 | Provides telemetry for twirp clients and servers 4 | 5 | Twirp executes the following events: 6 | 7 | * `[:twirp, :rpc, :start]` - Executed before making an rpc call to another service. 8 | 9 | #### Measurements 10 | 11 | * `:system_time` - The system time 12 | 13 | #### Metadata 14 | 15 | * `:client` - The client module issuing the call. 16 | * `:method` - The RPC method 17 | * `:service` - The url for the service 18 | 19 | * `[:twirp, :rpc, :stop]` - Executed after a connection is retrieved from the pool. 20 | 21 | #### Measurements 22 | 23 | * `:duration` - Duration to send an rpc to a service and wait for a response. 24 | 25 | #### Metadata 26 | 27 | * `:client` - The client module issuing the call. 28 | * `:method` - The RPC method 29 | * `:service` - The url for the service 30 | * `:error` - Optional key. If the call resulted in an error this key will be present along with the Twirp Error. 31 | 32 | * `[:twirp, :call, :start]` - Executed before the twirp handler is called 33 | 34 | #### Measurements 35 | 36 | * `:system_time` - The system time 37 | 38 | #### Metadata 39 | 40 | There is no metadata for this event. 41 | 42 | * `[:twirp, :call, :stop]` - Executed after twirp handler has been executed. 43 | 44 | #### Measurements 45 | 46 | * `:duration` - Duration to handle the rpc call. 47 | 48 | #### Metadata 49 | 50 | * `:content_type` - The content type being used, either proto or json. 51 | * `:method` - The name of the method being executed. 52 | * `:error` - Optional key. If the call resulted in an error this key will be present along with the Twirp Error. 53 | 54 | * `[:twirp, :call, :exception]` - Executed if the twirp handler raises an exception 55 | 56 | #### Measurements 57 | 58 | * `:duration` - Duration to handle the rpc call. 59 | 60 | #### Metadata 61 | 62 | * `:kind` - The kind of error that was raised. 63 | * `:error` - The exception 64 | * `:stacktrace` - The stacktrace 65 | """ 66 | 67 | @doc false 68 | def start(event, meta \\ %{}, extra_measurements \\ %{}) do 69 | start_time = System.monotonic_time() 70 | 71 | :telemetry.execute( 72 | [:twirp, event, :start], 73 | Map.merge(extra_measurements, %{system_time: System.system_time()}), 74 | meta 75 | ) 76 | 77 | start_time 78 | end 79 | 80 | @doc false 81 | def stop(event, start_time, meta \\ %{}, extra_measurements \\ %{}) do 82 | end_time = System.monotonic_time() 83 | measurements = Map.merge(extra_measurements, %{duration: end_time - start_time}) 84 | 85 | :telemetry.execute( 86 | [:twirp, event, :stop], 87 | measurements, 88 | meta 89 | ) 90 | end 91 | 92 | @doc false 93 | def exception(event, start_time, kind, reason, stack, meta \\ %{}, extra_measurements \\ %{}) do 94 | end_time = System.monotonic_time() 95 | measurements = Map.merge(extra_measurements, %{duration: end_time - start_time}) 96 | 97 | meta = 98 | meta 99 | |> Map.put(:kind, kind) 100 | |> Map.put(:error, reason) 101 | |> Map.put(:stacktrace, stack) 102 | 103 | :telemetry.execute([:twirp, event, :exception], measurements, meta) 104 | end 105 | 106 | @doc false 107 | def event(event, measurements, meta) do 108 | :telemetry.execute([:twirp, event], measurements, meta) 109 | end 110 | end 111 | -------------------------------------------------------------------------------- /mix.exs: -------------------------------------------------------------------------------- 1 | defmodule Twirp.MixProject do 2 | use Mix.Project 3 | 4 | @version "0.8.0" 5 | @source_url "https://github.com/keathley/twirp-elixir" 6 | 7 | def project do 8 | [ 9 | app: :twirp, 10 | version: @version, 11 | elixir: "~> 1.8", 12 | elixirc_paths: elixirc_paths(Mix.env()), 13 | escript: escript(), 14 | start_permanent: Mix.env() == :prod, 15 | consolidate_protocols: Mix.env() != :test, 16 | deps: deps(), 17 | 18 | dialyzer: [ 19 | plt_add_deps: :apps_direct, 20 | plt_add_apps: [:finch, :hackney], 21 | ], 22 | 23 | xref: [ 24 | exclude: [ 25 | Finch, 26 | :hackney, 27 | :hackney_pool, 28 | ] 29 | ], 30 | 31 | description: description(), 32 | package: package(), 33 | aliases: aliases(), 34 | preferred_cli_env: ["test.generation": :test], 35 | name: "Twirp", 36 | source_url: "https://github.com/keathley/twirp", 37 | docs: docs() 38 | ] 39 | end 40 | 41 | # Run "mix help compile.app" to learn about applications. 42 | def application do 43 | [ 44 | extra_applications: [:logger] 45 | ] 46 | end 47 | 48 | # Specifies which paths to compile per environment. 49 | defp elixirc_paths(:test), do: ["lib", "test/support"] 50 | defp elixirc_paths(_), do: ["lib"] 51 | 52 | def escript do 53 | [main_module: Twirp.Protoc.CLI, name: "protoc-gen-twirp_elixir", app: nil] 54 | end 55 | 56 | # Run "mix help deps" to learn about dependencies. 57 | defp deps do 58 | [ 59 | {:plug, "~> 1.13"}, 60 | {:norm, "~> 0.9"}, 61 | {:jason, "~> 1.1"}, 62 | {:protobuf, "~> 0.9"}, 63 | {:google_protos, "~>0.1"}, 64 | {:finch, "~> 0.6", optional: true}, 65 | {:hackney, "~> 1.17", optional: true}, 66 | {:telemetry, "~> 0.4 or ~> 1.0"}, 67 | 68 | {:bypass, "~> 2.1", only: [:dev, :test]}, 69 | {:credo, "~> 1.1", only: [:dev, :test], runtime: false}, 70 | {:dialyxir, "~> 1.0", only: [:dev, :test], runtime: false}, 71 | {:ex_doc, "~> 0.19", only: [:dev, :test], runtime: false}, 72 | {:plug_cowboy, "~> 2.0", only: [:dev, :test]}, 73 | {:mox, "~> 1.0", only: [:test]}, 74 | ] 75 | end 76 | 77 | def aliases do 78 | [ 79 | "test.generation": [ 80 | "escript.build", 81 | "escript.install --force", 82 | &generate_protos/1, 83 | "test" 84 | ] 85 | ] 86 | end 87 | 88 | defp generate_protos(_) do 89 | result = System.cmd("protoc", [ 90 | "--proto_path=./test/support", 91 | "--elixir_out=./test/support", 92 | "--twirp_elixir_out=./test/support", 93 | "./test/support/service.proto", 94 | ]) 95 | 96 | case result do 97 | {_, 0} -> 98 | :ok 99 | 100 | {error, code} -> 101 | throw {error, code} 102 | end 103 | end 104 | 105 | def description do 106 | """ 107 | Twirp provides an Elixir implementation of the Twirp RPC framework. 108 | """ 109 | end 110 | 111 | def package do 112 | [ 113 | licenses: ["Apache-2.0"], 114 | links: %{"GitHub" => @source_url} 115 | ] 116 | end 117 | 118 | def docs do 119 | [ 120 | source_ref: "v#{@version}", 121 | source_url: @source_url, 122 | main: "Twirp" 123 | ] 124 | end 125 | end 126 | -------------------------------------------------------------------------------- /mix.lock: -------------------------------------------------------------------------------- 1 | %{ 2 | "bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm", "7af5c7e09fe1d40f76c8e4f9dd2be7cebd83909f31fee7cd0e9eadc567da8353"}, 3 | "bypass": {:hex, :bypass, "2.1.0", "909782781bf8e20ee86a9cabde36b259d44af8b9f38756173e8f5e2e1fabb9b1", [:mix], [{:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 2.0", [hex: :plug_cowboy, repo: "hexpm", optional: false]}, {:ranch, "~> 1.3", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "d9b5df8fa5b7a6efa08384e9bbecfe4ce61c77d28a4282f79e02f1ef78d96b80"}, 4 | "castore": {:hex, :castore, "0.1.14", "3f6d7c7c1574c402fef29559d3f1a7389ba3524bc6a090a5e9e6abc3af65dcca", [:mix], [], "hexpm", "b34af542eadb727e6c8b37fdf73e18b2e02eb483a4ea0b52fd500bc23f052b7b"}, 5 | "certifi": {:hex, :certifi, "2.8.0", "d4fb0a6bb20b7c9c3643e22507e42f356ac090a1dcea9ab99e27e0376d695eba", [:rebar3], [], "hexpm", "6ac7efc1c6f8600b08d625292d4bbf584e14847ce1b6b5c44d983d273e1097ea"}, 6 | "cowboy": {:hex, :cowboy, "2.9.0", "865dd8b6607e14cf03282e10e934023a1bd8be6f6bacf921a7e2a96d800cd452", [:make, :rebar3], [{:cowlib, "2.11.0", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "1.8.0", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "2c729f934b4e1aa149aff882f57c6372c15399a20d54f65c8d67bef583021bde"}, 7 | "cowboy_telemetry": {:hex, :cowboy_telemetry, "0.4.0", "f239f68b588efa7707abce16a84d0d2acf3a0f50571f8bb7f56a15865aae820c", [:rebar3], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "7d98bac1ee4565d31b62d59f8823dfd8356a169e7fcbb83831b8a5397404c9de"}, 8 | "cowlib": {:hex, :cowlib, "2.11.0", "0b9ff9c346629256c42ebe1eeb769a83c6cb771a6ee5960bd110ab0b9b872063", [:make, :rebar3], [], "hexpm", "2b3e9da0b21c4565751a6d4901c20d1b4cc25cbb7fd50d91d2ab6dd287bc86a9"}, 9 | "credo": {:hex, :credo, "1.6.2", "2f82b29a47c0bb7b72f023bf3a34d151624f1cbe1e6c4e52303b05a11166a701", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2.8", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "ae9dc112bc368e7b145c547bec2ed257ef88955851c15057c7835251a17211c6"}, 10 | "dialyxir": {:hex, :dialyxir, "1.1.0", "c5aab0d6e71e5522e77beff7ba9e08f8e02bad90dfbeffae60eaf0cb47e29488", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "07ea8e49c45f15264ebe6d5b93799d4dd56a44036cf42d0ad9c960bc266c0b9a"}, 11 | "earmark_parser": {:hex, :earmark_parser, "1.4.19", "de0d033d5ff9fc396a24eadc2fcf2afa3d120841eb3f1004d138cbf9273210e8", [:mix], [], "hexpm", "527ab6630b5c75c3a3960b75844c314ec305c76d9899bb30f71cb85952a9dc45"}, 12 | "erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"}, 13 | "ex_doc": {:hex, :ex_doc, "0.28.0", "7eaf526dd8c80ae8c04d52ac8801594426ae322b52a6156cd038f30bafa8226f", [:mix], [{:earmark_parser, "~> 1.4.19", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "e55cdadf69a5d1f4cfd8477122ebac5e1fadd433a8c1022dafc5025e48db0131"}, 14 | "file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"}, 15 | "finch": {:hex, :finch, "0.10.2", "9ad27d68270d879f73f26604bb2e573d40f29bf0e907064a9a337f90a16a0312", [:mix], [{:castore, "~> 0.1", [hex: :castore, repo: "hexpm", optional: false]}, {:mint, "~> 1.3", [hex: :mint, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.4.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:nimble_pool, "~> 0.2", [hex: :nimble_pool, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "dd8b11b282072cec2ef30852283949c248bd5d2820c88d8acc89402b81db7550"}, 16 | "google_protos": {:hex, :google_protos, "0.2.0", "7c6280e288d7f04a58448444b12d937ddc8cc40dc3d1e4f61c69936ef71d4739", [:mix], [{:protobuf, "~> 0.8", [hex: :protobuf, repo: "hexpm", optional: false]}], "hexpm", "5de24802e89867cea544d0ee8cbd36f6c9c1ebd4a9fcc55276d6eeadeedcc1f5"}, 17 | "hackney": {:hex, :hackney, "1.18.0", "c4443d960bb9fba6d01161d01cd81173089686717d9490e5d3606644c48d121f", [:rebar3], [{:certifi, "~>2.8.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "~>6.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "~>1.0.0", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.3.1", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~>1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "9afcda620704d720db8c6a3123e9848d09c87586dc1c10479c42627b905b5c5e"}, 18 | "idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"}, 19 | "jason": {:hex, :jason, "1.3.0", "fa6b82a934feb176263ad2df0dbd91bf633d4a46ebfdffea0c8ae82953714946", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "53fc1f51255390e0ec7e50f9cb41e751c260d065dcba2bf0d08dc51a4002c2ac"}, 20 | "makeup": {:hex, :makeup, "1.0.5", "d5a830bc42c9800ce07dd97fa94669dfb93d3bf5fcf6ea7a0c67b2e0e4a7f26c", [:mix], [{:nimble_parsec, "~> 0.5 or ~> 1.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "cfa158c02d3f5c0c665d0af11512fed3fba0144cf1aadee0f2ce17747fba2ca9"}, 21 | "makeup_elixir": {:hex, :makeup_elixir, "0.15.2", "dc72dfe17eb240552857465cc00cce390960d9a0c055c4ccd38b70629227e97c", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.1", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "fd23ae48d09b32eff49d4ced2b43c9f086d402ee4fd4fcb2d7fad97fa8823e75"}, 22 | "makeup_erlang": {:hex, :makeup_erlang, "0.1.1", "3fcb7f09eb9d98dc4d208f49cc955a34218fc41ff6b84df7c75b3e6e533cc65f", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "174d0809e98a4ef0b3309256cbf97101c6ec01c4ab0b23e926a9e17df2077cbb"}, 23 | "metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"}, 24 | "mime": {:hex, :mime, "2.0.2", "0b9e1a4c840eafb68d820b0e2158ef5c49385d17fb36855ac6e7e087d4b1dcc5", [:mix], [], "hexpm", "e6a3f76b4c277739e36c2e21a2c640778ba4c3846189d5ab19f97f126df5f9b7"}, 25 | "mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"}, 26 | "mint": {:hex, :mint, "1.4.0", "cd7d2451b201fc8e4a8fd86257fb3878d9e3752899eb67b0c5b25b180bde1212", [:mix], [{:castore, "~> 0.1.0", [hex: :castore, repo: "hexpm", optional: true]}], "hexpm", "10a99e144b815cbf8522dccbc8199d15802440fc7a64d67b6853adb6fa170217"}, 27 | "mox": {:hex, :mox, "1.0.1", "b651bf0113265cda0ba3a827fcb691f848b683c373b77e7d7439910a8d754d6e", [:mix], [], "hexpm", "35bc0dea5499d18db4ef7fe4360067a59b06c74376eb6ab3bd67e6295b133469"}, 28 | "nimble_options": {:hex, :nimble_options, "0.4.0", "c89babbab52221a24b8d1ff9e7d838be70f0d871be823165c94dd3418eea728f", [:mix], [], "hexpm", "e6701c1af326a11eea9634a3b1c62b475339ace9456c1a23ec3bc9a847bca02d"}, 29 | "nimble_parsec": {:hex, :nimble_parsec, "1.2.1", "264fc6864936b59fedb3ceb89998c64e9bb91945faf1eb115d349b96913cc2ef", [:mix], [], "hexpm", "23c31d0ec38c97bf9adde35bc91bc8e1181ea5202881f48a192f4aa2d2cf4d59"}, 30 | "nimble_pool": {:hex, :nimble_pool, "0.2.4", "1db8e9f8a53d967d595e0b32a17030cdb6c0dc4a451b8ac787bf601d3f7704c3", [:mix], [], "hexpm", "367e8071e137b787764e6a9992ccb57b276dc2282535f767a07d881951ebeac6"}, 31 | "norm": {:hex, :norm, "0.13.0", "2c562113f3205e3f195ee288d3bd1ab903743e7e9f3282562c56c61c4d95dec4", [:mix], [{:stream_data, "~> 0.5", [hex: :stream_data, repo: "hexpm", optional: true]}], "hexpm", "447cc96dd2d0e19dcb37c84b5fc0d6842aad69386e846af048046f95561d46d7"}, 32 | "parse_trans": {:hex, :parse_trans, "3.3.1", "16328ab840cc09919bd10dab29e431da3af9e9e7e7e6f0089dd5a2d2820011d8", [:rebar3], [], "hexpm", "07cd9577885f56362d414e8c4c4e6bdf10d43a8767abb92d24cbe8b24c54888b"}, 33 | "plug": {:hex, :plug, "1.13.4", "addb6e125347226e3b11489e23d22a60f7ab74786befb86c14f94fb5f23ca9a4", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "06114c1f2a334212fe3ae567dbb3b1d29fd492c1a09783d52f3d489c1a6f4cf2"}, 34 | "plug_cowboy": {:hex, :plug_cowboy, "2.5.2", "62894ccd601cf9597e2c23911ff12798a8a18d237e9739f58a6b04e4988899fe", [:mix], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:cowboy_telemetry, "~> 0.3", [hex: :cowboy_telemetry, repo: "hexpm", optional: false]}, {:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "ea6e87f774c8608d60c8d34022a7d073bd7680a0a013f049fc62bf35efea1044"}, 35 | "plug_crypto": {:hex, :plug_crypto, "1.2.2", "05654514ac717ff3a1843204b424477d9e60c143406aa94daf2274fdd280794d", [:mix], [], "hexpm", "87631c7ad914a5a445f0a3809f99b079113ae4ed4b867348dd9eec288cecb6db"}, 36 | "protobuf": {:hex, :protobuf, "0.9.0", "9c1633ecc098f3d7ec0a00503e070541b0e1868114fff41523934888442319e7", [:mix], [{:jason, "~> 1.2", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm", "15fb7cddc5f85b8055fedaf81a9093020e4cd283647a21deb8f7de8d243abb9d"}, 37 | "ranch": {:hex, :ranch, "1.8.0", "8c7a100a139fd57f17327b6413e4167ac559fbc04ca7448e9be9057311597a1d", [:make, :rebar3], [], "hexpm", "49fbcfd3682fab1f5d109351b61257676da1a2fdbe295904176d5e521a2ddfe5"}, 38 | "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.6", "cf344f5692c82d2cd7554f5ec8fd961548d4fd09e7d22f5b62482e5aeaebd4b0", [:make, :mix, :rebar3], [], "hexpm", "bdb0d2471f453c88ff3908e7686f86f9be327d065cc1ec16fa4540197ea04680"}, 39 | "telemetry": {:hex, :telemetry, "1.0.0", "0f453a102cdf13d506b7c0ab158324c337c41f1cc7548f0bc0e130bbf0ae9452", [:rebar3], [], "hexpm", "73bc09fa59b4a0284efb4624335583c528e07ec9ae76aca96ea0673850aec57a"}, 40 | "unicode_util_compat": {:hex, :unicode_util_compat, "0.7.0", "bc84380c9ab48177092f43ac89e4dfa2c6d62b40b8bd132b1059ecc7232f9a78", [:rebar3], [], "hexpm", "25eee6d67df61960cf6a794239566599b09e17e668d3700247bc498638152521"}, 41 | } 42 | -------------------------------------------------------------------------------- /priv/templates/service.ex.eex: -------------------------------------------------------------------------------- 1 | # Generated by the protobuf compile. DO NOT EDIT! 2 | 3 | defmodule <%= mod_name %>Service do 4 | @moduledoc false 5 | use Twirp.Service 6 | 7 | package "<%= package %>" 8 | service "<%= service_name %>" 9 | 10 | <%= Enum.map methods, fn(method) -> %> 11 | rpc :<%= method.name %>, <%= method.input %>, <%= method.output %>, :<%= method.handler_fn %> 12 | <% end %> 13 | end 14 | 15 | defmodule <%= mod_name %>Client do 16 | @moduledoc """ 17 | Generated Twirp Client 18 | """ 19 | 20 | @package "<%= package %>" 21 | @service "<%= service_name %>" 22 | 23 | @type ctx :: map() 24 | 25 | <%= Enum.map methods, fn method -> %> 26 | @callback <%= method.handler_fn %>(ctx(), <%= method.input %>.t()) :: {:ok, <%= method.output %>.t()} | {:error, Twirp.Error.t()} 27 | <% end %> 28 | 29 | def child_spec(opts) do 30 | %{ 31 | id: __MODULE__, 32 | start: {__MODULE__, :start_link, [opts]} 33 | } 34 | end 35 | 36 | @doc """ 37 | Starts a new service client. 38 | 39 | ## Options 40 | * `:url` - The root url for the service. 41 | * `:content_type` - Either `:proto` or `:json` based on the desired client type. Defaults to `:proto`. 42 | * `:pool_config` - Configuration for the underlying Finch, http pool. 43 | """ 44 | def start_link(opts) do 45 | url = opts[:url] || raise ArgumentError, "#{__MODULE__} requires a `:url` option" 46 | content_type = opts[:content_type] || :proto 47 | full_path = Path.join([url, "twirp", "#{@package}.#{@service}"]) 48 | interceptors = opts[:interceptors] || [] 49 | {adapter_mod, adapter_opts} = opts[:adapter] || {Twirp.Client.Finch, pools: %{default: [size: 10, count: 1]}} 50 | http_opts = %{ 51 | name: __MODULE__, 52 | opts: adapter_opts, 53 | } 54 | 55 | :persistent_term.put({__MODULE__, :url}, full_path) 56 | :persistent_term.put({__MODULE__, :content_type}, content_type) 57 | :persistent_term.put({__MODULE__, :interceptors}, interceptors) 58 | :persistent_term.put({__MODULE__, :adapter}, {adapter_mod, adapter_opts}) 59 | Twirp.Client.HTTP.start_link(adapter_mod, http_opts) 60 | end 61 | 62 | <%= Enum.map methods, fn method -> %> 63 | <%= if method.comments != "" do %> 64 | @doc """ 65 | <%= method.comments %> 66 | """<% end %> 67 | @spec <%= method.handler_fn %>(ctx(), <%= method.input %>.t()) :: {:ok, <%= method.output %>.t()} | {:error, Twirp.Error.t()} 68 | def <%= method.handler_fn %>(ctx \\ %{}, %<%= method.input %>{}=req) do 69 | rpc(:<%= method.name %>, ctx, req, <%= method.input %>, <%= method.output %>) 70 | end 71 | <% end %> 72 | 73 | defp rpc(method, ctx, req, input_type, output_type) do 74 | service_url = :persistent_term.get({__MODULE__, :url}) 75 | interceptors = :persistent_term.get({__MODULE__, :interceptors}) 76 | {adapter_mod, _} = :persistent_term.get({__MODULE__, :adapter}) 77 | content_type = Twirp.Encoder.type(:persistent_term.get({__MODULE__, :content_type})) 78 | content_header = {"Content-Type", content_type} 79 | 80 | ctx = 81 | ctx 82 | |> Map.put(:content_type, content_type) 83 | |> Map.update(:headers, [content_header], & [content_header | &1]) 84 | |> Map.put_new(:deadline, 1_000) 85 | 86 | rpcdef = %{ 87 | service_url: service_url, 88 | method: method, 89 | req: req, 90 | input_type: input_type, 91 | output_type: output_type 92 | } 93 | 94 | metadata = %{ 95 | client: __MODULE__, 96 | method: method, 97 | service: service_url 98 | } 99 | start = Twirp.Telemetry.start(:rpc, metadata) 100 | 101 | call_chain = chain(Enum.reverse(interceptors), fn ctx, req -> 102 | case Twirp.Client.HTTP.call(adapter_mod, __MODULE__, ctx, %{rpcdef | req: req}) do 103 | {:ok, resp} -> 104 | Twirp.Telemetry.stop(:rpc, start, metadata) 105 | {:ok, resp} 106 | 107 | {:error, error} -> 108 | metadata = Map.put(metadata, :error, error) 109 | Twirp.Telemetry.stop(:rpc, start, metadata) 110 | {:error, error} 111 | end 112 | end) 113 | 114 | call_chain.(ctx, req) 115 | end 116 | 117 | defp chain([], f), do: f 118 | defp chain([func | fs], acc_f) do 119 | next = fn ctx, req -> 120 | func.(ctx, req, acc_f) 121 | end 122 | 123 | chain(fs, next) 124 | end 125 | end 126 | -------------------------------------------------------------------------------- /test/support/service.pb.ex: -------------------------------------------------------------------------------- 1 | defmodule Twirp.Test.Envelope do 2 | @moduledoc false 3 | use Protobuf, syntax: :proto3 4 | 5 | @type t :: %__MODULE__{ 6 | msg: String.t(), 7 | sub: Twirp.Test.Req.t() | nil 8 | } 9 | 10 | defstruct msg: "", 11 | sub: nil 12 | 13 | field :msg, 1, type: :string 14 | field :sub, 2, type: Twirp.Test.Req 15 | end 16 | defmodule Twirp.Test.Req do 17 | @moduledoc false 18 | use Protobuf, syntax: :proto3 19 | 20 | @type t :: %__MODULE__{ 21 | msg: String.t() 22 | } 23 | 24 | defstruct msg: "" 25 | 26 | field :msg, 1, type: :string 27 | end 28 | defmodule Twirp.Test.Resp do 29 | @moduledoc false 30 | use Protobuf, syntax: :proto3 31 | 32 | @type t :: %__MODULE__{ 33 | msg: String.t() 34 | } 35 | 36 | defstruct msg: "" 37 | 38 | field :msg, 1, type: :string 39 | end 40 | defmodule Twirp.Test.BatchReq do 41 | @moduledoc false 42 | use Protobuf, syntax: :proto3 43 | 44 | @type t :: %__MODULE__{ 45 | requests: [Twirp.Test.Req.t()] 46 | } 47 | 48 | defstruct requests: [] 49 | 50 | field :requests, 1, repeated: true, type: Twirp.Test.Req 51 | end 52 | defmodule Twirp.Test.BatchResp do 53 | @moduledoc false 54 | use Protobuf, syntax: :proto3 55 | 56 | @type t :: %__MODULE__{ 57 | responses: [Twirp.Test.Resp.t()] 58 | } 59 | 60 | defstruct responses: [] 61 | 62 | field :responses, 1, repeated: true, type: Twirp.Test.Resp 63 | end 64 | -------------------------------------------------------------------------------- /test/support/service.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | package twirp.test; 4 | 5 | option java_multiple_files = true; 6 | option java_package = "example.apps.services.grpc"; 7 | option java_outer_classname = "ProtocolProto"; 8 | 9 | message Envelope { 10 | string msg = 1; // Cannot be empty 11 | // Child message 12 | Req sub = 2; 13 | } 14 | 15 | message Req { 16 | string msg = 1; 17 | } 18 | 19 | message Resp { 20 | string msg = 1; 21 | } 22 | 23 | message BatchReq { 24 | repeated Req requests = 1; 25 | } 26 | 27 | message BatchResp { 28 | repeated Resp responses = 1; 29 | } 30 | 31 | service Echo { 32 | // Echo's some text back to you 33 | rpc Echo(Req) returns (Resp); 34 | 35 | rpc BatchEcho(BatchReq) returns (BatchResp); 36 | 37 | // Echo's some text back to you, slowly. 38 | rpc SlowEcho(Req) returns (Resp); 39 | 40 | rpc Undocumented(Req) returns (Resp); 41 | } 42 | -------------------------------------------------------------------------------- /test/support/service_twirp.ex: -------------------------------------------------------------------------------- 1 | # Generated by the protobuf compile. DO NOT EDIT! 2 | 3 | defmodule Twirp.Test.EchoService do 4 | @moduledoc false 5 | use Twirp.Service 6 | 7 | package "twirp.test" 8 | service "Echo" 9 | 10 | rpc :Echo, Twirp.Test.Req, Twirp.Test.Resp, :echo 11 | 12 | rpc :BatchEcho, Twirp.Test.BatchReq, Twirp.Test.BatchResp, :batch_echo 13 | 14 | rpc :SlowEcho, Twirp.Test.Req, Twirp.Test.Resp, :slow_echo 15 | 16 | rpc :Undocumented, Twirp.Test.Req, Twirp.Test.Resp, :undocumented 17 | end 18 | 19 | defmodule Twirp.Test.EchoClient do 20 | @moduledoc """ 21 | Generated Twirp Client 22 | """ 23 | 24 | @package "twirp.test" 25 | @service "Echo" 26 | 27 | @type ctx :: map() 28 | 29 | @callback echo(ctx(), Twirp.Test.Req.t()) :: 30 | {:ok, Twirp.Test.Resp.t()} | {:error, Twirp.Error.t()} 31 | 32 | @callback batch_echo(ctx(), Twirp.Test.BatchReq.t()) :: 33 | {:ok, Twirp.Test.BatchResp.t()} | {:error, Twirp.Error.t()} 34 | 35 | @callback slow_echo(ctx(), Twirp.Test.Req.t()) :: 36 | {:ok, Twirp.Test.Resp.t()} | {:error, Twirp.Error.t()} 37 | 38 | @callback undocumented(ctx(), Twirp.Test.Req.t()) :: 39 | {:ok, Twirp.Test.Resp.t()} | {:error, Twirp.Error.t()} 40 | 41 | def child_spec(opts) do 42 | %{ 43 | id: __MODULE__, 44 | start: {__MODULE__, :start_link, [opts]} 45 | } 46 | end 47 | 48 | @doc """ 49 | Starts a new service client. 50 | 51 | ## Options 52 | * `:url` - The root url for the service. 53 | * `:content_type` - Either `:proto` or `:json` based on the desired client type. Defaults to `:proto`. 54 | * `:pool_config` - Configuration for the underlying Finch, http pool. 55 | """ 56 | def start_link(opts) do 57 | url = opts[:url] || raise ArgumentError, "#{__MODULE__} requires a `:url` option" 58 | content_type = opts[:content_type] || :proto 59 | full_path = Path.join([url, "twirp", "#{@package}.#{@service}"]) 60 | interceptors = opts[:interceptors] || [] 61 | 62 | {adapter_mod, adapter_opts} = 63 | opts[:adapter] || {Twirp.Client.Finch, pools: %{default: [size: 10, count: 1]}} 64 | 65 | http_opts = %{ 66 | name: __MODULE__, 67 | opts: adapter_opts 68 | } 69 | 70 | :persistent_term.put({__MODULE__, :url}, full_path) 71 | :persistent_term.put({__MODULE__, :content_type}, content_type) 72 | :persistent_term.put({__MODULE__, :interceptors}, interceptors) 73 | :persistent_term.put({__MODULE__, :adapter}, {adapter_mod, adapter_opts}) 74 | Twirp.Client.HTTP.start_link(adapter_mod, http_opts) 75 | end 76 | 77 | @doc """ 78 | Echo's some text back to you 79 | """ 80 | @spec echo(ctx(), Twirp.Test.Req.t()) :: {:ok, Twirp.Test.Resp.t()} | {:error, Twirp.Error.t()} 81 | def echo(ctx \\ %{}, %Twirp.Test.Req{} = req) do 82 | rpc(:Echo, ctx, req, Twirp.Test.Req, Twirp.Test.Resp) 83 | end 84 | 85 | @spec batch_echo(ctx(), Twirp.Test.BatchReq.t()) :: 86 | {:ok, Twirp.Test.BatchResp.t()} | {:error, Twirp.Error.t()} 87 | def batch_echo(ctx \\ %{}, %Twirp.Test.BatchReq{} = req) do 88 | rpc(:BatchEcho, ctx, req, Twirp.Test.BatchReq, Twirp.Test.BatchResp) 89 | end 90 | 91 | @doc """ 92 | Echo's some text back to you, slowly. 93 | """ 94 | @spec slow_echo(ctx(), Twirp.Test.Req.t()) :: 95 | {:ok, Twirp.Test.Resp.t()} | {:error, Twirp.Error.t()} 96 | def slow_echo(ctx \\ %{}, %Twirp.Test.Req{} = req) do 97 | rpc(:SlowEcho, ctx, req, Twirp.Test.Req, Twirp.Test.Resp) 98 | end 99 | 100 | @spec undocumented(ctx(), Twirp.Test.Req.t()) :: 101 | {:ok, Twirp.Test.Resp.t()} | {:error, Twirp.Error.t()} 102 | def undocumented(ctx \\ %{}, %Twirp.Test.Req{} = req) do 103 | rpc(:Undocumented, ctx, req, Twirp.Test.Req, Twirp.Test.Resp) 104 | end 105 | 106 | defp rpc(method, ctx, req, input_type, output_type) do 107 | service_url = :persistent_term.get({__MODULE__, :url}) 108 | interceptors = :persistent_term.get({__MODULE__, :interceptors}) 109 | {adapter_mod, _} = :persistent_term.get({__MODULE__, :adapter}) 110 | content_type = Twirp.Encoder.type(:persistent_term.get({__MODULE__, :content_type})) 111 | content_header = {"Content-Type", content_type} 112 | 113 | ctx = 114 | ctx 115 | |> Map.put(:content_type, content_type) 116 | |> Map.update(:headers, [content_header], &[content_header | &1]) 117 | |> Map.put_new(:deadline, 1_000) 118 | 119 | rpcdef = %{ 120 | service_url: service_url, 121 | method: method, 122 | req: req, 123 | input_type: input_type, 124 | output_type: output_type 125 | } 126 | 127 | metadata = %{ 128 | client: __MODULE__, 129 | method: method, 130 | service: service_url 131 | } 132 | 133 | start = Twirp.Telemetry.start(:rpc, metadata) 134 | 135 | call_chain = 136 | chain(Enum.reverse(interceptors), fn ctx, req -> 137 | case Twirp.Client.HTTP.call(adapter_mod, __MODULE__, ctx, %{rpcdef | req: req}) do 138 | {:ok, resp} -> 139 | Twirp.Telemetry.stop(:rpc, start, metadata) 140 | {:ok, resp} 141 | 142 | {:error, error} -> 143 | metadata = Map.put(metadata, :error, error) 144 | Twirp.Telemetry.stop(:rpc, start, metadata) 145 | {:error, error} 146 | end 147 | end) 148 | 149 | call_chain.(ctx, req) 150 | end 151 | 152 | defp chain([], f), do: f 153 | 154 | defp chain([func | fs], acc_f) do 155 | next = fn ctx, req -> 156 | func.(ctx, req, acc_f) 157 | end 158 | 159 | chain(fs, next) 160 | end 161 | end 162 | -------------------------------------------------------------------------------- /test/test_helper.exs: -------------------------------------------------------------------------------- 1 | ExUnit.start() 2 | Application.ensure_all_started(:hackney) 3 | Application.ensure_all_started(:bypass) 4 | -------------------------------------------------------------------------------- /test/twirp/client/hackney_test.exs: -------------------------------------------------------------------------------- 1 | defmodule Twirp.Client.HackneyTest do 2 | use ExUnit.Case, async: false 3 | 4 | alias Twirp.Error 5 | 6 | alias Twirp.Test.Req 7 | alias Twirp.Test.Resp 8 | # alias Twirp.Test.EchoClient, as: Client 9 | 10 | defmodule Client do 11 | # We need a facade for testing purposes. In a real system this module is 12 | # not needed. 13 | 14 | def child_spec(opts) do 15 | %{ 16 | id: __MODULE__, 17 | start: {__MODULE__, :start_link, [opts]} 18 | } 19 | end 20 | 21 | def start_link(opts) do 22 | adapter = [adapter: {Twirp.Client.Hackney, []}] 23 | opts = Keyword.merge(adapter, opts) 24 | Twirp.Test.EchoClient.start_link(opts) 25 | end 26 | 27 | def echo(ctx \\ %{}, req), do: Twirp.Test.EchoClient.echo(ctx, req) 28 | 29 | def slow_echo(ctx \\ %{}, req), do: Twirp.Test.EchoClient.slow_echo(ctx, req) 30 | end 31 | 32 | setup tags do 33 | service = Bypass.open() 34 | base_url = "http://localhost:#{service.port}" 35 | content_type = tags[:client_type] || :proto 36 | start_supervised({Client, url: base_url, content_type: content_type}) 37 | 38 | {:ok, service: service} 39 | end 40 | 41 | test "generated clients have rpc functions defined on them" do 42 | assert {:echo, 2} in Client.__info__(:functions) 43 | end 44 | 45 | test "makes an http call if the rpc is defined", %{service: service} do 46 | Bypass.expect(service, fn conn -> 47 | assert Plug.Conn.get_req_header(conn, "content-type") == ["application/protobuf"] 48 | {:ok, body, conn} = Plug.Conn.read_body(conn) 49 | assert %Req{msg: "test"} == Req.decode(body) 50 | 51 | body = Resp.encode(Resp.new(msg: "test")) 52 | 53 | conn 54 | |> Plug.Conn.put_resp_content_type("application/protobuf") 55 | |> Plug.Conn.resp(200, body) 56 | end) 57 | 58 | resp = Client.echo(Req.new(msg: "test")) 59 | assert {:ok, Resp.new(msg: "test")} == resp 60 | end 61 | 62 | @tag client_type: :json 63 | test "json encoding and decoding", %{service: service} do 64 | Bypass.expect(service, fn conn -> 65 | assert Plug.Conn.get_req_header(conn, "content-type") == ["application/json"] 66 | {:ok, body, conn} = Plug.Conn.read_body(conn) 67 | assert %{"msg" => "Test"} == Jason.decode!(body) 68 | 69 | conn 70 | |> Plug.Conn.put_resp_content_type("application/json") 71 | |> Plug.Conn.resp(200, ~s|{"msg": "Test"}|) 72 | end) 73 | 74 | assert {:ok, resp} = Client.echo(Req.new(msg: "Test")) 75 | assert match?(%Resp{}, resp) 76 | assert resp.msg == "Test" 77 | end 78 | 79 | test "incorrect headers are returned", %{service: service} do 80 | Bypass.expect(service, fn conn -> 81 | conn 82 | |> Plug.Conn.put_resp_content_type("application/msgpack") 83 | |> Plug.Conn.resp(200, ~s|foo|) 84 | end) 85 | 86 | assert {:error, resp} = Client.echo(Req.new(msg: "test")) 87 | assert match?(%Error{code: :internal}, resp) 88 | end 89 | 90 | test "normalize headers", %{service: service} do 91 | Bypass.expect(service, fn conn -> 92 | assert Plug.Conn.get_req_header(conn, "content-type") == ["application/protobuf"] 93 | {:ok, body, conn} = Plug.Conn.read_body(conn) 94 | assert %Req{msg: "test"} == Req.decode(body) 95 | 96 | body = Resp.encode(Resp.new(msg: "test")) 97 | 98 | conn 99 | |> Plug.Conn.put_resp_header("Content-Type", "application/protobuf") 100 | |> Plug.Conn.resp(200, body) 101 | end) 102 | 103 | resp = Client.echo(Req.new(msg: "test")) 104 | assert {:ok, Resp.new(msg: "test")} == resp 105 | end 106 | 107 | test "no headers are returned", %{service: service} do 108 | Bypass.expect(service, fn conn -> 109 | conn 110 | |> Plug.Conn.resp(200, ~s|foo|) 111 | end) 112 | 113 | assert {:error, resp} = Client.echo(Req.new(msg: "test")) 114 | assert match?(%Error{code: :internal}, resp) 115 | assert resp.msg == ~s|Expected response Content-Type "application/protobuf" but found nil| 116 | end 117 | 118 | test "error is not json", %{service: service} do 119 | Bypass.expect(service, fn conn -> 120 | conn 121 | |> Plug.Conn.send_resp(503, ~s|plain text error|) 122 | end) 123 | 124 | assert {:error, resp} = Client.echo(Req.new(msg: "test")) 125 | assert resp.code == :unavailable 126 | assert resp.msg == "unavailable" 127 | assert resp.meta["http_error_from_intermediary"] == "true" 128 | assert resp.meta["not_a_twirp_error_because"] == "Response is not JSON" 129 | assert resp.meta["body"] == "plain text error" 130 | end 131 | 132 | test "error has no code", %{service: service} do 133 | Bypass.expect(service, fn conn -> 134 | conn 135 | |> Plug.Conn.put_resp_content_type("application/json") 136 | |> Plug.Conn.send_resp(500, ~s|{"msg": "I have no code"}|) 137 | end) 138 | 139 | assert {:error, resp} = Client.echo(Req.new(msg: "test")) 140 | assert resp.code == :unknown 141 | assert resp.msg == "unknown" 142 | assert resp.meta["http_error_from_intermediary"] == "true" 143 | assert resp.meta["not_a_twirp_error_because"] == "Response is JSON but it has no \"code\" attribute" 144 | end 145 | 146 | test "error has incorrect code", %{service: service} do 147 | Bypass.expect(service, fn conn -> 148 | conn 149 | |> Plug.Conn.put_resp_content_type("application/json") 150 | |> Plug.Conn.send_resp(500, ~s|{"code": "keathley", "msg": "incorrect code"}|) 151 | end) 152 | 153 | assert {:error, resp} = Client.echo(Req.new(msg: "test")) 154 | assert resp.code == :internal 155 | assert resp.msg == "Invalid Twirp error code: keathley" 156 | assert resp.meta["invalid_code"] == "keathley" 157 | end 158 | 159 | test "redirect errors", %{service: service} do 160 | Bypass.expect(service, fn conn -> 161 | url = "https://keathley.io" 162 | 163 | conn 164 | |> Plug.Conn.put_resp_header("location", url) 165 | |> Plug.Conn.send_resp(302, url) 166 | end) 167 | 168 | assert {:error, resp} = Client.echo(Req.new(msg: "test")) 169 | assert match?(%Error{code: :internal}, resp) 170 | assert resp.meta["http_error_from_intermediary"] == "true" 171 | assert resp.meta["not_a_twirp_error_because"] == "Redirects not allowed on Twirp requests" 172 | end 173 | 174 | test "connect timeouts", %{service: _service} do 175 | assert {:error, resp} = Client.echo(%{connect_deadline: 0}, Req.new(msg: "test")) 176 | assert resp.code == :deadline_exceeded 177 | assert resp.meta["error_type"] == "timeout" 178 | end 179 | 180 | test "recv timeouts", %{service: service} do 181 | Bypass.expect(service, fn _conn -> 182 | Bypass.pass(service) 183 | :timer.sleep(1_000) 184 | end) 185 | 186 | assert {:error, resp} = Client.echo(%{deadline: 1}, Req.new(msg: "test")) 187 | assert resp.code == :deadline_exceeded 188 | assert resp.meta["error_type"] == "timeout" 189 | end 190 | 191 | test "service is down", %{service: service} do 192 | Bypass.down(service) 193 | 194 | assert {:error, resp} = Client.echo(Req.new(msg: "test")) 195 | assert resp.code == :unavailable 196 | end 197 | 198 | @tag :skip 199 | test "clients are easy to stub" do 200 | Twirp.Client.Stub.new(echo: fn %{msg: "foo"} -> 201 | Error.unavailable("test") 202 | end) 203 | assert {:error, %Error{code: :unavailable}} = Client.echo(Req.new(msg: "foo")) 204 | 205 | Twirp.Client.Stub.new(echo: fn %{msg: "foo"} -> 206 | Resp.new(msg: "foo") 207 | end) 208 | assert {:ok, %Resp{msg: "foo"}} = Client.echo(Req.new(msg: "foo")) 209 | 210 | assert_raise Twirp.Client.StubError, ~r/does not define/, fn -> 211 | Twirp.Client.Stub.new() 212 | Client.echo(Req.new(msg: "foo")) 213 | end 214 | 215 | assert_raise Twirp.Client.StubError, ~r/expected to return/, fn -> 216 | Twirp.Client.Stub.new(echo: fn _ -> 217 | {:ok, Req.new(msg: "test")} 218 | end) 219 | Client.echo(Req.new(msg: "foo")) 220 | end 221 | end 222 | end 223 | -------------------------------------------------------------------------------- /test/twirp/client_test.exs: -------------------------------------------------------------------------------- 1 | defmodule Twirp.ClientTest do 2 | use ExUnit.Case, async: false 3 | 4 | alias Twirp.Error 5 | 6 | alias Twirp.Test.Req 7 | alias Twirp.Test.Resp 8 | alias Twirp.Test.EchoClient, as: Client 9 | 10 | setup tags do 11 | service = Bypass.open() 12 | base_url = "http://localhost:#{service.port}" 13 | content_type = tags[:client_type] || :proto 14 | {:ok, _} = start_supervised({Client, url: base_url, content_type: content_type}) 15 | 16 | {:ok, service: service} 17 | end 18 | 19 | test "generated clients have rpc functions defined on them" do 20 | assert {:echo, 2} in Client.__info__(:functions) 21 | end 22 | 23 | test "makes an http call if the rpc is defined", %{service: service} do 24 | Bypass.expect(service, fn conn -> 25 | assert Plug.Conn.get_req_header(conn, "content-type") == ["application/protobuf"] 26 | {:ok, body, conn} = Plug.Conn.read_body(conn) 27 | assert %Req{msg: "test"} == Req.decode(body) 28 | 29 | body = Resp.encode(Resp.new(msg: "test")) 30 | 31 | conn 32 | |> Plug.Conn.put_resp_content_type("application/protobuf") 33 | |> Plug.Conn.resp(200, body) 34 | end) 35 | 36 | resp = Client.echo(Req.new(msg: "test")) 37 | assert {:ok, Resp.new(msg: "test")} == resp 38 | end 39 | 40 | @tag client_type: :json 41 | test "json encoding and decoding", %{service: service} do 42 | Bypass.expect(service, fn conn -> 43 | assert Plug.Conn.get_req_header(conn, "content-type") == ["application/json"] 44 | {:ok, body, conn} = Plug.Conn.read_body(conn) 45 | assert %{"msg" => "Test"} == Jason.decode!(body) 46 | 47 | conn 48 | |> Plug.Conn.put_resp_content_type("application/json") 49 | |> Plug.Conn.resp(200, ~s|{"msg": "Test"}|) 50 | end) 51 | 52 | assert {:ok, resp} = Client.echo(Req.new(msg: "Test")) 53 | assert match?(%Resp{}, resp) 54 | assert resp.msg == "Test" 55 | end 56 | 57 | test "incorrect headers are returned", %{service: service} do 58 | Bypass.expect(service, fn conn -> 59 | conn 60 | |> Plug.Conn.put_resp_content_type("application/msgpack") 61 | |> Plug.Conn.resp(200, ~s|foo|) 62 | end) 63 | 64 | assert {:error, resp} = Client.echo(Req.new(msg: "test")) 65 | assert match?(%Error{code: :internal}, resp) 66 | end 67 | 68 | test "no headers are returned", %{service: service} do 69 | Bypass.expect(service, fn conn -> 70 | conn 71 | |> Plug.Conn.resp(200, ~s|foo|) 72 | end) 73 | 74 | assert {:error, resp} = Client.echo(Req.new(msg: "test")) 75 | assert match?(%Error{code: :internal}, resp) 76 | assert resp.msg == ~s|Expected response Content-Type "application/protobuf" but found nil| 77 | end 78 | 79 | test "error is not json", %{service: service} do 80 | Bypass.expect(service, fn conn -> 81 | conn 82 | |> Plug.Conn.send_resp(503, ~s|plain text error|) 83 | end) 84 | 85 | assert {:error, resp} = Client.echo(Req.new(msg: "test")) 86 | assert resp.code == :unavailable 87 | assert resp.msg == "unavailable" 88 | assert resp.meta["http_error_from_intermediary"] == "true" 89 | assert resp.meta["not_a_twirp_error_because"] == "Response is not JSON" 90 | assert resp.meta["body"] == "plain text error" 91 | end 92 | 93 | test "error has no code", %{service: service} do 94 | Bypass.expect(service, fn conn -> 95 | conn 96 | |> Plug.Conn.put_resp_content_type("application/json") 97 | |> Plug.Conn.send_resp(500, ~s|{"msg": "I have no code"}|) 98 | end) 99 | 100 | assert {:error, resp} = Client.echo(Req.new(msg: "test")) 101 | assert resp.code == :unknown 102 | assert resp.msg == "unknown" 103 | assert resp.meta["http_error_from_intermediary"] == "true" 104 | 105 | assert resp.meta["not_a_twirp_error_because"] == 106 | "Response is JSON but it has no \"code\" attribute" 107 | end 108 | 109 | test "error has incorrect code", %{service: service} do 110 | Bypass.expect(service, fn conn -> 111 | conn 112 | |> Plug.Conn.put_resp_content_type("application/json") 113 | |> Plug.Conn.send_resp(500, ~s|{"code": "keathley", "msg": "incorrect code"}|) 114 | end) 115 | 116 | assert {:error, resp} = Client.echo(Req.new(msg: "test")) 117 | assert resp.code == :internal 118 | assert resp.msg == "Invalid Twirp error code: keathley" 119 | assert resp.meta["invalid_code"] == "keathley" 120 | end 121 | 122 | test "error has meta", %{service: service} do 123 | Bypass.expect(service, fn conn -> 124 | resp = 125 | ~s|{"code": "internal", "msg": "Internal Server Error", "meta": {"cause": "some exception"}}| 126 | conn 127 | |> Plug.Conn.put_resp_content_type("application/json") 128 | |> Plug.Conn.send_resp(500, resp) 129 | end) 130 | 131 | assert {:error, resp} = Client.echo(Req.new(msg: "test")) 132 | assert resp.code == :internal 133 | assert resp.msg == "Internal Server Error" 134 | assert resp.meta == %{"cause" => "some exception"} 135 | end 136 | 137 | test "redirect errors", %{service: service} do 138 | Bypass.expect(service, fn conn -> 139 | url = "https://keathley.io" 140 | 141 | conn 142 | |> Plug.Conn.put_resp_header("location", url) 143 | |> Plug.Conn.send_resp(302, url) 144 | end) 145 | 146 | assert {:error, resp} = Client.echo(Req.new(msg: "test")) 147 | assert match?(%Error{code: :internal}, resp) 148 | assert resp.meta["http_error_from_intermediary"] == "true" 149 | assert resp.meta["not_a_twirp_error_because"] == "Redirects not allowed on Twirp requests" 150 | end 151 | 152 | test "service is down", %{service: service} do 153 | Bypass.down(service) 154 | 155 | assert {:error, resp} = Client.echo(Req.new(msg: "test")) 156 | assert resp.code == :unavailable 157 | end 158 | 159 | @tag :skip 160 | test "clients are easy to stub" do 161 | Twirp.Client.Stub.new( 162 | echo: fn %{msg: "foo"} -> 163 | Error.unavailable("test") 164 | end 165 | ) 166 | 167 | assert {:error, %Error{code: :unavailable}} = Client.echo(Req.new(msg: "foo")) 168 | 169 | Twirp.Client.Stub.new( 170 | echo: fn %{msg: "foo"} -> 171 | Resp.new(msg: "foo") 172 | end 173 | ) 174 | 175 | assert {:ok, %Resp{msg: "foo"}} = Client.echo(Req.new(msg: "foo")) 176 | 177 | assert_raise Twirp.Client.StubError, ~r/does not define/, fn -> 178 | Twirp.Client.Stub.new() 179 | Client.echo(Req.new(msg: "foo")) 180 | end 181 | 182 | assert_raise Twirp.Client.StubError, ~r/expected to return/, fn -> 183 | Twirp.Client.Stub.new( 184 | echo: fn _ -> 185 | {:ok, Req.new(msg: "test")} 186 | end 187 | ) 188 | 189 | Client.echo(Req.new(msg: "foo")) 190 | end 191 | end 192 | end 193 | -------------------------------------------------------------------------------- /test/twirp/encoder_test.exs: -------------------------------------------------------------------------------- 1 | defmodule Twirp.EncoderTest do 2 | use ExUnit.Case, async: true 3 | 4 | alias Twirp.Encoder 5 | alias Twirp.Test.Req 6 | alias Twirp.Test.Envelope 7 | alias Twirp.Test.BatchReq 8 | 9 | describe "decode/3 with json" do 10 | test "converts json to protobuf" do 11 | assert {:ok, %Req{msg: "test"}} = Encoder.decode(%{msg: "test"}, Req, "application/json") 12 | end 13 | 14 | test "converts nested string fields" do 15 | assert {:ok, %Envelope{msg: "test", sub: %Req{msg: "test"}}} == 16 | Encoder.decode( 17 | %{"msg" => "test", "sub" => %{"msg" => "test"}}, 18 | Envelope, 19 | "application/json" 20 | ) 21 | end 22 | 23 | test "converts nested JSON to nested structs" do 24 | assert {:ok, %Envelope{sub: %Req{msg: "test"}}} = 25 | Encoder.decode(%{sub: %{msg: "test"}}, Envelope, "application/json") 26 | end 27 | 28 | test "converts JSON rith repeated fields to structs" do 29 | assert {:ok, %BatchReq{requests: [%Req{msg: "test1"}, %Req{msg: "test2"}]}} = 30 | Encoder.decode( 31 | %{requests: [%{msg: "test1"}, %{msg: "test2"}]}, 32 | BatchReq, 33 | "application/json" 34 | ) 35 | end 36 | 37 | test "converts string keys to atom keys in nested lists fields" do 38 | response = 39 | Encoder.decode( 40 | %{"requests" => [%{"msg" => "test1"}, %{"msg" => "test2"}]}, 41 | BatchReq, 42 | "application/json" 43 | ) 44 | 45 | assert {:ok, %BatchReq{requests: [%Req{msg: "test1"}, %Req{msg: "test2"}]}} = response 46 | end 47 | end 48 | 49 | describe "encode/3 as json " do 50 | test "encodes to JSON without implementing a JSON protocol" do 51 | assert ~S({"msg":"test","sub":{"msg":"test"}}) == 52 | Encoder.encode( 53 | %Envelope{msg: "test", sub: %Req{msg: "test"}}, 54 | Envelope, 55 | "application/json" 56 | ) 57 | end 58 | 59 | test "encodes repeated structs as JSON without implementing a JSON protocol" do 60 | assert ~S({"requests":[{"msg":"test1"},{"msg":"test2"}]}) == 61 | Encoder.encode( 62 | %BatchReq{requests: [%Req{msg: "test1"}, %Req{msg: "test2"}]}, 63 | BatchReq, 64 | "application/json" 65 | ) 66 | end 67 | end 68 | end 69 | -------------------------------------------------------------------------------- /test/twirp/error_test.exs: -------------------------------------------------------------------------------- 1 | defmodule Twirp.ErrorTest do 2 | use ExUnit.Case, async: true 3 | 4 | alias Twirp.Error 5 | 6 | describe "message/1 callback" do 7 | test "returns the msg value" do 8 | error = Error.new(:invalid_argument, "I can't make a hat that small!") 9 | assert "I can't make a hat that small!" == Exception.message(error) 10 | end 11 | end 12 | 13 | test "can raise exception" do 14 | assert_raise Twirp.Error, ~r|hat that small|, fn -> 15 | raise Error.invalid_argument("I can't make a hat that small!") 16 | end 17 | end 18 | end 19 | -------------------------------------------------------------------------------- /test/twirp/plug_test.exs: -------------------------------------------------------------------------------- 1 | defmodule Twirp.PlugTest do 2 | use ExUnit.Case, async: false 3 | use Plug.Test 4 | 5 | alias Twirp.Error 6 | 7 | defmodule Size do 8 | @moduledoc false 9 | use Protobuf, syntax: :proto3 10 | 11 | defstruct [:inches] 12 | 13 | field :inches, 1, type: :int32 14 | end 15 | 16 | defmodule Hat do 17 | @moduledoc false 18 | use Protobuf, syntax: :proto3 19 | 20 | defstruct [:color] 21 | 22 | field :color, 2, type: :string 23 | end 24 | 25 | defmodule Service do 26 | use Twirp.Service 27 | 28 | package "plug.test" 29 | service "Haberdasher" 30 | 31 | rpc :MakeHat, Size, Hat, :make_hat 32 | end 33 | 34 | defmodule GoodHandler do 35 | def make_hat(_env, %Size{inches: inches}) do 36 | if inches <= 0 do 37 | Error.invalid_argument("I can't make a hat that small!") 38 | else 39 | %Hat{color: "red"} 40 | end 41 | end 42 | end 43 | 44 | defmodule EmptyHandler do 45 | end 46 | 47 | defmodule BadHandler do 48 | def make_hat(_env, size) do 49 | size 50 | end 51 | end 52 | 53 | @opts Twirp.Plug.init([service: Service, handler: GoodHandler]) 54 | 55 | def json_req(method, payload) do 56 | endpoint = "/twirp/plug.test.Haberdasher/#{method}" 57 | 58 | body = if is_map(payload), do: Jason.encode!(payload), else: payload 59 | 60 | :post 61 | |> conn(endpoint, body) 62 | |> put_req_header("content-type", "application/json") 63 | end 64 | 65 | def proto_req(method, payload) do 66 | endpoint = "/twirp/plug.test.Haberdasher/#{method}" 67 | 68 | mod = payload.__struct__ 69 | 70 | :post 71 | |> conn(endpoint, mod.encode(payload)) 72 | |> put_req_header("content-type", "application/protobuf") 73 | end 74 | 75 | def content_type(conn) do 76 | conn.resp_headers 77 | |> Enum.find_value(fn {h, v} -> if h == "content-type", do: v, else: false end) 78 | |> String.split(";") # drop the charset if there is one 79 | |> Enum.at(0) 80 | end 81 | 82 | def call(req, opts \\ @opts) do 83 | Twirp.Plug.call(req, opts) 84 | end 85 | 86 | test "json request" do 87 | req = json_req("MakeHat", %{inches: 10}) 88 | conn = call(req) 89 | 90 | assert conn.status == 200 91 | end 92 | 93 | test "proto request" do 94 | req = proto_req("MakeHat", Size.new(inches: 10)) 95 | conn = call(req) 96 | 97 | assert conn.status == 200 98 | assert Hat.new(color: "red") == Hat.decode(conn.resp_body) 99 | end 100 | 101 | test "non twirp requests" do 102 | req = conn(:post, "/anotherurl") 103 | conn = call(req) 104 | 105 | assert conn == req 106 | end 107 | 108 | test "twirp requests to a different service" do 109 | req = conn(:post, "/twirp/another.service.Service") 110 | conn = call(req) 111 | 112 | assert conn == req 113 | end 114 | 115 | test "not a POST" do 116 | req = conn(:get, "/twirp/plug.test.Haberdasher/MakeHat") 117 | conn = call(req) 118 | 119 | assert conn.status == 404 120 | assert content_type(conn) == "application/json" 121 | body = Jason.decode!(conn.resp_body) 122 | assert body["code"] == "bad_route" 123 | assert body["msg"] == "HTTP request must be POST" 124 | assert body["meta"] == %{ 125 | "twirp_invalid_route" => "GET /twirp/plug.test.Haberdasher/MakeHat" 126 | } 127 | end 128 | 129 | test "request has incorrect content type" do 130 | req = conn(:post, "/twirp/plug.test.Haberdasher/MakeHat") 131 | |> put_req_header("content-type", "application/msgpack") 132 | conn = call(req) 133 | 134 | assert conn.status == 404 135 | assert content_type(conn) == "application/json" 136 | body = Jason.decode!(conn.resp_body) 137 | assert body["code"] == "bad_route" 138 | assert body["msg"] == "Unexpected Content-Type: application/msgpack" 139 | assert body["meta"] == %{ 140 | "twirp_invalid_route" => "POST /twirp/plug.test.Haberdasher/MakeHat" 141 | } 142 | end 143 | 144 | test "request has no content-type" do 145 | req = conn(:post, "/twirp/plug.test.Haberdasher/MakeHat") 146 | conn = call(req) 147 | 148 | assert conn.status == 404 149 | assert content_type(conn) == "application/json" 150 | body = Jason.decode!(conn.resp_body) 151 | assert body["code"] == "bad_route" 152 | assert body["msg"] == "Unexpected Content-Type: nil" 153 | assert body["meta"] == %{ 154 | "twirp_invalid_route" => "POST /twirp/plug.test.Haberdasher/MakeHat" 155 | } 156 | end 157 | 158 | test "handler doesn't define function" do 159 | opts = Twirp.Plug.init([service: Service, handler: EmptyHandler]) 160 | req = proto_req("MakeHat", Size.new(inches: 10)) 161 | 162 | # We need to manually set options like this to skip the 163 | # compile time checking done in init. 164 | conn = call(req, opts) 165 | assert conn.status == 501 166 | assert content_type(conn) == "application/json" 167 | resp = Jason.decode!(conn.resp_body) 168 | assert resp["code"] == "unimplemented" 169 | assert resp["msg"] == "Handler function make_hat is not implemented" 170 | end 171 | 172 | test "unknown method" do 173 | req = proto_req("MakeShoes", Size.new(inches: 10)) 174 | conn = call(req) 175 | 176 | assert conn.status == 404 177 | assert content_type(conn) == "application/json" 178 | resp = Jason.decode!(conn.resp_body) 179 | assert resp["code"] == "bad_route" 180 | assert resp["msg"] == "Invalid rpc method: MakeShoes" 181 | assert resp["meta"] == %{ 182 | "twirp_invalid_route" => "POST /twirp/plug.test.Haberdasher/MakeShoes" 183 | } 184 | end 185 | 186 | test "bad json message" do 187 | req = json_req("MakeHat", "not json") 188 | conn = call(req) 189 | 190 | assert conn.status == 404 191 | assert content_type(conn) == "application/json" 192 | resp = Jason.decode!(conn.resp_body) 193 | assert resp["code"] == "bad_route" 194 | assert resp["msg"] == "Invalid request body for rpc method: MakeHat" 195 | assert resp["meta"] == %{ 196 | "twirp_invalid_route" => "POST /twirp/plug.test.Haberdasher/MakeHat" 197 | } 198 | end 199 | 200 | test "bad proto message" do 201 | req = 202 | :post 203 | |> conn("/twirp/plug.test.Haberdasher/MakeHat", "bad protobuf") 204 | |> put_req_header("content-type", "application/protobuf") 205 | conn = call(req) 206 | 207 | assert conn.status == 404 208 | assert content_type(conn) == "application/json" 209 | resp = Jason.decode!(conn.resp_body) 210 | assert resp["code"] == "bad_route" 211 | assert resp["msg"] == "Invalid request body for rpc method: MakeHat" 212 | assert resp["meta"] == %{ 213 | "twirp_invalid_route" => "POST /twirp/plug.test.Haberdasher/MakeHat" 214 | } 215 | end 216 | 217 | test "handler returns incorrect response" do 218 | opts = Twirp.Plug.init([service: Service, handler: BadHandler]) 219 | req = proto_req("MakeHat", Size.new(inches: 10)) 220 | conn = call(req, opts) 221 | 222 | assert conn.status == 500 223 | assert content_type(conn) == "application/json" 224 | resp = Jason.decode!(conn.resp_body) 225 | assert resp["code"] == "internal" 226 | assert resp["msg"] == "Handler method make_hat expected to return one of Elixir.Twirp.PlugTest.Hat or Twirp.Error but returned %Twirp.PlugTest.Size{inches: 10}" 227 | end 228 | 229 | test "handler doesn't return an error, struct or map" do 230 | defmodule InvalidHandler do 231 | def make_hat(_, _) do 232 | "invalid" 233 | end 234 | end 235 | 236 | opts = Twirp.Plug.init([service: Service, handler: InvalidHandler]) 237 | req = proto_req("MakeHat", Size.new(inches: 10)) 238 | conn = call(req, opts) 239 | 240 | assert conn.status == 500 241 | assert content_type(conn) == "application/json" 242 | resp = Jason.decode!(conn.resp_body) 243 | assert resp["code"] == "internal" 244 | assert resp["msg"] == "Handler method make_hat expected to return one of Elixir.Twirp.PlugTest.Hat or Twirp.Error but returned \"invalid\"" 245 | end 246 | 247 | describe "when the body has been pre-parsed" do 248 | test "json requests use the body params" do 249 | req = json_req("MakeHat", %{}) 250 | req = Map.put(req, :body_params, %{"inches" => 10}) 251 | conn = call(req) 252 | 253 | assert conn.status == 200 254 | assert resp = Jason.decode!(conn.resp_body) 255 | assert resp["color"] != nil 256 | end 257 | 258 | test "returns errors if the payload is incorrect" do 259 | req = json_req("MakeHat", %{}) 260 | req = Map.put(req, :body_params, %{"keathley" => "bar"}) 261 | conn = call(req) 262 | 263 | assert conn.status == 404 264 | assert content_type(conn) == "application/json" 265 | resp = Jason.decode!(conn.resp_body) 266 | assert resp["code"] == "bad_route" 267 | assert resp["msg"] == "Invalid request body for rpc method: MakeHat" 268 | assert resp["meta"] == %{ 269 | "twirp_invalid_route" => "POST /twirp/plug.test.Haberdasher/MakeHat" 270 | } 271 | end 272 | end 273 | 274 | test "handler receives env" do 275 | defmodule HandlerWithEnv do 276 | def make_hat(env, _) do 277 | assert Norm.valid?(env, Norm.selection(Twirp.Plug.env_s())) 278 | end 279 | end 280 | 281 | opts = Twirp.Plug.init([service: Service, handler: RaiseHandler]) 282 | req = proto_req("MakeHat", Size.new(inches: 10)) 283 | call(req, opts) 284 | end 285 | 286 | test "handler raises exception" do 287 | defmodule RaiseHandler do 288 | def make_hat(_env, _size) do 289 | raise ArgumentError, "Blow this ish up" 290 | end 291 | end 292 | 293 | opts = Twirp.Plug.init([service: Service, handler: RaiseHandler]) 294 | req = proto_req("MakeHat", Size.new(inches: 10)) 295 | conn = call(req, opts) 296 | 297 | assert conn.status == 500 298 | assert content_type(conn) == "application/json" 299 | resp = Jason.decode!(conn.resp_body) 300 | assert resp["code"] == "internal" 301 | assert resp["msg"] == "Blow this ish up" 302 | assert resp["meta"] == %{} 303 | end 304 | 305 | describe "before" do 306 | test "hooks are run before the handler is called" do 307 | us = self() 308 | 309 | f = fn conn, env -> 310 | assert %Plug.Conn{} = conn 311 | assert Norm.valid?(env, Norm.selection(Twirp.Plug.env_s())) 312 | assert env.input == Size.new(inches: 10) 313 | send(us, :plug_called) 314 | env 315 | end 316 | 317 | opts = Twirp.Plug.init [ 318 | service: Service, 319 | handler: GoodHandler, 320 | before: [f] 321 | ] 322 | req = proto_req("MakeHat", Size.new(inches: 10)) 323 | _conn = call(req, opts) 324 | assert_receive :plug_called 325 | end 326 | 327 | test "hooks can update the env" do 328 | us = self() 329 | 330 | first = fn _conn, env -> 331 | Map.put(env, :test, :foobar) 332 | end 333 | 334 | second = fn _conn, env -> 335 | assert env.test == :foobar 336 | send(us, :done) 337 | env 338 | end 339 | 340 | opts = Twirp.Plug.init [ 341 | service: Service, 342 | handler: GoodHandler, 343 | before: [first, second] 344 | ] 345 | req = proto_req("MakeHat", Size.new(inches: 10)) 346 | _conn = call(req, opts) 347 | assert_receive :done 348 | end 349 | 350 | test "before hooks are halted if they return an error" do 351 | first = fn _conn, _env -> 352 | Twirp.Error.permission_denied("You're not authorized for this") 353 | end 354 | second = fn _conn, _env -> 355 | flunk "I should never make it here" 356 | end 357 | 358 | opts = Twirp.Plug.init [ 359 | service: Service, 360 | handler: GoodHandler, 361 | before: [first, second] 362 | ] 363 | req = proto_req("MakeHat", Size.new(inches: 10)) 364 | _conn = call(req, opts) 365 | end 366 | 367 | test "returns the most recent env" do 368 | us = self() 369 | 370 | first = fn _conn, env -> 371 | Map.put(env, :test, "This is a test") 372 | end 373 | second = fn _conn, _env -> 374 | Twirp.Error.permission_denied("Bail out") 375 | end 376 | 377 | error = fn env, error -> 378 | assert error == Twirp.Error.permission_denied("Bail out") 379 | assert env.test == "This is a test" 380 | 381 | send us, :done 382 | end 383 | 384 | opts = Twirp.Plug.init [ 385 | service: Service, 386 | handler: GoodHandler, 387 | before: [first, second], 388 | on_error: [error] 389 | ] 390 | req = proto_req("MakeHat", Size.new(inches: 10)) 391 | _conn = call(req, opts) 392 | 393 | assert_receive :done 394 | end 395 | end 396 | 397 | describe "on_success hooks" do 398 | test "are called if the rpc handler was successful" do 399 | us = self() 400 | 401 | first = fn env -> 402 | assert env.output == Hat.encode(Hat.new(color: "red")) 403 | send us, :done 404 | end 405 | 406 | opts = Twirp.Plug.init [ 407 | service: Service, 408 | handler: GoodHandler, 409 | on_success: [first] 410 | ] 411 | req = proto_req("MakeHat", Size.new(inches: 10)) 412 | _conn = call(req, opts) 413 | assert_receive :done 414 | end 415 | end 416 | 417 | describe "on_error hooks" do 418 | test "run if the handler returns an error" do 419 | defmodule ErrorHandler do 420 | def make_hat(_, _) do 421 | Twirp.Error.permission_denied("not allowed") 422 | end 423 | end 424 | 425 | us = self() 426 | 427 | first = fn _env, error -> 428 | assert error == Twirp.Error.permission_denied("not allowed") 429 | send us, :done 430 | end 431 | 432 | opts = Twirp.Plug.init [ 433 | service: Service, 434 | handler: ErrorHandler, 435 | on_error: [first] 436 | ] 437 | req = proto_req("MakeHat", Size.new(inches: 10)) 438 | conn = call(req, opts) 439 | assert_receive :done 440 | 441 | assert conn.status == 403 442 | error = Jason.decode!(conn.resp_body) 443 | assert error["code"] == "permission_denied" 444 | end 445 | 446 | test "are called if there was an exception" do 447 | defmodule ExceptionToErrorHandler do 448 | def make_hat(_, _) do 449 | raise ArgumentError, "Boom!" 450 | end 451 | end 452 | 453 | us = self() 454 | 455 | on_error = fn _env, error -> 456 | assert error == Twirp.Error.internal("Boom!") 457 | send us, :done 458 | end 459 | 460 | opts = Twirp.Plug.init [ 461 | service: Service, 462 | handler: ExceptionToErrorHandler, 463 | on_error: [on_error] 464 | ] 465 | req = proto_req("MakeHat", Size.new(inches: 10)) 466 | conn = call(req, opts) 467 | assert_receive :done 468 | 469 | assert conn.status == 500 470 | error = Jason.decode!(conn.resp_body) 471 | assert error["code"] == "internal" 472 | end 473 | end 474 | 475 | describe "on_exception hooks" do 476 | test "are called if there is an exception raised while processing the call" do 477 | defmodule ExceptionHandler do 478 | def make_hat(_, _) do 479 | raise ArgumentError, "Boom!" 480 | end 481 | end 482 | 483 | us = self() 484 | 485 | first = fn env, exception -> 486 | assert Norm.valid?(env, Twirp.Plug.env_s()) 487 | assert match?(%ArgumentError{}, exception) 488 | send us, :done 489 | end 490 | 491 | opts = Twirp.Plug.init [ 492 | service: Service, 493 | handler: ExceptionHandler, 494 | on_exception: [first] 495 | ] 496 | req = proto_req("MakeHat", Size.new(inches: 10)) 497 | conn = call(req, opts) 498 | assert_receive :done 499 | 500 | assert conn.status == 500 501 | error = Jason.decode!(conn.resp_body) 502 | assert error["code"] == "internal" 503 | end 504 | 505 | test "catches exceptions raised in other before hooks" do 506 | us = self() 507 | bad_hook = fn _, _ -> 508 | raise ArgumentError, "Thrown from hook" 509 | end 510 | bad_success_hook = fn _env -> 511 | raise ArgumentError, "Thrown from success" 512 | end 513 | 514 | exception_hook = fn env, exception -> 515 | assert Norm.valid?(env, Twirp.Plug.env_s()) 516 | assert match?(%ArgumentError{}, exception) 517 | send us, :done 518 | end 519 | 520 | opts = Twirp.Plug.init [ 521 | service: Service, 522 | handler: GoodHandler, 523 | before: [bad_hook], 524 | on_exception: [exception_hook], 525 | ] 526 | req = proto_req("MakeHat", Size.new(inches: 10)) 527 | _conn = call(req, opts) 528 | assert_receive :done 529 | 530 | opts = Twirp.Plug.init [ 531 | service: Service, 532 | handler: GoodHandler, 533 | on_success: [bad_success_hook], 534 | on_exception: [exception_hook], 535 | ] 536 | req = proto_req("MakeHat", Size.new(inches: 10)) 537 | _conn = call(req, opts) 538 | assert_receive :done 539 | end 540 | end 541 | end 542 | -------------------------------------------------------------------------------- /test/twirp/service_test.exs: -------------------------------------------------------------------------------- 1 | defmodule Twirp.ServiceTest do 2 | use ExUnit.Case, async: false 3 | 4 | defmodule Req do 5 | def new(input) do 6 | Enum.into(input, %{}) 7 | end 8 | end 9 | 10 | defmodule Resp do 11 | def new(input) do 12 | Enum.into(input, %{}) 13 | end 14 | end 15 | 16 | defmodule TestService do 17 | use Twirp.Service 18 | 19 | package "test.service" 20 | service "TestService" 21 | rpc :Foo, Req, Resp, :foo 22 | end 23 | 24 | test "DSL adds definition/0 fn to service module" do 25 | assert TestService.definition() == %{ 26 | package: "test.service", 27 | service: "TestService", 28 | rpcs: [ 29 | %{method: :Foo, input: Req, output: Resp, handler_fn: :foo} 30 | ] 31 | } 32 | end 33 | end 34 | -------------------------------------------------------------------------------- /test/twirp_test.exs: -------------------------------------------------------------------------------- 1 | defmodule TwirpTest do 2 | use ExUnit.Case, async: false 3 | 4 | alias Twirp.Test.EchoService, as: Service 5 | alias Twirp.Test.EchoClient, as: Client 6 | alias Twirp.Test.Req 7 | alias Twirp.Test.Resp 8 | 9 | defmodule Handler do 10 | def echo(_conn, %Req{msg: msg}) do 11 | %Resp{msg: msg} 12 | end 13 | 14 | def slow_echo(_conn, %Req{msg: msg}) do 15 | :timer.sleep(50) 16 | %Resp{msg: msg} 17 | end 18 | end 19 | 20 | defmodule TestRouter do 21 | use Plug.Router 22 | 23 | plug Plug.Parsers, parsers: [:urlencoded, :json], 24 | pass: ["*/*"], 25 | json_decoder: Jason 26 | 27 | plug Twirp.Plug, service: Service, handler: Handler 28 | 29 | plug :match 30 | 31 | match _ do 32 | send_resp(conn, 404, "oops") 33 | end 34 | end 35 | 36 | setup_all do 37 | {:ok, _} = Plug.Cowboy.http TestRouter, [], [port: 4002] 38 | 39 | :ok 40 | end 41 | 42 | test "clients can call services" do 43 | {:ok, _} = start_supervised({Client, url: "http://localhost:4002"}) 44 | req = Req.new(msg: "Hello there") 45 | 46 | assert {:ok, %Resp{}=resp} = Client.echo(req) 47 | assert resp.msg == "Hello there" 48 | end 49 | 50 | test "can call services with json" do 51 | {:ok, _} = start_supervised({Client, url: "http://localhost:4002", content_type: :json}) 52 | req = Req.new(msg: "Hello there") 53 | 54 | assert {:ok, %Resp{}=resp} = Client.echo(req) 55 | assert resp.msg == "Hello there" 56 | end 57 | 58 | test "users can specify deadlines" do 59 | {:ok, _} = start_supervised({Client, url: "http://localhost:4002"}) 60 | req = Req.new(msg: "Hello there") 61 | 62 | assert {:error, resp} = Client.slow_echo(%{deadline: 5}, req) 63 | assert resp.code == :deadline_exceeded 64 | assert resp.meta["error_type"] == "timeout" 65 | end 66 | 67 | test "clients allow interceptors" do 68 | ref = make_ref() 69 | us = self() 70 | 71 | interceptors = [ 72 | fn ctx, req, next -> 73 | deadline = ctx.deadline 74 | ctx = %{ctx | headers: [{"x-twirp-deadline", to_string(deadline)} | ctx.headers]} 75 | next.(ctx, req) 76 | end, 77 | fn ctx, req, next -> 78 | send(us, {ref, ctx, req}) 79 | next.(ctx, req) 80 | end 81 | ] 82 | 83 | start_supervised({Client, url: "http://localhost:4002", interceptors: interceptors}) 84 | 85 | assert {:ok, req} = Client.echo(%{deadline: 1000}, Req.new(msg: "Test")) 86 | assert req.msg == "Test" 87 | 88 | assert_receive {^ref, ctx, _req} 89 | assert {"x-twirp-deadline", "1000"} in ctx.headers 90 | end 91 | 92 | test "errors halt the call chain" do 93 | ref = make_ref() 94 | us = self() 95 | 96 | interceptors = [ 97 | fn _ctx, _req, _next -> 98 | {:error, Twirp.Error.resource_exhausted("Client is over concurrency limit")} 99 | end, 100 | fn ctx, req, next -> 101 | send(us, {ref, ctx, req}) 102 | next.(ctx, req) 103 | end 104 | ] 105 | 106 | start_supervised({Client, url: "http://localhost:4002", interceptors: interceptors}) 107 | 108 | assert {:error, %Twirp.Error{}} = Client.echo(%{deadline: 1000}, Req.new(msg: "Test")) 109 | 110 | refute_receive {^ref, _ctx, _req} 111 | end 112 | end 113 | --------------------------------------------------------------------------------