From 0b6323963d45dae1fe9766fc1073a3d7e26ad7e7 Mon Sep 17 00:00:00 2001 From: Alex Laird Date: Sat, 11 Jan 2025 14:20:56 -0600 Subject: [PATCH] Added tunnel_name and api_key to Config. --- CHANGELOG.md | 6 +++++- hookee/__init__.py | 4 ++-- hookee/cli.py | 20 ++++++++++++++++++-- hookee/conf.py | 4 +++- hookee/tunnel.py | 6 +++++- 5 files changed, 33 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ba07fe3..d9af7d3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,10 +4,14 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## [Unreleased](https://github.com/alexdlaird/hookee/compare/2.3.7...HEAD) +## [Unreleased](https://github.com/alexdlaird/hookee/compare/2.3.8...HEAD) + +## [2.3.8](https://github.com/alexdlaird/hookee/compare/2.3.7...2.3.8) - 2025-01-11 ### Added +- Added `tunnel_name` parameter to the `Config`, which can be used to tell `ngrok` to use a definition from its config file when starting a tunnel. +- Added `ngrok`'s `api_key` to the `Config`. If not set in `Config`, it will attempt to use the environment variable `NGROK_API_KEY` if it is set. - Documentation improvements. ## [2.3.7](https://github.com/alexdlaird/hookee/compare/2.3.6...2.3.7) - 2024-11-21 diff --git a/hookee/__init__.py b/hookee/__init__.py index 7d9c000..13620a0 100644 --- a/hookee/__init__.py +++ b/hookee/__init__.py @@ -1,5 +1,5 @@ -__copyright__ = "Copyright (c) 2020-2024 Alex Laird" +__copyright__ = "Copyright (c) 2020-2025 Alex Laird" __license__ = "MIT" -__version__ = "2.3.7" +__version__ = "2.3.8" from hookee.hookeemanager import HookeeManager # noqa: F401 diff --git a/hookee/cli.py b/hookee/cli.py index 4e7984d..1f6a0c8 100644 --- a/hookee/cli.py +++ b/hookee/cli.py @@ -1,6 +1,6 @@ #!/usr/bin/env python -__copyright__ = "Copyright (c) 2020-2024 Alex Laird" +__copyright__ = "Copyright (c) 2020-2025 Alex Laird" __license__ = "MIT" import click @@ -25,13 +25,15 @@ @click.option("--response-script", type=click.Path(exists=True), help="[server] A Python script whose `run(request, response)` method will be called by the default " "`/webhook` after all response plugins have run.") +@click.option("--tunnel-name", help="[tunnel] The name to use for the ngrok tunnel.") @click.option("--subdomain", help="[tunnel] The subdomain to use for ngrok endpoints.") @click.option("--region", type=click.Choice(["us", "eu", "ap", "au", "sa", "jp", "in"]), help="The region to use for ngrok endpoints.") -@click.option("--hostname", help="[tunnel] The hostname to use for ngrok endpoints.") +@click.option("--hostname", help="[tunnel] The domain to use for ngrok endpoints.") @click.option("--auth", help="[tunnel] The basic auth to use for ngrok endpoints.") @click.option("--host-header", help="[tunnel] The \"Host\" header value to use for ngrok endpoints.") @click.option("--auth-token", help="[tunnel] A valid ngrok auth token.") +@click.option("--api-key", help="[tunnel] A valid ngrok API key.") @click.option("--plugins-dir", type=click.Path(exists=True), help="The directory to scan for custom hookee plugins.") @click.option("--plugins", multiple=True, help="A list of hookee plugins to use.") @click.option('--version', is_flag=True, default=False, help="Display version information.") @@ -210,5 +212,19 @@ def authtoken(ctx, token): hookee_manager.print_util.print_config_update("The auth token has been set in the config.") +@hookee.command() +@click.pass_context +@click.argument("key") +def apikey(ctx, key): + """ + Save the API key to the config. + """ + hookee_manager = ctx.obj["hookee_manager"] + + hookee_manager.config.set("api_key", key) + + hookee_manager.print_util.print_config_update("The API key has been set in the config.") + + if __name__ == "__main__": hookee(obj={}) diff --git a/hookee/conf.py b/hookee/conf.py index 1a864c3..b59ecc6 100644 --- a/hookee/conf.py +++ b/hookee/conf.py @@ -1,4 +1,4 @@ -__copyright__ = "Copyright (c) 2020-2024 Alex Laird" +__copyright__ = "Copyright (c) 2020-2025 Alex Laird" __license__ = "MIT" import os @@ -13,6 +13,7 @@ "default_route_methods": confuse.String(default="^(GET|HEAD|POST|PUT|DELETE|PATCH|OPTIONS|TRACE|CONNECT)$"), "port": int, "no_tunnel": confuse.OneOf([True, False], default=False), + "tunnel_name": confuse.String(default=None), "subdomain": confuse.String(default=None), "region": confuse.Choice(["us", "eu", "ap", "au", "sa", "jp", "in", "us-cal-1"], default=None), "domain": confuse.String(default=None), @@ -27,6 +28,7 @@ "request_script": confuse.Filename(default=None), "response_script": confuse.Filename(default=None), "auth_token": confuse.String(default=os.environ.get("NGROK_AUTHTOKEN")), + "api_key": confuse.String(default=os.environ.get("NGROK_API_KEY")), "plugins_dir": confuse.Filename(), "plugins": list, "console_width": confuse.Integer(default=80), diff --git a/hookee/tunnel.py b/hookee/tunnel.py index 69831ca..70cb077 100644 --- a/hookee/tunnel.py +++ b/hookee/tunnel.py @@ -1,4 +1,4 @@ -__copyright__ = "Copyright (c) 2020-2024 Alex Laird" +__copyright__ = "Copyright (c) 2020-2025 Alex Laird" __license__ = "MIT" import threading @@ -37,6 +37,7 @@ def __init__(self, hookee_manager): self.port = self.config.get("port") self.pyngrok_config = PyngrokConfig(auth_token=self.config.get("auth_token"), + api_key=self.config.get("api_key"), region=self.config.get("region")) conf.set_default(self.pyngrok_config) @@ -80,10 +81,13 @@ def start(self): def _start_tunnel(self): options = {"schemes": ["https"]} + name = self.config.get("tunnel_name") subdomain = self.config.get("subdomain") domain = self.config.get("domain", self.config.get("hostname")) host_header = self.config.get("host_header") basic_auth = self.config.get("basic_auth", self.config.get("auth")) + if name: + options["name"] = name if subdomain: options["subdomain"] = subdomain if domain: