Skip to content
This repository has been archived by the owner on Jul 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #154 from mozilla-services/add-mobile-endpoint
Browse files Browse the repository at this point in the history
Add support for multiple API endpoints 📱
  • Loading branch information
hackebrot authored Feb 1, 2022
2 parents a16a718 + 33d6cbf commit fcb19b5
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 10 deletions.
7 changes: 6 additions & 1 deletion test-engineering/contract-tests/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ services:
environment:
PORT: 5000
RESPONSES_DIR: /tmp/partner/
ACCEPTED_MOBILE_FORM_FACTORS: 'phone,tablet'
ACCEPTED_DESKTOP_FORM_FACTORS: 'desktop'
volumes:
- ./volumes/partner:/tmp/partner
contile:
Expand All @@ -17,7 +19,8 @@ services:
links:
- partner
environment:
CONTILE_ADM_ENDPOINT_URL: http://partner:5000/tilesp
CONTILE_ADM_ENDPOINT_URL: http://partner:5000/tilesp/desktop
CONTILE_ADM_MOBILE_ENDPOINT_URL: http://partner:5000/tilesp/mobile
CONTILE_ADM_QUERY_TILE_COUNT: 2
CONTILE_ADM_SETTINGS: /tmp/contile/adm_settings.json
# Timeout requests to the ADM server after this many seconds (default: 5)
Expand All @@ -26,6 +29,8 @@ services:
CONTILE_HOST: 0.0.0.0
CONTILE_HUMAN_LOGS: 1
CONTILE_PORT: 8000
CONTILE_ADM_SUB1: "123456789"
CONTILE_ADM_PARTNER_ID: "demofeed"
RUST_LOG: main,contile=INFO
volumes:
- ./volumes/contile:/tmp/contile
Expand Down
44 changes: 42 additions & 2 deletions test-engineering/contract-tests/partner/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import asyncio
import enum
import json
import logging
import os
import pathlib
import sys
from typing import Dict, List

from fastapi import FastAPI, Query, Request, Response, status
from fastapi.encoders import jsonable_encoder
Expand Down Expand Up @@ -87,10 +89,32 @@ async def read_root():
]


@app.get("/tilesp", response_model=Tiles, status_code=200)
class Endpoint(str, enum.Enum):
"""Path parameters with pre-defined values for the supported endpoints."""

mobile: str = "mobile"
desktop: str = "desktop"


# Map from supported API endpoint path to accepted form-factor query parameter
# values. Example environment variables: 'phone,tablet' or 'desktop'.
FORM_FACTORS: Dict[Endpoint, List[str]] = {
Endpoint.mobile: [
form_factor.strip().lower()
for form_factor in os.environ["ACCEPTED_MOBILE_FORM_FACTORS"].split(",")
],
Endpoint.desktop: [
form_factor.strip().lower()
for form_factor in os.environ["ACCEPTED_DESKTOP_FORM_FACTORS"].split(",")
],
}


@app.get("/tilesp/{endpoint}", response_model=Tiles, status_code=200)
async def read_tilesp(
request: Request,
response: Response,
endpoint: Endpoint,
partner: str = Query(..., example="demofeed"),
sub1: str = Query(..., example="123456789"),
sub2: str = Query(
Expand All @@ -116,7 +140,7 @@ async def read_tilesp(
):
"""Endpoint for requests from Contile."""

unknown_query_params = [
unknown_query_params: List[str] = [
param for param in request.query_params if param not in ACCEPTED_QUERY_PARAMS
]

Expand All @@ -125,6 +149,7 @@ async def read_tilesp(
"received unexpected query parameters from Contile: %s",
unknown_query_params,
)

return JSONResponse(
status_code=status.HTTP_400_BAD_REQUEST,
content=jsonable_encoder(
Expand All @@ -135,6 +160,21 @@ async def read_tilesp(
),
)

if form_factor not in FORM_FACTORS[endpoint]:
logger.error("received form-factor '%s' on %s API", form_factor, endpoint.name)

return JSONResponse(
status_code=status.HTTP_400_BAD_REQUEST,
content=jsonable_encoder(
{
"test": {
f"invalid form-factor for {endpoint.name} API": form_factor
},
**BODY_FROM_API_SPEC,
}
),
)

# Load responses from the responses.yml file for the given country_code and
# region_code. If that fails and the fallback behavior fails as well, this
# will raise an Exception resulting in a 500 Internal Server Error
Expand Down
14 changes: 7 additions & 7 deletions test-engineering/contract-tests/partner/app/tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def test_read_tilesp(client):
example values for the required query parameters from the API specification.
"""
response = client.get(
"/tilesp",
"/tilesp/desktop",
params={
"partner": "demofeed",
"sub1": "123456789",
Expand Down Expand Up @@ -75,7 +75,7 @@ def test_read_tilesp_validate_sub2(client, sub2):
See https://github.com/mozilla-services/contile-integration-tests/issues/38
"""
response = client.get(
"/tilesp",
"/tilesp/desktop",
params={
"partner": "demofeed",
"sub1": "123456789",
Expand Down Expand Up @@ -124,7 +124,7 @@ def test_read_tilesp_validate_country_code(client, country_code):
See https://github.com/mozilla-services/contile-integration-tests/issues/39
"""
response = client.get(
"/tilesp",
"/tilesp/desktop",
params={
"partner": "demofeed",
"sub1": "123456789",
Expand Down Expand Up @@ -174,7 +174,7 @@ def test_read_tilesp_accepted_country_region_code(
See https://github.com/mozilla-services/contile-integration-tests/issues/40
"""
response = client.get(
"/tilesp",
"/tilesp/desktop",
params={
"partner": "demofeed",
"sub1": "123456789",
Expand Down Expand Up @@ -215,7 +215,7 @@ def test_read_tilesp_validate_region_code(client, region_code):
See https://github.com/mozilla-services/contile-integration-tests/issues/40
"""
response = client.get(
"/tilesp",
"/tilesp/desktop",
params={
"partner": "demofeed",
"sub1": "123456789",
Expand Down Expand Up @@ -257,7 +257,7 @@ def test_read_tilesp_validate_dma_code(client, dma_code):
See https://github.com/mozilla-services/contile-integration-tests/issues/62
"""
response = client.get(
"/tilesp",
"/tilesp/desktop",
params={
"partner": "demofeed",
"sub1": "123456789",
Expand Down Expand Up @@ -301,7 +301,7 @@ def test_read_tilesp_error_for_unknown_query_params(client):
See https://github.com/mozilla-services/contile-integration-tests/issues/41
"""
response = client.get(
"/tilesp",
"/tilesp/desktop",
params={
"partner": "demofeed",
"sub1": "123456789",
Expand Down
2 changes: 2 additions & 0 deletions test-engineering/contract-tests/partner/tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ isolated_build = False
basepython = python3.8
setenv =
RESPONSES_DIR = {toxinidir}/app/tests/responses
ACCEPTED_MOBILE_FORM_FACTORS = phone,tablet
ACCEPTED_DESKTOP_FORM_FACTORS = desktop
PYTHONPATH = {toxinidir}/app

# The app is not set up as a Python package
Expand Down

0 comments on commit fcb19b5

Please sign in to comment.