Exemplo n.º 1
0
 def __init__(self,
              api_url=None,
              host=None,
              port=None,
              context_root=None,
              server=None,
              db=None,
              user=None,
              password=None,
              client_type="db"):
     """
     Initializes the api client.
     :param api_url:
     :param host:
     :param port:
     :param context_root:
     """
     # Since we are using the db client for temporary purpose, we are not parameterising the arguments for the db
     # client but only parameterising the variables for api client.
     # We will instruct the function to read the parameters directly from the configuration file for db client.
     # We will use a hardcoded if condition variable and will switch back once the apis are ready
     if client_type == "db":
         self.client = DBClient(server=server,
                                db=db,
                                user=user,
                                password=password)
     else:
         self.client = APIClient()
Exemplo n.º 2
0
 def __init__(self, seller_uuid, retry=False):
     """Init API client."""
     self.__seller_uuid = seller_uuid
     seller_auth = SellerAuth.objects.get(seller=seller_uuid)
     profile_id_dict = dict(
         seller_auth.seller.sellerprofile_set.values_list(
             'profile_id',
             'country_code',
         ))
     self.__client = APIClient(seller_auth.access_token,
                               seller_auth.refresh_token, profile_id_dict)
     self.__retry_count = self._RETRY_COUNT if retry else 0
     self.__retry_seconds = self._RETRY_SECONDS if retry else 0
Exemplo n.º 3
0
    def auth(cls, email, code, client_id):
        seller_uuid = uuid.uuid4()
        access_token, refresh_token = APIClient.grant_auth(authcode=code)
        if not access_token:
            raise APIException('grant_auth failed')
        logger.info('%s start to retrieve_profile_dict' % client_id)
        profile_dict = APIClient(access_token,
                                 refresh_token).retrieve_profile_dict()
        logger.info('%s retrieve_profile_dict success' % client_id)
        sellers = CustomerSeller.objects.filter(customer_id=client_id).all()
        # 遍历查到的profiles 去验证用户是否授权过了
        for country, profile in profile_dict.items():
            if SellerProfile.objects.filter(
                    profile_id=profile['profile_id']) \
                    .filter(seller__in=sellers):
                # 如果profileId已经存在,表示已经授权过,返回false
                return False

        CustomerSeller.objects.create(customer_id=client_id,
                                      seller_uuid=seller_uuid,
                                      seller_email=email)
        seller = CustomerSeller.objects.filter(seller_uuid=seller_uuid).first()
        SellerAuth.objects.create(seller=seller,
                                  access_token=access_token,
                                  refresh_token=refresh_token,
                                  expire_after=datetime.datetime.now() +
                                  datetime.timedelta(hours=1))

        for seller in sellers:
            for country, profile in profile_dict.items():
                model, created = SellerProfile.objects.update_or_create(
                    profile_id=profile['profile_id'],
                    seller=seller,
                    defaults={
                        "country_code": profile['country'],
                        "currency_code": profile['currency'],
                        "daily_budget": profile['daily_budget'],
                        "timezone": profile['timezone'],
                        "marketplace_string_id": profile['marketplace_str_id'],
                        "amazon_account_id": profile['seller_str_id'],
                        "amazon_account_type": profile['account_type'],
                    })
                if created:
                    model.status = 'new'
                    model.save()

        # 没有授权过,返回true
        return True
def api_client():
    client = APIClient()
    client.login(ADMIN_USERNAME, ADMIN_PASSWORD)
    return client