GeoserverConfig.Connection (GeoserverConfig v0.4.1)

Copy Markdown View Source

Holds authentication and connection configuration for a GeoServer instance.

A Connection struct contains the base URL and credentials needed to communicate with GeoServer's REST API. Pass it as the first argument to every API function in this library. This makes the connection explicit and testable, allows multiple GeoServer instances in the same application, and avoids the pitfall of reading environment variables at compile time.

Building a connection

From explicit values — suitable for runtime-configured applications:

conn = GeoserverConfig.Connection.new(
  "http://localhost:8080/geoserver/rest",
  "admin",
  "geoserver"
)

From system environment variables (read at runtime, never at compile time):

# Reads GEOSERVER_BASE_URL, GEOSERVER_USERNAME, GEOSERVER_PASSWORD
conn = GeoserverConfig.Connection.from_env()

# Custom prefix, e.g. STAGING_GEOSERVER_BASE_URL
conn = GeoserverConfig.Connection.from_env(prefix: "STAGING_GEOSERVER")

From your application's config — mirrors the pattern used in config/runtime.exs:

# config/runtime.exs
config :my_app, :geoserver,
  base_url: System.get_env("GEOSERVER_BASE_URL"),
  username: System.get_env("GEOSERVER_USERNAME"),
  password: System.get_env("GEOSERVER_PASSWORD")

# application code
conn = GeoserverConfig.Connection.from_application_env(:my_app)
conn = GeoserverConfig.Connection.from_application_env(:my_app, :geo_api)

Usage

conn = GeoserverConfig.Connection.from_env()
{:ok, workspaces} = GeoserverConfig.Workspaces.fetch_workspaces(conn)

Summary

Functions

Creates a Connection from your application's runtime config.

Creates a Connection by reading system environment variables at runtime.

Creates a Connection from explicit values.

Returns Req options for this connection.

Functions

from_application_env(app, key \\ :geoserver)

Creates a Connection from your application's runtime config.

Reads Application.fetch_env!(app, key) and expects a keyword list with :base_url, :username, and :password. Raises ArgumentError if any key is missing or nil.

Parameters

  • app — your OTP application atom.
  • key — the config key under which GeoServer config is stored (default: :geoserver).

Example

# config/runtime.exs
config :my_app, :geoserver,
  base_url: System.get_env("GEOSERVER_BASE_URL"),
  username: System.get_env("GEOSERVER_USERNAME"),
  password: System.get_env("GEOSERVER_PASSWORD")

# application code
conn = GeoserverConfig.Connection.from_application_env(:my_app)

from_env(opts \\ [])

Creates a Connection by reading system environment variables at runtime.

Raises System.EnvError if any required variable is missing.

Options

  • :prefix — env var prefix (default: "GEOSERVER").

Variables read (with default prefix)

  • GEOSERVER_BASE_URL
  • GEOSERVER_USERNAME
  • GEOSERVER_PASSWORD

Examples

conn = GeoserverConfig.Connection.from_env()
conn = GeoserverConfig.Connection.from_env(prefix: "STAGING_GEOSERVER")

new(base_url, username, password)

Creates a Connection from explicit values.

Example

iex> GeoserverConfig.Connection.new("http://localhost:8080/geoserver/rest", "admin", "geoserver")
%GeoserverConfig.Connection{base_url: "http://localhost:8080/geoserver/rest", username: "admin", password: "geoserver"}

req_opts(conn)

Returns Req options for this connection.

Always includes auth, connect_options, receive_timeout, and retry: false. Also includes plug when set (used by tests via Req.Test).

Modules in this library call Connection.req_opts(conn) and merge their own per-request options on top, so every HTTP call automatically picks up the timeouts and any test adapter set on the connection.

Timeouts

  • Connect timeout: 5000 ms
  • Receive timeout: 10000 ms

Example (in tests)

conn = %GeoserverConfig.Connection{
  base_url: "http://test",
  username: "admin",
  password: "geoserver",
  plug: {Req.Test, MyStub}
}