mirror of
https://github.com/marcel-dempers/docker-development-youtube-series.git
synced 2025-06-06 17:01:30 +00:00
updates
This commit is contained in:
parent
23767a5644
commit
c23f3a0cdc
37
python/introduction/part-5.database.redis/customers.json
Normal file
37
python/introduction/part-5.database.redis/customers.json
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
{
|
||||||
|
"a": {
|
||||||
|
"customerID": "a",
|
||||||
|
"firstName": "James",
|
||||||
|
"lastName": "Baker"
|
||||||
|
},
|
||||||
|
"b": {
|
||||||
|
"customerID": "b",
|
||||||
|
"firstName": "Jonathan",
|
||||||
|
"lastName": "D"
|
||||||
|
},
|
||||||
|
"c": {
|
||||||
|
"customerID": "c",
|
||||||
|
"firstName": "Aleem",
|
||||||
|
"lastName": "Janmohamed"
|
||||||
|
},
|
||||||
|
"d": {
|
||||||
|
"customerID": "d",
|
||||||
|
"firstName": "Ivo",
|
||||||
|
"lastName": "Galic"
|
||||||
|
},
|
||||||
|
"e": {
|
||||||
|
"customerID": "e",
|
||||||
|
"firstName": "Joel",
|
||||||
|
"lastName": "Griffiths"
|
||||||
|
},
|
||||||
|
"f": {
|
||||||
|
"customerID": "f",
|
||||||
|
"firstName": "Michael",
|
||||||
|
"lastName": "Spinks"
|
||||||
|
},
|
||||||
|
"g": {
|
||||||
|
"customerID": "g",
|
||||||
|
"firstName": "Victor",
|
||||||
|
"lastName": "Savkov"
|
||||||
|
}
|
||||||
|
}
|
@ -1,2 +1,2 @@
|
|||||||
Flask == 2.0.2
|
Flask == 2.0.2
|
||||||
Redis == 3.5.3
|
redis == 3.5.3
|
@ -1,99 +1,101 @@
|
|||||||
import os.path
|
import os.path
|
||||||
import csv
|
import csv
|
||||||
import os
|
import json
|
||||||
import json
|
import time
|
||||||
import time
|
from flask import Flask
|
||||||
from flask import Flask
|
from flask import request
|
||||||
from flask import request
|
import os
|
||||||
import redis
|
from redis.sentinel import Sentinel
|
||||||
from redis.sentinel import Sentinel
|
|
||||||
|
dataPath = "./customers.json"
|
||||||
redis_sentinels = os.environ.get('REDIS_SENTINELS')
|
|
||||||
redis_master_name = os.environ.get('REDIS_MASTER_NAME')
|
redis_sentinels = os.environ.get('REDIS_SENTINELS')
|
||||||
redis_password = os.environ.get('REDIS_PASSWORD')
|
redis_master_name = os.environ.get('REDIS_MASTER_NAME')
|
||||||
|
redis_password = os.environ.get('REDIS_PASSWORD')
|
||||||
class Customer:
|
|
||||||
def __init__(self, c="",f="",l=""):
|
def redis_command(command, *args):
|
||||||
self.customerID = c
|
max_retries = 3
|
||||||
self.firstName = f
|
count = 0
|
||||||
self.lastName = l
|
backoffSeconds = 5
|
||||||
def fullName(self):
|
while True:
|
||||||
return self.firstName + " " + self.lastName
|
try:
|
||||||
|
return command(*args)
|
||||||
def redis_command(command, *args):
|
except (redis.exceptions.ConnectionError, redis.exceptions.TimeoutError):
|
||||||
max_retries = 3
|
count += 1
|
||||||
count = 0
|
if count > max_retries:
|
||||||
backoffSeconds = 5
|
raise
|
||||||
while True:
|
print('Retrying in {} seconds'.format(backoffSeconds))
|
||||||
try:
|
time.sleep(backoffSeconds)
|
||||||
return command(*args)
|
|
||||||
except (redis.exceptions.ConnectionError, redis.exceptions.TimeoutError):
|
class Customer:
|
||||||
count += 1
|
def __init__(self, c="",f="",l=""):
|
||||||
if count > max_retries:
|
self.customerID = c
|
||||||
raise
|
self.firstName = f
|
||||||
print('Retrying in {} seconds'.format(backoffSeconds))
|
self.lastName = l
|
||||||
time.sleep(backoffSeconds)
|
def fullName(self):
|
||||||
|
return self.firstName + " " + self.lastName
|
||||||
def getCustomers():
|
|
||||||
customers = {}
|
def getCustomers():
|
||||||
customerIDs = redis_command(redis_master.scan_iter, "*")
|
customers = {}
|
||||||
for customerID in customerIDs:
|
customerIDs = redis_command(redis_master.scan_iter, "*")
|
||||||
customer = getCustomer(customerID)
|
for customerID in customerIDs:
|
||||||
customers[customer["customerID"]] = customer
|
customer = getCustomer(customerID)
|
||||||
|
customers[customer["customerID"]] = customer
|
||||||
return customers
|
return customers
|
||||||
|
|
||||||
def getCustomer(customerID):
|
def getCustomer(customerID):
|
||||||
customer = redis_command(redis_master.get, customerID)
|
customer = redis_command(redis_master.get, customerID)
|
||||||
|
|
||||||
if customer == None:
|
if customer == None:
|
||||||
return {}
|
return {}
|
||||||
else:
|
else:
|
||||||
c = str(customer, 'utf-8')
|
c = str(customer, 'utf-8')
|
||||||
return json.loads(c)
|
return json.loads(c)
|
||||||
|
|
||||||
def updateCustomer(customer):
|
def updateCustomer(customer):
|
||||||
redis_command(redis_master.set, customer.customerID, json.dumps(customer.__dict__))
|
redis_command(redis_master.set,customer.customerID, json.dumps(customer.__dict__) )
|
||||||
|
|
||||||
def updateCustomers(customers):
|
def updateCustomers(customers):
|
||||||
for customer in customers:
|
for customer in customers:
|
||||||
updateCustomer(customer)
|
updateCustomer(customer)
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
sentinels = []
|
sentinels = []
|
||||||
|
|
||||||
for s in redis_sentinels.split(","):
|
for s in redis_sentinels.split(","):
|
||||||
sentinels.append((s.split(":")[0], s.split(":")[1]))
|
sentinels.append((s.split(":")[0], s.split(":")[1]))
|
||||||
|
|
||||||
redis_sentinel = Sentinel(sentinels, socket_timeout=5)
|
redis_sentinel = Sentinel(sentinels, socket_timeout=5)
|
||||||
redis_master = redis_sentinel.master_for(redis_master_name,password = redis_password, socket_timeout=5)
|
redis_master = redis_sentinel.master_for(redis_master_name,password = redis_password, socket_timeout=5)
|
||||||
|
|
||||||
@app.route("/", methods=['GET'])
|
|
||||||
def get_customers():
|
@app.route("/", methods=['GET'])
|
||||||
customers = getCustomers()
|
def get_customers():
|
||||||
return json.dumps(customers)
|
customers = getCustomers()
|
||||||
|
return json.dumps(customers)
|
||||||
@app.route("/get/<string:customerID>", methods=['GET'])
|
|
||||||
def get_customer(customerID):
|
@app.route("/get/<string:customerID>", methods=['GET'])
|
||||||
customer = getCustomer(customerID)
|
def get_customer(customerID):
|
||||||
|
customer = getCustomer(customerID)
|
||||||
if customer == {}:
|
|
||||||
return {}, 404
|
if customer == {}:
|
||||||
else:
|
return {}, 404
|
||||||
return customer
|
else:
|
||||||
|
return customer
|
||||||
@app.route("/set", methods=['POST'])
|
|
||||||
def add_customer():
|
@app.route("/set", methods=['POST'])
|
||||||
jsonData = request.json
|
def add_customer():
|
||||||
|
jsonData = request.json
|
||||||
if "customerID" not in jsonData:
|
|
||||||
return "customerID required", 400
|
if "customerID" not in jsonData:
|
||||||
if "firstName" not in jsonData:
|
return "customerID required", 400
|
||||||
return "firstName required", 400
|
if "firstName" not in jsonData:
|
||||||
if "lastName" not in jsonData:
|
return "firstName required", 400
|
||||||
return "lastName required", 400
|
if "lastName" not in jsonData:
|
||||||
|
return "lastName required", 400
|
||||||
customer = Customer( jsonData["customerID"], jsonData["firstName"], jsonData["lastName"])
|
|
||||||
updateCustomer(customer)
|
customer = Customer( jsonData["customerID"], jsonData["firstName"], jsonData["lastName"])
|
||||||
return "success", 200
|
updateCustomer(customer)
|
||||||
|
return "success", 200
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user