Skip to content

Commit

Permalink
Merge pull request #228 from karrioapi/patch-2022.6
Browse files Browse the repository at this point in the history
[patch]2022.6
  • Loading branch information
danh91 authored Jul 20, 2022
2 parents ecf7e19 + d781a80 commit f9e0860
Show file tree
Hide file tree
Showing 23 changed files with 112 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 3.2.14 on 2022-07-19 22:23

from django.db import migrations, models
import functools
import karrio.server.core.fields
import karrio.server.core.models


class Migration(migrations.Migration):

dependencies = [
('tenants', '0002_client_feature_flags'),
]

operations = [
migrations.AlterField(
model_name='client',
name='feature_flags',
field=karrio.server.core.fields.MultiChoiceField(base_field=models.CharField(choices=[('AUDIT_LOGGING', 'AUDIT_LOGGING'), ('ALLOW_SIGNUP', 'ALLOW_SIGNUP'), ('ALLOW_ADMIN_APPROVED_SIGNUP', 'ALLOW_ADMIN_APPROVED_SIGNUP'), ('ALLOW_MULTI_ACCOUNT', 'ALLOW_MULTI_ACCOUNT'), ('ORDERS_MANAGEMENT', 'ORDERS_MANAGEMENT'), ('APPS_MANAGEMENT', 'APPS_MANAGEMENT'), ('DOCUMENTS_MANAGEMENT', 'DOCUMENTS_MANAGEMENT'), ('DATA_IMPORT_EXPORT', 'DATA_IMPORT_EXPORT'), ('CUSTOM_CARRIER_DEFINITION', 'CUSTOM_CARRIER_DEFINITION'), ('PERSIST_SDK_TRACING', 'PERSIST_SDK_TRACING')], max_length=100), default=functools.partial(karrio.server.core.models._identity, *(), **{'value': ['AUDIT_LOGGING', 'ALLOW_SIGNUP', 'ALLOW_ADMIN_APPROVED_SIGNUP', 'ALLOW_MULTI_ACCOUNT', 'ORDERS_MANAGEMENT', 'APPS_MANAGEMENT', 'DOCUMENTS_MANAGEMENT', 'DATA_IMPORT_EXPORT', 'CUSTOM_CARRIER_DEFINITION', 'PERSIST_SDK_TRACING']}), help_text='The list of feature flags.', size=None),
),
]
2 changes: 1 addition & 1 deletion insiders/server/tenants/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name="karrio.server.tenants",
version="2022.6",
version="2022.6.1",
description="Multi-carrier shipping API muti-tenant module",
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down
2 changes: 1 addition & 1 deletion server/main/karrio/server/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2022.6
2022.6.1
1 change: 1 addition & 0 deletions server/main/karrio/server/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@
AUDIT_LOGGING = importlib.util.find_spec( # type:ignore
"karrio.server.audit"
) is not None and config("AUDIT_LOGGING", default=True, cast=bool)
PERSIST_SDK_TRACING = config("PERSIST_SDK_TRACING", default=True, cast=bool)


# components path settings
Expand Down
18 changes: 10 additions & 8 deletions server/main/karrio/server/static/karrio/js/karrio.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion server/main/karrio/server/static/karrio/js/karrio.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions server/modules/core/karrio/server/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"DOCUMENTS_MANAGEMENT",
"DATA_IMPORT_EXPORT",
"CUSTOM_CARRIER_DEFINITION",
"PERSIST_SDK_TRACING",
]
DEFAULT_ALLOWED_CONFIG = [
"APP_NAME",
Expand Down
5 changes: 2 additions & 3 deletions server/modules/core/karrio/server/core/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,11 @@ def catch_auth_exception(func):
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except exceptions.AuthenticationFailed as e:
except exceptions.AuthenticationFailed:
from karrio.server.core.exceptions import APIException

message, *_ = list(e.args)
raise APIException(
message or "The given token is invalid or expired",
"Given token not valid for any token type",
code="invalid_token",
status_code=status.HTTP_401_UNAUTHORIZED,
)
Expand Down
1 change: 1 addition & 0 deletions server/modules/core/karrio/server/core/dataunits.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def contextual_metadata(request: Request):
"DATA_IMPORT_EXPORT": settings.DATA_IMPORT_EXPORT,
"ALLOW_SIGNUP": settings.ALLOW_SIGNUP,
"ALLOW_ADMIN_APPROVED_SIGNUP": settings.ALLOW_ADMIN_APPROVED_SIGNUP,
"PERSIST_SDK_TRACING": settings.PERSIST_SDK_TRACING,
}


Expand Down
2 changes: 1 addition & 1 deletion server/modules/core/karrio/server/core/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class APIExceptions(APIException):


def custom_exception_handler(exc, context):
logger.exception(exc)
logger.exception(exc, exc_info=False)

response = exception_handler(exc, context)
code = getattr(exc, "code", getattr(exc, "default_code", None))
Expand Down
7 changes: 4 additions & 3 deletions server/modules/core/karrio/server/core/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.db.models import Q
from django.http import HttpResponse
from karrio.core.utils import Tracer
from karrio.server.conf import settings


class CreatorAccess:
Expand Down Expand Up @@ -41,17 +42,17 @@ def __call__(self, request):
# Code to be executed for each request/response after
# the view is called.
try:
self._save_tracing_records(request)
self._save_tracing_records(request, schema=settings.schema)
del self._threadmap[threading.get_ident()]
except KeyError:
pass

return response

def _save_tracing_records(self, request):
def _save_tracing_records(self, request, schema: str = None):
from karrio.server.tracing.utils import save_tracing_records

save_tracing_records(request)
save_tracing_records(request, schema=schema)

@classmethod
def get_current_request(cls):
Expand Down
2 changes: 0 additions & 2 deletions server/modules/core/karrio/server/core/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ class APIAccessPermissions(permissions.BasePermission):
"""

def has_permission(self, request, view):
print(request.path)

if ("/v1/data" in request.path) and (settings.DATA_IMPORT_EXPORT is False):
raise exceptions.PermissionDenied()

Expand Down
1 change: 1 addition & 0 deletions server/modules/core/karrio/server/core/views/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Metadata(Serializer):
AUDIT_LOGGING = BooleanField()
ALLOW_SIGNUP = BooleanField()
ALLOW_ADMIN_APPROVED_SIGNUP = BooleanField()
PERSIST_SDK_TRACING = BooleanField()
ADMIN = CharField()
OPENAPI = CharField()
GRAPHQL = CharField()
Expand Down
1 change: 1 addition & 0 deletions server/modules/core/karrio/server/core/views/references.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class References(Serializer):
AUDIT_LOGGING = BooleanField()
ALLOW_SIGNUP = BooleanField()
ALLOW_ADMIN_APPROVED_SIGNUP = BooleanField()
PERSIST_SDK_TRACING = BooleanField()
ADMIN = CharField()
OPENAPI = CharField()
GRAPHQL = CharField()
Expand Down
9 changes: 8 additions & 1 deletion server/modules/core/karrio/server/tracing/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@


class TracingRecordAdmin(admin.ModelAdmin):
readonly_fields = [f.name for f in models.TracingRecord._meta.get_fields()]
readonly_fields = [
f.name
for f in models.TracingRecord._meta.get_fields()
if f.name not in ["org", "link"]
]

def get_queryset(self, request):
query = super().get_queryset(request)
return query.filter(id__in=models.TracingRecord.access_by(request.user))

def has_add_permission(self, request) -> bool:
return False


admin.site.register(models.TracingRecord, TracingRecordAdmin)
15 changes: 10 additions & 5 deletions server/modules/core/karrio/server/tracing/utils.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import logging
import concurrent.futures as futures
from django.conf import settings
from karrio.core.settings import Settings

from karrio.core.settings import Settings
from karrio.core.utils import DP, Tracer
from karrio.server.conf import settings
from karrio.server.core import utils
from karrio.server.tracing import models

logger = logging.getLogger(__name__)


def save_tracing_records(context, tracer: Tracer = None):
def save_tracing_records(context, tracer: Tracer = None, schema: str = None):
if settings.PERSIST_SDK_TRACING is False:
return

tracer = tracer or getattr(context, "tracer", Tracer())

# Process Karrio SDK tracing records to persist records of interest.
def persist_records():
@utils.tenant_wrapper
def persist_records(**kwarg):
if len(tracer.records) == 0:
return

Expand Down Expand Up @@ -66,7 +71,7 @@ def persist_records():
except Exception as e:
logger.error(e, exc_info=False)

futures.ThreadPoolExecutor(max_workers=1).submit(persist_records)
futures.ThreadPoolExecutor(max_workers=1).submit(persist_records, schema=schema)


def set_tracing_context(**kwargs):
Expand Down
2 changes: 1 addition & 1 deletion server/modules/core/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name="karrio.server.core",
version="2022.6",
version="2022.6.1",
description="Multi-carrier shipping API Core module",
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import functools
import time
import logging
import datetime
Expand Down Expand Up @@ -73,7 +74,9 @@ def create_request_batches(trackers: List[models.Tracking]) -> List[RequestBatch
# Collect the 5 trackers between the start and end indexes
batch_trackers = trackers[start:end]
tracking_numbers = [t.tracking_number for t in batch_trackers]
options: Dict = {t.tracking_number: t.options for t in batch_trackers}
options: dict = functools.reduce(
lambda acc, t: {**acc, **(t.options or {})}, batch_trackers, {}
)

logger.debug(f"prepare tracking request for {tracking_numbers}")

Expand Down Expand Up @@ -126,16 +129,29 @@ def save_updated_trackers(
for tracker in related_trackers:
# update values only if changed; This is important for webhooks notification
changes = []
meta = details.meta or {}
status = utils.compute_tracking_status(details).value
events = process_events(
response_events=details.events,
current_events=tracker.events,
)
options = {
**(tracker.options or {}),
tracker.tracking_number: details.meta,
}

if events != tracker.events:
tracker.events = events
changes.append("events")

if options != tracker.options:
tracker.options = options
changes.append("options")

if details.meta != tracker.meta:
tracker.meta = meta
changes.append("meta")

if details.delivered != tracker.delivered:
tracker.delivered = details.delivered
changes.append("delivered")
Expand Down
2 changes: 1 addition & 1 deletion server/modules/events/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name="karrio.server.events",
version="2022.6",
version="2022.6.1",
description="Multi-carrier shipping API Events module",
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,10 @@ def create_shipment_tracker(shipment: Optional[models.Shipment], context):
tracking_carrier=carrier,
created_by=shipment.created_by,
shipment=shipment,
meta=dict(carrier=rate_provider),
options={
shipment.tracking_number: dict(carrier=rate_provider),
},
)
tracker.save()
link_org(tracker, context)
Expand Down
2 changes: 1 addition & 1 deletion server/modules/manager/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name="karrio.server.manager",
version="2022.6",
version="2022.6.1",
description="Multi-carrier shipping API Shipments manager module",
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down
14 changes: 12 additions & 2 deletions server/schemas/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"openapi": "3.0.1",
"info": {
"title": "Karrio API",
"description": "\n## API Reference\n\nKarrio is an open source multi-carrier shipping API that simplifies the integration of logistic carrier services.\n\nThe Karrio API is organized around REST. Our API has predictable resource-oriented URLs, accepts JSON-encoded\nrequest bodies, returns JSON-encoded responses, and uses standard HTTP response codes, authentication, and verbs.\n\nThe Karrio API differs for every account as we release new versions.\nThese docs are customized to your version of the API.\n\n\n## Versioning\n\nWhen backwards-incompatible changes are made to the API, a new, dated version is released.\nThe current version is `2022.6`.\n\nRead our API changelog and to learn more about backwards compatibility.\n\nAs a precaution, use API versioning to check a new API version before committing to an upgrade.\n\n\n## Environments\n\nThe Karrio API offer the possibility to create and retrieve certain objects in `test_mode`.\nIn development, it is therefore possible to add carrier connections, get live rates,\nbuy labels, create trackers and schedule pickups in `test_mode`.\n\n\n## Pagination\n\nAll top-level API resources have support for bulk fetches via \"list\" API methods. For instance, you can list addresses,\nlist shipments, and list trackers. These list API methods share a common structure, taking at least these\ntwo parameters: limit, and offset.\n\nKarrio utilizes offset-based pagination via the offset and limit parameters.\nBoth parameters take a number as value (see below) and return objects in reverse chronological order.\nThe offset parameter returns objects listed after an index.\nThe limit parameter take a limit on the number of objects to be returned from 1 to 100.\n\n\n```json\n{\n \"count\": 100,\n \"next\": \"/v1/shipments?limit=25&offset=50\",\n \"previous\": \"/v1/shipments?limit=25&offset=25\",\n \"results\": [\n { ... },\n ]\n}\n```\n\n## Metadata\n\nUpdateable Karrio objects\u2014including Shipment and Order\u2014have a metadata parameter.\nYou can use this parameter to attach key-value data to these Karrio objects.\n\nMetadata is useful for storing additional, structured information on an object.\nAs an example, you could store your user's full name and corresponding unique identifier\nfrom your system on a Karrio Order object.\n\nDo not store any sensitive information as metadata.\n\n",
"description": "\n## API Reference\n\nKarrio is an open source multi-carrier shipping API that simplifies the integration of logistic carrier services.\n\nThe Karrio API is organized around REST. Our API has predictable resource-oriented URLs, accepts JSON-encoded\nrequest bodies, returns JSON-encoded responses, and uses standard HTTP response codes, authentication, and verbs.\n\nThe Karrio API differs for every account as we release new versions.\nThese docs are customized to your version of the API.\n\n\n## Versioning\n\nWhen backwards-incompatible changes are made to the API, a new, dated version is released.\nThe current version is `2022.6.1`.\n\nRead our API changelog and to learn more about backwards compatibility.\n\nAs a precaution, use API versioning to check a new API version before committing to an upgrade.\n\n\n## Environments\n\nThe Karrio API offer the possibility to create and retrieve certain objects in `test_mode`.\nIn development, it is therefore possible to add carrier connections, get live rates,\nbuy labels, create trackers and schedule pickups in `test_mode`.\n\n\n## Pagination\n\nAll top-level API resources have support for bulk fetches via \"list\" API methods. For instance, you can list addresses,\nlist shipments, and list trackers. These list API methods share a common structure, taking at least these\ntwo parameters: limit, and offset.\n\nKarrio utilizes offset-based pagination via the offset and limit parameters.\nBoth parameters take a number as value (see below) and return objects in reverse chronological order.\nThe offset parameter returns objects listed after an index.\nThe limit parameter take a limit on the number of objects to be returned from 1 to 100.\n\n\n```json\n{\n \"count\": 100,\n \"next\": \"/v1/shipments?limit=25&offset=50\",\n \"previous\": \"/v1/shipments?limit=25&offset=25\",\n \"results\": [\n { ... },\n ]\n}\n```\n\n## Metadata\n\nUpdateable Karrio objects\u2014including Shipment and Order\u2014have a metadata parameter.\nYou can use this parameter to attach key-value data to these Karrio objects.\n\nMetadata is useful for storing additional, structured information on an object.\nAs an example, you could store your user's full name and corresponding unique identifier\nfrom your system on a Karrio Order object.\n\nDo not store any sensitive information as metadata.\n\n",
"contact": {
"email": ""
},
"version": "2022.6"
"version": "2022.6.1"
},
"servers": [
{
Expand Down Expand Up @@ -4299,6 +4299,7 @@
"MULTI_ORGANIZATIONS",
"OPENAPI",
"ORDERS_MANAGEMENT",
"PERSIST_SDK_TRACING",
"VERSION"
],
"type": "object",
Expand Down Expand Up @@ -4355,6 +4356,10 @@
"title": "Allow admin approved signup",
"type": "boolean"
},
"PERSIST_SDK_TRACING": {
"title": "Persist sdk tracing",
"type": "boolean"
},
"ADMIN": {
"title": "Admin",
"minLength": 1,
Expand Down Expand Up @@ -8942,6 +8947,7 @@
"MULTI_ORGANIZATIONS",
"OPENAPI",
"ORDERS_MANAGEMENT",
"PERSIST_SDK_TRACING",
"VERSION",
"carrier_capabilities",
"carriers",
Expand Down Expand Up @@ -9012,6 +9018,10 @@
"title": "Allow admin approved signup",
"type": "boolean"
},
"PERSIST_SDK_TRACING": {
"title": "Persist sdk tracing",
"type": "boolean"
},
"ADMIN": {
"title": "Admin",
"minLength": 1,
Expand Down
Loading

0 comments on commit f9e0860

Please sign in to comment.