django中间件支持视图和视图集函数中的等幂键。

django-idempotency-ke的Python项目详细描述


Circle CIcodecov.io

等幂键django中间件

中间件到django,它从请求头中提取幂等键,如果已经指定了幂等键,它将自动返回以前的响应数据。 有两个中间件类允许视图函数根据需要分别选择加入或退出。

安装

pip install django_idempotency_key

配置

首先,在设置文件下添加到中间件设置。

如果希望所有非安全视图功能都自动使用等幂键,请使用以下命令:

MIDDLEWARE = [
   ...
   'idempotency_key.middleware.IdempotencyKeyMiddleware',
]

警告-将此添加为中间件将要求所有到非安全http方法的客户端请求提供在http幂等键下的请求头中指定的幂等键。如果缺少,则返回400个错误请求。

但是,如果您希望默认情况下所有视图功能都是免税的,并且您将以每个视图功能为基础进入,请使用以下选项:

MIDDLEWARE = [
   ...
   'idempotency_key.middleware.ExemptIdempotencyKeyMiddleware',
]

装饰工

有三个decorator可用于控制等幂键在视图函数中的工作方式。

@等幂键

这将确保指定的视图函数使用等幂键,并期望客户端发送http等幂键(等幂键)头。

注意:如果使用了idempotencykeymidware类,则此装饰器是多余的。

@等幂键豁免

这将确保指定的视图函数不受等幂键的影响,并且每次都会运行具有相同数据的多个请求。

注意:如果使用了exemptIdempotencyKeyMiddleware类,则此修饰符是多余的。

@等幂键手册

指定时,视图函数将指示冲突时提供的响应。装饰器将在请求对象上设置两个变量,如果密钥存在于存储中,则通知用户,如果存在密钥,则响应对象在最后一次调用上。

这两个变量的定义如下:

(boolean) request.idempotency_key_exists
(object) request.idempotency_key_response

idempotency_key_response将始终返回已设置的响应对象。

所需标题

当视图函数上启用了等幂键时,调用客户端必须在名为http等幂键的头中指定唯一键。如果缺少,则返回400个错误响应。

设置

以下设置可用于修改等幂密钥中间件的行为。

from idempotency_key import status

IDEMPOTENCY_KEY = {
    # Specify the key encoder class to be used for idempotency keys.
    # If not specified then defaults to 'idempotency_key.encoders.BasicKeyEncoder'
    'ENCODER_CLASS': 'idempotency_key.encoders.BasicKeyEncoder',

    # Set the response code on a conflict.
    # If not specified this defaults to HTTP_409_CONFLICT
    # If set to None then the original request's status code is used.
    'CONFLICT_STATUS_CODE': status.HTTP_409_CONFLICT,

    'STORAGE': {
        # Specify the storage class to be used for idempotency keys
        # If not specified then defaults to 'idempotency_key.storage.MemoryKeyStorage'
        'CLASS': 'idempotency_key.storage.MemoryKeyStorage',

        # Name of the django cache configuration to use for the CacheStorageKey storage class.
        # This can be overriden using the @idempotency_key(cache_name='MyCacheName') view/viewset function decorator.
        'CACHE_NAME': 'default',

        # When the response is to be stored you have the option of deciding when this happens based on the responses
        # status code. If the response status code matches one of the statuses below then it will be stored.
        # The statuses below are the defaults used if this setting is not specified.
        'STORE_ON_STATUSES': [
            status.HTTP_200_OK,
            status.HTTP_201_CREATED,
            status.HTTP_202_ACCEPTED,
            status.HTTP_203_NON_AUTHORITATIVE_INFORMATION,
            status.HTTP_204_NO_CONTENT,
            status.HTTP_205_RESET_CONTENT,
            status.HTTP_206_PARTIAL_CONTENT,
            status.HTTP_207_MULTI_STATUS,
        ]
    },

    # The following settings deal with the process/thread lock that can be placed around the cache storage object
    # to ensure that multiple threads do not try to call the same view/viewset method at the same time.
    'LOCK': {    
        # Specify the key object locking class to be used for locking access to the cache storage object.
        # If not specified then defaults to 'idempotency_key.locks.ThreadLock'
        'CLASS': 'idempotency_key.locks.ThreadLock',
    
        # Location of the Redis server if MultiProcessRedisLock is used otherwise this is ignored.
        # The host name can be specified or both the host name and the port separated by a colon ':'        
        'LOCATION': 'localhost:6379',
    
        # The unique name to be used accross processes for the lock. Only used by the MultiProcessRedisLock class
        'NAME': 'MyLock',
        
        # The maximum time to live for the lock. If a lock is given and is never released this timeout forces the release
        # The lock time is in seconds and the default is None which means lock until it is manually released
        'TTL': None,
    
        # The use of a lock around the storage object so that only one thread at a time can access it.
        # By default this is set to true. WARNING: setting this to false may allow duplicate calls to occur if the timing 
        # is right. 
        'ENABLE': True,
        
        # If the ENABLE_LOCK setting is True above then this represents the timeout (in seconds as a floating point number) 
        # to occur before the thread gives up waiting. If a timeout occurs the middleware will return a HTTP_423_LOCKED 
        # response.
        'TIMEOUT': 0.1,
    },

}

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
内存Java正在运行。jar heapdump错误   java如何在安卓画布中弯曲文本区域?   java如何在Gdx 安卓游戏编程中获得矩形的真实触碰位置?   找不到java Spring MVC控制器   在Java中使用双重检查锁定单例扩展类   java在高效的时间和内存中动态执行insert(索引、数据)、delete(索引)、getAt(索引)操作。   java 安卓 Toast和视图帮助   java协议缓冲区:从文件中读取所有序列化消息   java如何在Jackson中为参数化接口类型执行通用自定义反序列化程序   与简单的空检查相比,使用(平面)映射的java优势是什么?   异步方法seam中的java Get contextparam   jar使用相同的java运行时运行另一个java程序   java访问Spring批处理中的作业参数   java给定字符串为空或null   在h2数据库1.4中找不到java类“org.h2.fulltext.FullTextLucene”。*不适用于Lucene Core 4*   java Spring Boot在使用@enableSync时不响应任何请求   java错误:在bash上找不到或加载主类pj2   “返回对象”和“返回(对象)”之间的Java差异   java Android开发:如何使用onKeyUp?