Skip to content

Conversation

sanyo4ever
Copy link
Contributor

🎯 Overview

This PR enhances the Mailgun provider for self-hosted Keep instances with:

  1. EU Region Support - Users can now use Mailgun EU accounts
  2. UI Region Selection - Dropdown to choose US/EU region per provider
  3. API Request Format Fix - Corrects route creation bug
  4. Hybrid Configuration - Supports both UI and environment variable configuration

🐛 Problems Solved

Issue 1: EU Region Not Supported

The Mailgun API endpoint was hardcoded to https://api.mailgun.net (US region only). Users with EU Mailgun accounts received 401/400 errors when installing the provider.

Issue 2: Incorrect API Request Format

The setup_webhook() method was passing both files and data parameters to requests.post(), causing "400 Bad Request" errors from Mailgun API.

Issue 3: No UI Control for Region

Users had to modify code or use environment variables. No per-provider region configuration was possible.

✨ Solution

1. UI Dropdown for Region Selection

Added api_region field to MailgunProviderAuthConfig with dropdown:

  • Type: Select field with options ["US", "EU"]
  • Optional: Can be left empty to use environment variable
  • User-friendly: Clear hints and descriptions

2. Hybrid Configuration Priority

region = (
    self.authentication_config.api_region  # 1. Check UI field first
    or MailgunProvider.MAILGUN_REGION      # 2. Fall back to env var
    or "US"                                 # 3. Default to US
).upper()

Benefits:

  • Per-provider region configuration (UI)
  • Global default via MAILGUN_REGION environment variable
  • Backward compatible (existing deployments unaffected)
  • Flexible for multi-region deployments

3. API Endpoint Selection

Dynamically selects the correct API endpoint:

  • US Region: https://api.mailgun.net
  • EU Region: https://api.eu.mailgun.net

4. API Request Format Fix

Before (Broken):

response = requests.post(
    url,
    files=payload,  # ❌ Wrong parameter
    auth=("api", key),
    data=payload,   # ❌ Missing action array
)

After (Fixed):

response = requests.post(
    url,
    auth=("api", key),
    data={
        **payload,
        "action": [  # ✅ Correct format
            f"forward('{keep_api_url}&api_key={api_key}')",
            "stop()",
        ],
    },
)

🧪 Testing

  • ✅ Tested with US region Mailgun account
  • ✅ Tested with EU region Mailgun account
  • ✅ Route creation works correctly in both regions
  • ✅ UI dropdown displays and saves correctly
  • ✅ Environment variable fallback works
  • ✅ Backward compatible (defaults to US)
  • ✅ No linting errors

📊 Changes

Files Modified:

  • keep/providers/mailgun_provider/mailgun_provider.py
    • Added api_region field with dropdown (lines 49-59)
    • Added MAILGUN_REGION class variable (line 74)
    • Dynamic API endpoint selection (lines 222-229)
    • Fixed request format in setup_webhook() (lines 231-259)

🔄 Backward Compatibility

  • ✅ Existing deployments unaffected (defaults to US region)
  • ✅ Environment variable MAILGUN_REGION still works
  • ✅ No breaking changes to configuration
  • ✅ Works with both old and new Mailgun API key formats

💡 Usage Examples

Example 1: UI Configuration (Per-Provider)

- Added MAILGUN_REGION environment variable (US or EU)
- Dynamically select API endpoint based on region
- Fixes 401 errors for EU Mailgun accounts
- Remove incorrect files parameter
- Use only data parameter for form data
- Fix duplicate payload passing
- Resolves 400 bad request errors
- Added api_region field to MailgunProviderAuthConfig with US/EU dropdown
- Hybrid approach: UI field takes priority, falls back to MAILGUN_REGION env var
- Maintains backward compatibility with environment variable configuration
- Allows per-provider region configuration when needed
Copy link

vercel bot commented Oct 14, 2025

@sanyo4ever is attempting to deploy a commit to the KeepHQ Team on Vercel.

A member of the Team first needs to authorize it.

@CLAassistant
Copy link

CLAassistant commented Oct 14, 2025

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

✅ talboren
❌ sanyo4ever
You have signed the CLA already but the status is still pending? Let us recheck it.

Copy link
Contributor

No linked issues found. Please add the corresponding issues in the pull request description.
Use GitHub automation to close the issue when a PR is merged

@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. Feature A new feature Provider Providers related issues labels Oct 14, 2025
talboren
talboren previously approved these changes Oct 15, 2025
Copy link
Member

@talboren talboren left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Oct 15, 2025
@talboren
Copy link
Member

Thanks @sanyo4ever

Can you execute python3 scripts/docs_render_provider_snippets.py to fix the docs?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Feature A new feature lgtm This PR has been approved by a maintainer Provider Providers related issues size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants