Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Set base url for segment #4684

Merged
merged 8 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion api/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from rest_framework.authtoken.models import Token
from rest_framework.test import APIClient
from task_processor.task_run_method import TaskRunMethod
from urllib3 import HTTPResponse
from urllib3.connectionpool import HTTPConnectionPool
from xdist import get_xdist_worker_id

Expand Down Expand Up @@ -163,7 +164,7 @@ def urlopen_mock(
url: str,
*args,
**kwargs,
) -> HTTPConnectionPool.ResponseCls:
) -> HTTPResponse:
if self.host in allowed_hosts:
return original_urlopen(self, method, url, *args, **kwargs)

Expand Down
2 changes: 2 additions & 0 deletions api/integrations/segment/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DEFAULT_BASE_URL = "api.segment.io/"
DUBLIN_BASE_URL = "events.eu1.segmentapis.com/"
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from django.apps.registry import Apps
from django.db import migrations
from django.db.backends.base.schema import BaseDatabaseSchemaEditor

from integrations.segment import constants


def set_base_url(apps: Apps, schema_editor: BaseDatabaseSchemaEditor) -> None:
SegmentConfiguration = apps.get_model("segment", "SegmentConfiguration")
SegmentConfiguration.objects.filter(base_url__isnull=True).update(
base_url=constants.DEFAULT_BASE_URL,
)


class Migration(migrations.Migration):

dependencies = [
("segment", "0004_segmentconfiguration_deleted_at"),
]

operations = [
migrations.RunPython(set_base_url, reverse_code=migrations.RunPython.noop),
]
6 changes: 3 additions & 3 deletions api/integrations/segment/segment.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@

class SegmentWrapper(AbstractBaseIdentityIntegrationWrapper):
def __init__(self, config: SegmentConfiguration):
api_key = config.api_key
self.analytics = SegmentClient(write_key=api_key, sync_mode=True)
self.analytics = SegmentClient(
write_key=config.api_key, sync_mode=True, host=config.base_url
)

def _identify_user(self, data: dict) -> None:
self.analytics.identify(**data)
logger.debug("Sent event to Segment.")

def generate_user_data(
self,
Expand Down
15 changes: 14 additions & 1 deletion api/integrations/segment/serializers.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
from rest_framework import serializers

from integrations.common.serializers import (
BaseEnvironmentIntegrationModelSerializer,
)
from integrations.segment import constants
from integrations.segment.models import SegmentConfiguration


class SegmentConfigurationSerializer(BaseEnvironmentIntegrationModelSerializer):

base_url = serializers.ChoiceField(
choices=[
constants.DEFAULT_BASE_URL,
constants.DUBLIN_BASE_URL,
],
required=False,
default="api.segment.io/",
)

class Meta:
model = SegmentConfiguration
fields = ("id", "api_key")
fields = ("id", "api_key", "base_url")
6 changes: 4 additions & 2 deletions api/tests/unit/integrations/segment/test_unit_segment.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
def test_segment_initialized_correctly():
# Given
api_key = "123key"
config = SegmentConfiguration(api_key=api_key)
base_url = "api.segment.io/"
config = SegmentConfiguration(api_key=api_key, base_url=base_url)

# When initialized
# When
segment_wrapper = SegmentWrapper(config)

# Then
assert segment_wrapper.analytics.write_key == api_key
assert segment_wrapper.analytics.host == base_url


@pytest.mark.django_db
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from rest_framework.test import APIClient

from environments.models import Environment
from integrations.segment.constants import DEFAULT_BASE_URL
from integrations.segment.models import SegmentConfiguration


Expand All @@ -13,6 +14,7 @@ def test_should_create_segment_config_when_post(
admin_client: APIClient,
) -> None:
# Given

data = {"api_key": "abc-123"}
url = reverse(
"api-v1:environments:integrations-segment-list",
Expand All @@ -30,7 +32,9 @@ def test_should_create_segment_config_when_post(
assert response.status_code == status.HTTP_201_CREATED
qs = SegmentConfiguration.objects.filter(environment=environment)
assert qs.count() == 1
assert response.data["id"] == qs.first().id
segment_configuration = qs.first()
assert response.data["id"] == segment_configuration.id
assert segment_configuration.base_url == DEFAULT_BASE_URL


def test_should_return_400_when_duplicate_segment_config_is_posted(
Expand Down Expand Up @@ -105,7 +109,7 @@ def test_should_return_segment_config_list_when_requested(
response = admin_client.get(url)

# Then
expected_response = {"api_key": config.api_key, "id": config.id}
expected_response = {"api_key": config.api_key, "id": config.id, "base_url": None}
assert response.status_code == status.HTTP_200_OK
assert response.data == [expected_response]

Expand Down
Loading