def get_spocs(self): targeting = { "site": self.site } # setting site here by default so it's picked up by API try: geo = self.geolocation.get_city(self.ip) targeting['country'] = self.geolocation.get_country(geo) targeting['region'] = self.geolocation.get_region(geo) except Exception as e: logging.warning( "Could not target based on geolocation. {0}".format(str(e))) if self.pocket_id: targeting['pocket_id'] = self.pocket_id else: logging.warning( "Could not target based on pocket_id because it's missing.") targeting['placements'] = self.placements adzerk_api = AdZerk(**targeting) decisions = adzerk_api.get_decisions() response = { 'settings': conf.spocs['settings'], } self.__transform_spocs(response, decisions) if conf.env in ('development', 'staging'): response['__debug__'] = adzerk_api.get_decision_body() return response
def delete_user(): pocket_id = request.json['pocket_id'] adzerk_api = AdZerk(pocket_id=pocket_id) response = adzerk_api.delete_user() return jsonify({'status': int(response.status_code == 200) }), response.status_code
def test_new_placements(self): api = Api(pocket_id="{123}", placements=mock_placements) body = api.get_decision_body() self.assertTrue(2, len(body['placements'])) for p in body['placements']: self.assertEqual(10250, p['networkId']) self.assertEqual(1070098, p['siteId']) self.assertEqual(20, p['count'])
def test_delete_user(self): url = 'https://e-10250.adzerk.net/udb/10250/?userKey=%7B123%7D' responses.add(responses.DELETE, url, status=200) api = Api(pocket_id="{123}") api.delete_user() self.assertEqual(1, len(responses.calls)) request = responses.calls[0].request self.assertEqual(url, request.url) self.assertEqual('DUMMY_123', request.headers['X-Adzerk-ApiKey'])
def test_update_api_key(self, mock_get_api_key): url = 'https://e-10250.adzerk.net/udb/10250/?userKey=%7B123%7D' responses.add(responses.DELETE, url, status=401) responses.add(responses.DELETE, url, status=200) api = Api(pocket_id="{123}") api.delete_user() self.assertEqual(2, len(responses.calls)) request = responses.calls[0].request self.assertEqual(url, request.url) self.assertEqual('OUT_OF_DATE_456', request.headers['X-Adzerk-ApiKey']) request = responses.calls[1].request self.assertEqual(url, request.url) self.assertEqual('DUMMY_123', request.headers['X-Adzerk-ApiKey'])
def test_site_is_not_stored_in_conf(self): api = Api(pocket_id="{123}", country='USA', region='CA', site=1084367) body = api.get_decision_body() self.assertEqual(1084367, body['placements'][0]['siteId']) api = Api(pocket_id="{123}", country='USA', region='CA') body = api.get_decision_body() self.assertEqual(1070098, body['placements'][0]['siteId'])
def test_keywords_empty(self): api = Api(pocket_id="{123}") body = api.get_decision_body() self.assertFalse('keywords' in body)
def test_keywords(self): api = Api(pocket_id="{123}", country='USA', region='CA') body = api.get_decision_body() self.assertTrue('country-USA' in body['keywords']) self.assertTrue('region-CA' in body['keywords'])