Skip to main content

Geofencing middleware and NextUne IP/whoami helpers for Django.

Project description

nextune-geofencing

Django app paired with the Express package @nextuneapps/geofencing: optional middleware that attaches location-related attributes to each request, optional URL routes, and TypedDict shapes for the payload structures your project works with.

Install

pip install nextune-geofencing

Requires Django 4.1+ and Python 3.10+.

Types (IDE and static checkers)

Import TypedDict definitions from the package so views, services, and tests stay aligned with the JSON you handle:

from typing import cast

from nextune_geofencing import (
    ExchangeRatesBlock,
    ExchangeRatesResponse,
    NextuneGeo,
    NextuneGeofencing,
    NextuneLocationPayload,
)


def summarize_location(body: object) -> dict:
    data = cast(NextuneLocationPayload, body)
    geo = cast(NextuneGeo | None, data.get("geo"))
    raw_gf = data.get("geofencing")
    gf = cast(NextuneGeofencing | None, raw_gf if isinstance(raw_gf, dict) else None)
    return {
        "currency": (gf or {}).get("currency") or (geo or {}).get("currency"),
        "country": (gf or {}).get("country_code") or (geo or {}).get("country"),
        "payment_methods": data.get("payment_methods"),
    }


def rates_table(body: object, base_code: str) -> dict[str, float | int] | None:
    payload = cast(ExchangeRatesResponse, body)
    block = cast(ExchangeRatesBlock | None, payload.get(base_code.upper()))
    return block["rates"] if block else None

After middleware — use the helpers for typed reads of request:

from django.http import HttpRequest, JsonResponse

from nextune_geofencing import get_nextune_geofencing, get_nextune_country_code


def checkout(request: HttpRequest):
    gf = get_nextune_geofencing(request)
    return JsonResponse(
        {
            "country": get_nextune_country_code(request),
            "currency": gf.get("currency") if gf else None,
        }
    )

Error bodies from this app’s views can be annotated as GeofencingAPIError when you parse JSON.

Django setup

INSTALLED_APPS = [
    # ...
    "nextune_geofencing",
]

MIDDLEWARE = [
    # ...
    "nextune_geofencing.middleware.GeofencingMiddleware",
]

Routes (optional)

from django.urls import include, path

urlpatterns = [
    # ...
    path("v1/geo/", include("nextune_geofencing.urls")),
]

Under the mount prefix (example above: v1/geo/):

  • GET …/whoami — resolves the requesting client’s IP, then returns location JSON (NextuneLocationPayload: geo, geofencing, payment_methods, etc.). Commonly used for “current visitor” context (country, currency, payments).
  • POST …/ip — JSON body {"ip": "<address>"} → the same location JSON shape (NextuneLocationPayload).
  • GET …/exchange-rates/<currency> — three-letter ISO code (e.g. KES) → exchange-rate JSON (ExchangeRatesResponse).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nextune_geofencing-0.1.8.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nextune_geofencing-0.1.8-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file nextune_geofencing-0.1.8.tar.gz.

File metadata

  • Download URL: nextune_geofencing-0.1.8.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.12

File hashes

Hashes for nextune_geofencing-0.1.8.tar.gz
Algorithm Hash digest
SHA256 353a950fe0fcf91c435965ff9b6f97d2d4b7a8bbf3013a8ee9d6ba24451b369d
MD5 e5cdcb533df73f6e7a2e1f4423b7e279
BLAKE2b-256 2adc6e7a6b2bdd5b39edc1ec62b1a9ae1bf0981bd241c05c61796ea65e6eca3e

See more details on using hashes here.

File details

Details for the file nextune_geofencing-0.1.8-py3-none-any.whl.

File metadata

File hashes

Hashes for nextune_geofencing-0.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 c34893f5b04f8008d3a824f7f3e85cb6fb4e84baa011a0651446bb39b29e5216
MD5 a070b540ad0f812119f2f886803677b2
BLAKE2b-256 6306b6421a434bd3dc2d6fe16c684d3efa61013865743de97c32846d5fe2cba1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page