• Django Rest Framework threoy


    rest_framework源码分析:

        1.as_view()

        2.父类的as_view() view = super(APIView, cls).as_view(**initkwargs)

        3.view 方法中 return self.dispatch(request, *args, **kwargs)

        4.dispatch

            1.将原来的request进行分装成restframework

                 request = self.initialize_request(request, *args, **kwargs)

                 self.request = request

            2.处理版本,认证,权限,访问频率

       self.initial(request, *args, **kwargs)

       1.处理版本信息

          version, scheme = self.determine_version(request, *args, **kwargs),request.version, request.versioning_scheme = version, scheme

              if self.versioning_class is None:return (None, None)没有版本控制类,不做版本控制

                  scheme = self.versioning_class()#如果有走版本控制类

                  return (scheme.determine_version(request, *args, **kwargs), scheme)

                      version = kwargs.get(self.version_param, self.default_version) 获取版本

                      if not self.is_allowed_version(version): 如果版本不存在或者不在允许范围内

                          

             

       2.处理认证信息

          self.perform_authentication(request)

              request.uer 进入带有装饰器@Propryty 下的uer方法

                  if not hasattr(self, '_user'): #判断是否没有认证

                      self._authenticate() #没有认证,进入_authenticate()认证

                          for authenticator in self.authenticators: #循环获取认证对象

                              authenticators=self.get_authenticators() #将认证对象封装到 authenticators

                                  return [auth() for auth in self.authentication_classes] #列表循环实例化authentication_classes列表的类的对象

                                      authentication_classes = api_settings.DEFAULT_AUTHENTICATION_CLASSES #默认使用setting的配置

                  #认证过了直接返回

                  return self._user

       3.处理权限信息

          self.check_permissions(request)

              for permission in self.get_permissions():   从permission_classes中/获取权限类,在执行对象的列表生成式

                   if not permission.has_permission(request, self): 执行每一个类的has_permission进行权限认证

                      permission_denied(self, request, message=None) #如果权限认证失败执行

                        def permission_denied(self, request, message=None):

                          """

                          If request is not permitted, determine what kind of exception to raise.

                          """

                          if request.authenticators and not request.successful_authenticator:

                              raise exceptions.NotAuthenticated('无权访问')

                          raise exceptions.PermissionDenied(detail=message)

                 

       4.处理访问频率限制

      self.check_throttles(request)

        for throttle in self.get_throttles(): 从throttle_classes=[MyThrottle,] 循环获取节流对象

          if not throttle.allow_request(request, self):  执行对象的allow_request(request, self)方法,成功返回True

             self.throttled(request, throttle.wait()) 访问频率限制执行throttled(request, throttle.wait())

                def throttled(self,request,wait): 在APIView中

                    class InnerThrottled(exceptions.Throttled):

                        default_detail = '请求被限制.'

                        extra_detail_singular = 'Expected available in {wait} second.'

                        extra_detail_plural = '还需要再等待{wait}秒'

                    raise InnerThrottled(wait)

                throttle.wait() 返回,剩余多少时间可以进行再访问

          在自定制的频率限制类中实现:

                 def wait(self):父类已经实现

         

            3.根据用户提交的方法反射请求方法

                handler = getattr(self, request.method.lower(),)

            4.将处理后的数据进行,序列化,路由,渲染等方式进行封装

                self.response = self.finalize_response(request, response, *args, **kwargs)

        

  • 相关阅读:
    CSS3 flexbox弹性布局实例
    移动端自适应:flexible.js可伸缩布局使用
    玩转HTML5移动页面(转自http://tqtan.com/)
    CSS3那些不为人知的高级属性
    android开发问题记录1——Android SDK Manager无法更新Packages
    Javascirpt详解之函数function
    SPFA 最短路
    Sonya and Robots
    三角形问题
    平方和与立方和 七夕节
  • 原文地址:https://www.cnblogs.com/mihon/p/8980961.html
Copyright © 2020-2023  润新知