This repository has been archived on 2020-09-24. You can view files and clone it, but cannot push or open issues or pull requests.
consumer-api/consumer_api/views/sync_view.py

39 lines
1.1 KiB
Python
Raw Normal View History

2020-03-29 18:12:16 +02:00
import json
2020-05-08 19:34:29 +02:00
import time
2020-03-29 18:12:16 +02:00
from flask import request, current_app, jsonify
2020-03-29 17:08:53 +02:00
from flask_classful import FlaskView
2020-03-29 18:12:16 +02:00
from db import redis_client # ez nagyon otvar
2020-03-29 17:08:53 +02:00
class SyncView(FlaskView):
2020-03-29 18:12:16 +02:00
def post(self):
remote_uuid = request.json['uuid']
remote_ip = request.remote_addr
2020-05-08 19:34:29 +02:00
cust_key = f"customer_{remote_uuid}"
2020-03-29 18:12:16 +02:00
2020-05-08 19:34:29 +02:00
last_known_info = json.loads((redis_client.get("cust_key") or b"{}").decode('utf-8'))
if not last_known_info:
2020-03-29 18:12:16 +02:00
current_app.logger.info(f"New consumer registered (unknown UUID): {remote_uuid} at {remote_ip}")
2020-05-08 19:34:29 +02:00
else:
if last_known_info['ip'] != remote_ip:
2020-03-29 18:12:16 +02:00
current_app.logger.info(f"Address of consumer {remote_uuid} changed to {remote_ip}")
2020-05-08 19:34:29 +02:00
info = {
"uuid": remote_uuid,
"ip": remote_ip,
"last_seen": time.time()
}
2020-03-29 18:12:16 +02:00
2020-05-08 19:34:29 +02:00
redis_client.set(cust_key, json.dumps(info).encode('utf-8'))
redis_client.expire(cust_key, 30)
2020-03-29 18:12:16 +02:00
response = {
"uuid": current_app.config['LOCAL_UUID']
}
return jsonify(response)