Smarter Request Mixin
- class smarter.lib.django.request.SmarterRequestMixin(*args, request=None, **kwargs)[source]
Bases:
AccountMixinHelper class for the Django request object that enforces authentication and provides lazy loading of the user, account, user profile, and session_key.
This mixin works with any Django request object and any valid URL, but is designed as a helper class for Smarter ChatBot URLs.
Note
The request object is an optional positional argument due to Django view lifecycles, which do not recognize the request object until after class
__init__().SmarterRequestMixinis included as a mixin in the Smarter base view classes.Valid endpoints:
Root endpoints for named URLs (public or authenticated chats) (
self.is_chatbot_named_url == True)http://example.3141-5926-5359.api.localhost:9357/→smarter.apps.chatbot.api.v1.views.default.DefaultChatbotApiViewhttp://example.3141-5926-5359.api.localhost:9357/config→smarter.apps.prompt.views.ChatConfigView
Authenticated sandbox endpoints (authenticated chats) (
self.is_chatbot_sandbox_url == True)http://localhost:9357/workbench/<str:name>/→smarter.apps.prompt.views.ChatAppWorkbenchViewhttp://localhost:9357/workbench/<str:name>/config/→smarter.apps.prompt.views.ChatConfigView
smarter.sh/v1 endpoints (public or authenticated chats) (
self.is_chatbot_smarter_api_url == True)http://localhost:9357/api/v1/workbench/<int:chatbot_id>/chat/→smarter.apps.chatbot.api.v1.views.default.DefaultChatbotApiViewhttp://localhost:9357/api/v1/workbench/<int:chatbot_id>/chat/config/→smarter.apps.prompt.views.ChatConfigView
Command-line interface API endpoints (authenticated chats) (
self.is_chatbot_cli_api_url == True)http://localhost:9357/api/v1/cli/chat/<str:name>/→smarter.apps.chatbot.api.v1.cli.views.nonbrokered.chat.ApiV1CliChatApiViewhttp://localhost:9357/api/v1/cli/chat/config/<str:name>/→smarter.apps.chatbot.api.v1.cli.views.nonbrokered.chat_config.ApiV1CliChatConfigApiView
Other endpoints (possibly deprecated or unused) -
http://localhost:9357/api/v1/chat/
Example URLs:
http://testserverhttp://localhost:9357/http://localhost:9357/docs/http://localhost:9357/dashboard/https://alpha.platform.smarter.sh/api/v1/workbench/1/chatbot/http://example.com/contact/http://localhost:9357/workbench/example/config/?session_key=...https://hr.3141-5926-5359.alpha.api.example.com/https://hr.3141-5926-5359.alpha.api.example.com/config/?session_key=...http://example.3141-5926-5359.api.localhost:9357/http://example.3141-5926-5359.api.localhost:9357/?session_key=...http://example.3141-5926-5359.api.localhost:9357/config/http://example.3141-5926-5359.api.localhost:9357/config/?session_key=...http://localhost:9357/api/v1/workbench/1/chat/http://localhost:9357/api/v1/cli/chat/smarter/?new_session=false&uid=mcdanielhttps://hr.smarter.sh/
- Variables:
session_key – Unique identifier for a chat session, generated by
generate_session_key().
- property api_subdomain: str | None[source]
Extracts the API subdomain from the URL.
- Returns:
The API subdomain or None if not found.
example:
- https://hr.3141-5926-5359.alpha.api.example.com/chatbot/ returns 'hr'
- property api_token: bytes | None[source]
Get the API token from the request.
- Returns:
The API token as bytes if present in the Authorization header, otherwise None.
Example:
request_mixin = SmarterRequestMixin(request) token = request_mixin.api_token
- Returns:
The API token as bytes, or None if not present.
- property auth_header: str | None
Get the Authorization header from the request.
Example:
request_mixin = SmarterRequestMixin(request) print(request_mixin.auth_header)
This property checks for the “Authorization” header in the request headers or in the Django META dictionary.
- Returns:
The value of the “Authorization” header as a string, or None if not present.
- property cache_key: str | None[source]
Returns a cache key for the request.
This is used to cache the chat request thread. The key is a combination of: - the class name, - authenticated username, - the chat name, - and the client UID.
Currently used by the ApiV1CliChatConfigApiView and ApiV1CliChatApiView as a means of sharing the session_key.
- Parameters:
name – A generic object or resource name.
uid – UID of the client, assumed to have been created from the machine MAC address and the hostname of the client.
- Returns:
A unique cache key string.
Example:
request_mixin = SmarterRequestMixin(request) key = request_mixin.cache_key print(key) # e.g., 'a1b2c3d4e5f6...'
- property data: dict | list | str | None[source]
Get the request body data as a dictionary, list or str.
Used for setting the session_key.
- Returns:
The request body data as a dict, list, or str, or None if not available.
Example:
request_mixin = SmarterRequestMixin(request) data = request_mixin.data print(data) # e.g., {'session_key': 'abc123', ...}
- property domain: str | None[source]
Extracts the domain from the URL.
- Returns:
The domain or None if not found.
examples:
- https://hr.3141-5926-5359.alpha.api.example.com/chatbot/ returns 'hr.3141-5926-5359.alpha.api.example.com'
- eval_chatbot_url()[source]
If we are a chatbot, based on analysis of the URL format then we need to make a follow up check of the user and account.
Examples
- 1.) For named urls, we extract the account number from the url,
then we load the account and admin user for that account.
- 2.) For smarter api urls, we would extract the chatbot id from the url,
then we would load the chatbot, account, and admin user for that account.
- 3.) For cli api urls, we would extract the chatbot name from the url,
then we would load the chatbot, account, and admin user for that account.
- find_session_key()[source]
Returns the unique chat session key value for this request.
The session_key is managed by the /config/ endpoint for the chatbot. The React app calls this endpoint at app initialization to get a JSON dict that includes, among other info, this session_key, which uniquely identifies the device and the individual chatbot session for the device.
For subsequent chat prompt requests, the session_key is intended to be sent in the body of the request as a key-value pair, e.g. {“session_key”: “1234567890”}.
This method will also check the request headers and cookies for the session_key. The session key can be found in one of the following:
URL parameter: http://localhost:9357/workbench/example/config/?session_key=1aeee4c1f183354247f43f80261573da921b0167c7c843b28afd3cb5ebba0d9a
Request JSON body: {‘session_key’: ‘1aeee4c1f183354247f43f80261573da921b0167c7c843b28afd3cb5ebba0d9a’}
Request header: {‘session_key’: ‘1aeee4c1f183354247f43f80261573da921b0167c7c843b28afd3cb5ebba0d9a’}
Cookie
A session_key generator
- property formatted_class_name: str[source]
Returns the class name in a formatted string along with the name of this mixin.
- Returns:
Formatted class name string.
- generate_session_key()[source]
Generate a session_key based on a unique string and the current datetime.
- Return type:
- Returns:
A unique session key string.
- get_cookie_value(cookie_name)[source]
Retrieve the value of a cookie from the request object.
- Parameters:
request – Django HttpRequest object
cookie_name – Name of the cookie to retrieve
- Returns:
Value of the cookie or None if the cookie does not exist
- invalidate_cached_properties()[source]
Invalidates all cached properties on the instance to force re-evaluation.
This method removes all attributes cached by @cached_property decorators from the instance’s __dict__. It is useful for testing or when the request object changes and you need to ensure that all dependent properties are recalculated.
Example:
from smarter.lib.django.request import SmarterRequestMixin class Foo(SmarterRequestMixin): pass foo = Foo(request) foo.invalidate_cached_properties(request)
- Raises:
None –
- property ip_address: str | None[source]
Get the client’s IP address from the request object.
This property attempts to extract the IP address from the request’s META dictionary, using the “REMOTE_ADDR” key. If the IP address is not available, it returns None.
- Returns:
The client’s IP address as a string, or None if not found.
- Return type:
Optional[str]
Example:
request_mixin = SmarterRequestMixin(request) ip = request_mixin.ip_address print(ip) # e.g., '192.168.1.100'
- property is_chatbot: bool[source]
Returns True if the URL resolves to a chatbot endpoint.
Conditions are checked in a lazy sequence to avoid unnecessary processing.
Examples
http://localhost:9357/workbench/chatbots/<str:hashed_id>/chat/
http://localhost:9357/workbench/chatbots/<str:hashed_id>/config/
http://localhost:9357/workbench/chatbots/<str:hashed_id>/manifest/
- Returns:
True if the URL is a chatbot endpoint, otherwise False.
- Return type:
- property is_chatbot_cli_api_url: bool[source]
9357/api/v1/cli/chat/example/.
- The expected path parts are:
[‘api’, ‘v1’, ‘cli’, ‘chat’, ‘example’]
- Returns:
True if the URL matches the CLI chatbot API pattern, otherwise False.
- Return type:
- Type:
Returns True if the URL is of the form http
- Type:
//localhost
- property is_chatbot_named_url: bool[source]
Returns True if the url is of the form:
- Returns:
True if the URL matches the named chatbot pattern, otherwise False.
- Return type:
- property is_chatbot_sandbox_url: bool[source]
Example URLs for chatbot sandbox endpoints.
Examples
Web console urls: - http://localhost:9357/workbench/chatbots/<str:hashed_id>/chat/ - http://localhost:9357/workbench/chatbots/<str:hashed_id>/config/ - http://localhost:9357/workbench/chatbots/<str:hashed_id>/manifest/
Api urls: - http://localhost:9357/api/v1/prompt/1/chat/ - http://localhost:9357/api/v1/prompt/1/config/
Manifest view urls: https://alpha.platform.smarter.sh/workbench/chatbots/hashed_id/ https://<environment_domain>/workbench/chatbots/<str:hashed_id>/ path_parts: [‘workbench’, ‘chatbots’, ‘rxy123hashedx’]
- Returns:
True if the URL matches a chatbot sandbox endpoint, otherwise False.
- Return type:
- property is_chatbot_smarter_api_url: bool[source]
- Returns True if the URL is of the form:
http://localhost:9357/api/v1/workbench/1/chat/ path_parts: [‘api’, ‘v1’, ‘workbench’, ‘<int:pk>’, ‘chat’]
http://localhost:9357/api/v1/chatbots/1556/chat/ path_parts: [‘api’, ‘v1’, ‘chatbots’, ‘<int:pk>’, ‘chat’]
- Returns:
True if the URL matches a smarter API chatbot endpoint, otherwise False.
- Return type:
- property is_config: bool[source]
Returns True if the URL resolves to a config endpoint.
Examples
http://testserver/api/v1/cli/chat/config/testc7098865f39202d5/ http://localhost:9357/workbench/example/config/?session_key=1aeee4c1f183354247f43f80261573da921b0167c7c843b28afd3cb5ebba0d9a http://localhost:9357/api/v1/workbench/<int:chatbot_id>/chat/config/ http://example.api.localhost:9357/config
- Returns:
True if the URL is a config endpoint, otherwise False.
- Return type:
- property is_dashboard: bool[source]
Returns True if the URL resolves to a dashboard endpoint.
- Returns:
True if the URL is a dashboard endpoint, otherwise False.
- Return type:
- property is_default_domain: bool[source]
Returns True if the URL is the default domain for the environment.
Example
api.alpha.platform.smarter.sh
- Returns:
bool: True if the URL is the default environment domain, otherwise False.
- property is_environment_root_domain: bool[source]
Returns True if the URL resolves to the environment root domain.
- Returns:
True if the URL is the environment root domain, otherwise False.
- Return type:
- is_internal_api_request(request)[source]
Check if the request is an internal API request.
This method checks for a custom attribute on the request object that indicates whether the request is an internal API request. This can be used to bypass certain authentication or permission checks for internal requests.
- Parameters:
request (
HttpRequest) – The Django request object.- Returns:
True if it’s an internal API request, False otherwise.
- Return type:
- property is_requestmixin_ready: bool[source]
Returns True if the request mixin is ready for processing. This is a convenience property to check if the request is ready.
- Returns:
True if the request mixin is ready, False otherwise.
- property is_smarter_api: bool[source]
9357/api/v1/.
Examples
path_parts: [‘api’, ‘v1’, ‘chatbots’, ‘1’, ‘chat’]
- Returns:
True if the URL matches the smarter API pattern, otherwise False.
- Return type:
- Type:
Returns True if the URL is of the form http
- Type:
//localhost
- property is_workbench: bool[source]
Returns True if the URL resolves to a workbench endpoint.
- Returns:
True if the URL is a workbench endpoint, otherwise False.
- Return type:
- property params: QueryDict[source]
The query string parameters from the Django request object.
This extracts the query string parameters from the request object and converts them to a dictionary. Used in child views to pass optional command-line parameters to the broker.
- Returns:
QueryDict containing the query string parameters.
Example:
request_mixin = SmarterRequestMixin(request) params = request_mixin.params print(params) # e.g., {'session_key': 'abc123', 'uid': 'xyz'}
- property parsed_url: ParseResult | None
Expose the private ParseResult URL object as a public property.
- Returns:
The parsed URL as a ParseResult object.
Example:
request_mixin = SmarterRequestMixin(request) parsed = request_mixin.parsed_url print(parsed.netloc) # e.g., 'example.com'
- property path: str | None[source]
Extracts the path from the URL.
- Returns:
Optional[str]: The path as a string, or None if not found.
Examples
https://hr.3141-5926-5359.alpha.api.example.com/chatbot/ returns ‘/chatbot/’
- property qualified_request: bool
A cursory screening of the WSGI request object to look for any disqualifying conditions that confirm this is not a request that we are interested in.
The request is considered “qualified” if all of the following are true:
The request object (self._smarter_request) is present.
The URL path is present and non-empty.
The request does not originate from an internal AWS Kubernetes subnet (netloc starts with 192.168).
The path is not in the list of amnesty_urls.
The path does not start with /admin/.
The path does not start with /docs/.
The path does not end with a static file extension (e.g., .css, .js, .png, .jpg, .jpeg, .gif, .svg, .woff, .woff2, .ttf, .eot, .ico).
- Returns:
True if the request passes all checks and is of interest, otherwise False.
Example:
# True case: a valid chatbot request request_mixin = SmarterRequestMixin(request) if request_mixin.qualified_request: print("This is a qualified chatbot request.") # False case: a static asset or admin/docs request static_request = SmarterRequestMixin(static_asset_request) if not static_request.qualified_request: print("This request is not of interest.")
- property ready: bool
returns True if the request is ready for processing.
- Returns:
True if the request is ready, False otherwise.
- property request_mixin_ready_state: str
Returns a string representation of the request mixin’s ready state.
- Returns:
A string indicating whether the request mixin is ready or not.
- property root_domain: str | None[source]
Extracts the root domain from the URL.
- Returns:
The root domain or None if not found.
Example:
request_mixin = SmarterRequestMixin(request) print(request_mixin.root_domain) # For 'https://hr.3141-5926-5359.alpha.api.example.com/chatbot/' → 'smarter.sh' # For 'http://localhost:9357/' → 'localhost'
- property session_key: str
Getter for the session_key property.
The session_key is a unique identifier for a chat session. It is used to identify the chat session across multiple requests. If the session_key is not already set, it attempts to find it in the URL parameters. Barring that, it generates a new one.
- Returns:
The session key as a string.
Example:
request_mixin = SmarterRequestMixin(request) session_key = request_mixin.session_key print(session_key) # e.g., '38486326c21ef4bcb7e7bc305bdb062f16ee97ed8d2462dedb4565c860cd8ecc'
- set_is_internal_api_request(request, value=True)[source]
Set the internal API request attribute on the request object.
This method allows you to mark a request as an internal API request by setting a custom attribute on the request object. This can be used in middleware or views to indicate that the request should be treated as internal.
- Parameters:
request (
HttpRequest) – The Django request object.value (
bool) – The value to set for the internal API request attribute (default is True).
- Returns:
The modified Django request object.
- Return type:
- property smarter_request: Request | HttpRequest | ASGIRequest | MagicMock | None
Returns the current request object.
This property is named to avoid potential name collisions in child classes. This property is preferred over standard Django request types in that it more elegantly resolves idiosyncratic usage like Unit tests, Sphinx docs, and other non-standard request objects.
Example:
request_mixin = SmarterRequestMixin(request) req = request_mixin.smarter_request
- Returns:
The current request object.
- property smarter_request_chatbot_id: int | None
Extract the chatbot id from the URL.
Example
http://localhost:9357/workbench/chatbots/rMTAwMDAyNwx/chat/
returns the pk id that when decoded from the hashed ID format corresponds to the chatbot id.
- Returns:
The chatbot id as an integer, or None if not found.
- property smarter_request_chatbot_name: str | None[source]
Extract the chatbot name from the URL.
Example
http://example.3141-5926-5359.api.localhost:9357/config
returns “example”
- Returns:
The chatbot name as a string, or None if not found.
- property smarter_request_user: AnonymousUser | User | None
Returns the user associated with the request
This property is named to avoid potential name collisions in child classes. It retrieves the user from the request object if available.
Example:
request_mixin = SmarterRequestMixin(request) user = request_mixin.smarter_request_user
- Returns:
The user associated with the request, or None if not available.
- property subdomain: str | None[source]
Extracts the subdomain from the URL.
- Returns:
The subdomain or None if not found.
Example:
request_mixin = SmarterRequestMixin(request) sub = request_mixin.subdomain print(sub) # e.g., 'hr.3141-5926-5359.alpha' for # 'https://hr.3141-5926-5359.alpha.api.example.com/chatbot/'
- property timestamp
Create a consistent timestamp based on the time that this object was instantiated.
- Returns:
The timestamp as a datetime object.
Example:
request_mixin = SmarterRequestMixin(request) ts = request_mixin.timestamp print(ts) # e.g., 2025-12-01 12:34:56.789012
- property uid: str | None
Unique identifier for the client.
This is assumed to be a combination of the machine MAC address and the hostname.
- Returns:
The client UID as a string, or None if not available.
Example:
request_mixin = SmarterRequestMixin(request) uid = request_mixin.uid print(uid) # e.g., '00:1A:2B:3C:4D:5E-myhost'
- property unique_client_string: str
Generate a unique string based on several request attributes.
This string is used for generating session_key and client_key.
- The unique string is composed of:
Account number
URL
User agent
IP address
Timestamp
- Returns:
A unique string representing the client and request context.
- Return type:
Example:
request_mixin = SmarterRequestMixin(request) unique_str = request_mixin.unique_client_string print(unique_str)
- property url: str | None
The string representation of the ParseResult object stored in _parsed_url.
- Returns:
The URL as a string.
Example:
request_mixin = SmarterRequestMixin(request) url_str = request_mixin.url print(url_str) # e.g., 'https://example.com/path/'
- property url_account_number: str | None
Extract the account number from the URL using the pattern defined in SmarterValidator.VALID_ACCOUNT_NUMBER_PATTERN.
Example
http://example.3141-5926-5359.api.localhost:9357/config
returns “3141-5926-5359”
- Returns:
The account number as a string, or None if not found.
- property url_path_parts: list[str][source]
Extract the path parts from the URL.
- Returns:
A list of strings representing each part of the URL path.
Example:
request_mixin = SmarterRequestMixin(request) parts = request_mixin.url_path_parts print(parts) # e.g., ['api', 'v1', 'workbench', '1', 'chat']
- property user_agent: str | None
Get the client’s user agent string from the request object.
This property attempts to extract the user agent from the request’s META dictionary, using the “HTTP_USER_AGENT” key. If the user agent is not available, it returns a default value.
- Returns:
The client’s user agent string, or None if not found.
- Return type:
Optional[str]
Example:
request_mixin = SmarterRequestMixin(request) ua = request_mixin.user_agent print(ua) # e.g., 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)...'