Skip to content

Commit

Permalink
feat(integrations): add support for Amplitude base url (#2534)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewelwell authored Jul 31, 2023
1 parent 35a4f06 commit 5d52564
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 9 deletions.
4 changes: 1 addition & 3 deletions api/integrations/amplitude/amplitude.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@

logger = logging.getLogger(__name__)

AMPLITUDE_API_URL = "https://api.amplitude.com"


class AmplitudeWrapper(AbstractBaseIdentityIntegrationWrapper):
def __init__(self, config: AmplitudeConfiguration):
self.api_key = config.api_key
self.url = f"{AMPLITUDE_API_URL}/identify"
self.url = f"{config.base_url}/identify"

def _identify_user(self, user_data: dict) -> None:
payload = {"api_key": self.api_key, "identification": json.dumps([user_data])}
Expand Down
1 change: 1 addition & 0 deletions api/integrations/amplitude/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DEFAULT_AMPLITUDE_API_URL = "https://api2.amplitude.com"
34 changes: 34 additions & 0 deletions api/integrations/amplitude/migrations/0006_add_default_base_url.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Generated by Django 3.2.20 on 2023-07-27 13:04

from django.db import migrations, models
from django.db.models import Q


def add_default_base_url(apps, schema_editor):
amplitude_configuration_model = apps.get_model(
"amplitude", "AmplitudeConfiguration"
)

# Note that we're updating the existing items to point to
# api.amplitude.com (rather than api2) to maintain existing
# behaviour for these integrations.
amplitude_configuration_model.objects.filter(
Q(base_url__isnull=True) | Q(base_url="")
).update(base_url="https://api.amplitude.com")


class Migration(migrations.Migration):
dependencies = [
("amplitude", "0005_amplitudeconfiguration_deleted_at"),
]

operations = [
migrations.RunPython(
add_default_base_url, reverse_code=migrations.RunPython.noop
),
migrations.AlterField(
model_name="amplitudeconfiguration",
name="base_url",
field=models.URLField(default="https://api2.amplitude.com"),
),
]
2 changes: 2 additions & 0 deletions api/integrations/amplitude/models.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from django.db import models

from environments.models import Environment
from integrations.amplitude.constants import DEFAULT_AMPLITUDE_API_URL
from integrations.common.models import EnvironmentIntegrationModel


class AmplitudeConfiguration(EnvironmentIntegrationModel):
base_url = models.URLField(default=DEFAULT_AMPLITUDE_API_URL)
environment = models.OneToOneField(
Environment, related_name="amplitude_config", on_delete=models.CASCADE
)
2 changes: 1 addition & 1 deletion api/integrations/amplitude/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
class AmplitudeConfigurationSerializer(BaseEnvironmentIntegrationModelSerializer):
class Meta:
model = AmplitudeConfiguration
fields = ("id", "api_key")
fields = ("id", "api_key", "base_url")
20 changes: 15 additions & 5 deletions api/tests/unit/integrations/amplitude/test_unit_amplitude.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
from environments.identities.models import Identity
from environments.models import Environment
from features.models import FeatureState
from integrations.amplitude.amplitude import (
AMPLITUDE_API_URL,
AmplitudeWrapper,
)
from integrations.amplitude.amplitude import AmplitudeWrapper
from integrations.amplitude.constants import DEFAULT_AMPLITUDE_API_URL
from integrations.amplitude.models import AmplitudeConfiguration


Expand All @@ -18,10 +16,22 @@ def test_amplitude_initialized_correctly():
amplitude_wrapper = AmplitudeWrapper(config)

# Then
expected_url = f"{AMPLITUDE_API_URL}/identify"
expected_url = f"{DEFAULT_AMPLITUDE_API_URL}/identify"
assert amplitude_wrapper.url == expected_url


def test_amplitude_initialized_correctly_with_custom_base_url():
# Given
base_url = "https://api.eu.amplitude.com"
config = AmplitudeConfiguration(api_key="123key", base_url=base_url)

# When initialized
amplitude_wrapper = AmplitudeWrapper(config)

# Then
assert amplitude_wrapper.url == f"{base_url}/identify"


@pytest.mark.django_db
def test_amplitude_when_generate_user_data_with_correct_values_then_success(
environment: Environment,
Expand Down

3 comments on commit 5d52564

@vercel
Copy link

@vercel vercel bot commented on 5d52564 Jul 31, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

docs – ./docs

docs-flagsmith.vercel.app
docs.flagsmith.com
docs.bullet-train.io
docs-git-main-flagsmith.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 5d52564 Jul 31, 2023

Choose a reason for hiding this comment

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

@vercel
Copy link

@vercel vercel bot commented on 5d52564 Jul 31, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.