Skip to content

Instantly share code, notes, and snippets.

@y-abe
Last active December 14, 2019 01:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save y-abe/ede8f5560b377575c99fa7fbf29d7467 to your computer and use it in GitHub Desktop.
Save y-abe/ede8f5560b377575c99fa7fbf29d7467 to your computer and use it in GitHub Desktop.
import time
import json
import requests
import logging
import os
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def send_event(params, retry_count=0):
try:
resp = requests.get('http://s2s.adjust.com/event', params=params)
resp.raise_for_status()
time.sleep(0.01)
except Exception as err:
if retry_count > 0:
status = resp.status_code
headers = resp.headers
encoding = resp.encoding
body = resp.text
if ('content-type' in headers) and headers['content-type'] == 'application/json; charset=utf-8':
try:
j = json.loads(body)
if ('error' in j) and isinstance(j['error'], str):
#
# 実際はここでエラーに応じて処理をしている
#
pass
except Exception as e:
logging.error(f"Unexpected errors occur: {e}")
logging.error(f"requests to adjust is failed: status_code: {status}, header: {headers}, encoding: {encoding}, body: {body}")
time.sleep(0.1)
send_event(params, retry_count-1)
raise err
def lambda_handler(event, context):
num_retries = int(os.getenv('NUM_RETRIES', '3'))
num_events = 0
num_success = 0
for record in event['Records']:
if 'NewImage' not in record['dynamodb']:
continue
num_events += 1
data = record['dynamodb']['NewImage']
try:
params = {
'app_token': data['app_token']['S'],
'event_token': data['event_token']['S'],
's2s': 1,
'created_at_unix': data['created_at_unix']['N'],
}
# 値がなければ - を入れているため
if data['idfa']['S'] and data['idfa']['S'] != '-':
params['idfa'] = data['idfa']['S']
if data['idfv']['S'] and data['idfv']['S'] != '-':
params['idfv'] = data['idfv']['S']
if data['gps_adid']['S'] and data['gps_adid']['S'] != '-':
params['gps_adid'] = data['gps_adid']['S']
except Exception:
logging.error(f'failed to parse record: {data}')
continue
try:
send_event(params, retry_count=num_retries)
num_success += 1
except Exception:
logging.error(f'failed to send event {params}')
return f'successfully sent {num_success} events / {num_events} events'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment