Skip to content

Commit

Permalink
replaces mojito(deprecated) with finch default http client (#6)
Browse files Browse the repository at this point in the history
* replaces mojito(deprecated) with finch default http client

* update readme and changelog

* circleci not used anymore

Co-authored-by: Dan McGuire <[email protected]>
Co-authored-by: James Dalton <[email protected]>
  • Loading branch information
3 people authored May 23, 2022
1 parent 5f8596e commit 400d134
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 122 deletions.
49 changes: 0 additions & 49 deletions .circleci/config.yml

This file was deleted.

34 changes: 23 additions & 11 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,36 +1,48 @@
### Changelog:
# Changelog

#### v0.2.2
## v0.2.4 (2022-05-23)

- Switch from mojito to finch since mojito is deprecated
- bump lib versions
- jason
- ex_doc
- stream_data
- yaml_elixir
- telemetry
- mox

## v0.2.2

- hex release

#### v0.2.1
## v0.2.1

- allow dry run

#### v0.2.0
## v0.2.0

- Support for CONSUL_PREFIX
- Breaking changes
- Changes how tranforms are done

#### v0.1.4
## v0.1.4

- Namespace modules by `ConsulConfigProvider` (https://github.com/blueshift-labs/consul_config_provider/pull/1)
- Do not force compiling the request function (https://github.com/blueshift-labs/consul_config_provider/pull/2)
- Namespace modules by `ConsulConfigProvider` [#1](https://github.com/blueshift-labs/consul_config_provider/pull/1)
- Do not force compiling the request function [#2](https://github.com/blueshift-labs/consul_config_provider/pull/2)
- README.md updates

#### v0.1.3
## v0.1.3

- Adding in ConsulConfigProvider.Transformer behaviour

#### v0.1.2
## v0.1.2

- Updating README.md and recursive string_atoms

#### v0.1.1
## v0.1.1

- Upgrading mojito version to 0.6.0 and refactoring

#### v0.1.0
## v0.1.0

- Initial implementation
32 changes: 19 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@ The package can be installed by adding `consul_config_provider` to your list of
def deps do
[
{:consul_config_provider, "~> 0.2"},
{:mojito, "~> 0.7"}, # default implmentation for http client
{:finch, "~> 0.12"}, # default implementation for http client
]
end
```
* Only add in mojito if you want to use the default http behaviour and not define your own client

- Only add in finch if you want to use the default http behaviour and not define your own client

The docs can be found at [https://hexdocs.pm/consul_config_provider](https://hexdocs.pm/consul_config_provider).

### Sample Usage
## Sample Usage

- In `mix.exs`

* In mix.exs
```elixir
releases: [
release_name: [
Expand All @@ -40,8 +42,9 @@ releases: [
```

### Transformer
- You can also implement an optional transformer behaviour to change the form of your configs.
- This is helpful for interopt with erlang modules that might have different opinions about things

- You can also implement an optional transformer behaviour to change the form of your configs
- This is helpful for interop with erlang modules that might have different opinions about things

```elixir
defmodule Example.Config do
Expand Down Expand Up @@ -71,20 +74,23 @@ end
```

Then in your configs:

```elixir
config :consul_config_provider, transformer_module: Example.Config
```

### Information
* This provider assumes the config name has a file extension which is either `.json`, `.yml`, or `.yaml` no other extensions are supported although PRs would be welcomed. If you do not follow this naming convention the provider will not work and throw.
* In the above the `prefix` is used for the keys path and can also be set with `CONSUL_PREFIX` (provide an empty string to the prefix if you wish to just use the env var)
* The `CONSUL_HOST` env var is used for the host to talk to consul and defaults to localhost
* The `CONSUL_PORT` env var is used for the port to talk to consul and defaults to 8500
* The http_module is dynamic and you can specify your own if you choose to do so. Just implement the HTTP behaviour and make your implementation return an `{:ok, json_body_binary_string}` as per the mojito example which will be the fallback. You might have to deal with coercing the input keyword list for the mojito arguments to support the client you are using, as well. You also need to set `config :consul_config_provider, :http_module, Client.YourClient` pointing to your client in your configs.
* Dependency-related configs are namespaced with their own application name while other configs use the input app_name for their namespace
## Information

- This provider assumes the config name has a file extension which is either `.json`, `.yml`, or `.yaml` no other extensions are supported although PRs would be welcomed. If you do not follow this naming convention the provider will not work and throw.
- In the above the `prefix` is used for the keys path and can also be set with `CONSUL_PREFIX` (provide an empty string to the prefix if you wish to just use the env var)
- The `CONSUL_HOST` env var is used for the host to talk to consul and defaults to localhost
- The `CONSUL_PORT` env var is used for the port to talk to consul and defaults to 8500
- The http_module is dynamic and you can specify your own if you choose to do so. Just implement the HTTP behaviour and make your implementation return an `{:ok, json_body_binary_string}` as per the finch example which will be the fallback. You might have to deal with coercing the input keyword list for the finch arguments to support the client you are using, as well. You also need to set `config :consul_config_provider, :http_module, Client.YourClient` pointing to your client in your configs.
- Dependency-related configs are namespaced with their own application name while other configs use the input app_name for their namespace

## Release

- Increment [mix.exs project version](https://github.com/blueshift-labs/consul_config_provider/blob/master/mix.exs#L7)
- Update CHANGELOG.md
- Create a release in the [github ui](https://github.com/blueshift-labs/consul_config_provider/releases/new)
- Github actions will create a hex release and upload it
2 changes: 1 addition & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# General application configuration
import Config

config :consul_config_provider, :http_module, ConsulConfigProvider.Client.Mojito
config :consul_config_provider, :http_module, ConsulConfigProvider.Client.Finch

# Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above.
Expand Down
28 changes: 28 additions & 0 deletions lib/client/finch.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
defmodule ConsulConfigProvider.Client.Finch do
@moduledoc "Http client using finch"
@behaviour ConsulConfigProvider.Http
@compile {:inline, request: 1}

if match?({:module, _}, Code.ensure_compiled(Finch)) do
@impl true
def request(method: method, url: url) do
{:ok, _} = Application.ensure_all_started(:telemetry)
pid = Process.whereis(ConsulConfigDefaultHTTPClient)

if pid == nil do
Finch.start_link(name: ConsulConfigDefaultHTTPClient)
end

headers = [{"Content-Type", "application/json"}]
pool_opts = %{default: [max_idle_time: 60_000, size: 10, count: 1]}
finch_req = Finch.build(method, url, headers, nil, [pools: pool_opts])
{:ok, %Finch.Response{body: body}} = Finch.request(finch_req, ConsulConfigDefaultHTTPClient)
{:ok, body}
end
else
@impl true
def request(_request_opts) do
{:error, :finch_not_compiled}
end
end
end
22 changes: 0 additions & 22 deletions lib/client/mojito.ex

This file was deleted.

6 changes: 3 additions & 3 deletions lib/consul_config_provider.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ defmodule ConsulConfigProvider do
http_module =
get_in(config, [:consul_config_provider, :http_module])
|> case do
nil -> ConsulConfigProvider.Client.Mojito
nil -> ConsulConfigProvider.Client.Finch
mod -> mod
end

transformer_module = get_in(config, [:consul_config_provider, :transformer_module])

{:ok, body} = http_module.request(method: :get, url: keys_url, opts: [pool: false])
{:ok, body} = http_module.request(method: :get, url: keys_url)

consul_configs =
body
Expand Down Expand Up @@ -59,7 +59,7 @@ defmodule ConsulConfigProvider do

defp get_consul_key(http_module, host, port, prefix, key_name) do
url = "http://#{host}:#{port}/v1/kv/#{prefix}/#{key_name}"
{:ok, body} = http_module.request(method: :get, url: url, opts: [pool: false])
{:ok, body} = http_module.request(method: :get, url: url)

key_val =
body
Expand Down
16 changes: 8 additions & 8 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule ConsulConfigProvider.MixProject do
def project do
[
app: :consul_config_provider,
version: "0.2.3",
version: "0.2.4",
elixir: "~> 1.9",
start_permanent: Mix.env() == :prod,
deps: deps(),
Expand Down Expand Up @@ -34,13 +34,13 @@ defmodule ConsulConfigProvider.MixProject do
# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:telemetry, "~> 0.4"},
{:ex_doc, "~> 0.24", only: :dev, runtime: false},
{:jason, "~> 1.2"},
{:mojito, "~> 0.7", optional: true},
{:mox, "~> 0.5", only: :test},
{:stream_data, "~> 0.1", only: :test},
{:yaml_elixir, "~> 2.6"}
{:telemetry, "~> 1.1"},
{:ex_doc, "~> 0.28", only: :dev, runtime: false},
{:jason, "~> 1.3"},
{:mox, "~> 1.0", only: :test},
{:stream_data, "~> 0.5", only: :test},
{:yaml_elixir, "~> 2.9"},
{:finch, "~> 0.12", optional: true}
]
end

Expand Down
Loading

0 comments on commit 400d134

Please sign in to comment.