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
|
||||
Redis == 3.5.3
|
||||
redis == 3.5.3
|
@ -1,25 +1,18 @@
|
||||
import os.path
|
||||
import csv
|
||||
import os
|
||||
import json
|
||||
import time
|
||||
from flask import Flask
|
||||
from flask import request
|
||||
import redis
|
||||
import os
|
||||
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_password = os.environ.get('REDIS_PASSWORD')
|
||||
|
||||
class Customer:
|
||||
def __init__(self, c="",f="",l=""):
|
||||
self.customerID = c
|
||||
self.firstName = f
|
||||
self.lastName = l
|
||||
def fullName(self):
|
||||
return self.firstName + " " + self.lastName
|
||||
|
||||
def redis_command(command, *args):
|
||||
max_retries = 3
|
||||
count = 0
|
||||
@ -34,13 +27,20 @@ def redis_command(command, *args):
|
||||
print('Retrying in {} seconds'.format(backoffSeconds))
|
||||
time.sleep(backoffSeconds)
|
||||
|
||||
class Customer:
|
||||
def __init__(self, c="",f="",l=""):
|
||||
self.customerID = c
|
||||
self.firstName = f
|
||||
self.lastName = l
|
||||
def fullName(self):
|
||||
return self.firstName + " " + self.lastName
|
||||
|
||||
def getCustomers():
|
||||
customers = {}
|
||||
customerIDs = redis_command(redis_master.scan_iter, "*")
|
||||
for customerID in customerIDs:
|
||||
customer = getCustomer(customerID)
|
||||
customers[customer["customerID"]] = customer
|
||||
|
||||
return customers
|
||||
|
||||
def getCustomer(customerID):
|
||||
@ -53,7 +53,7 @@ def getCustomer(customerID):
|
||||
return json.loads(c)
|
||||
|
||||
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):
|
||||
for customer in customers:
|
||||
@ -69,6 +69,7 @@ for s in redis_sentinels.split(","):
|
||||
redis_sentinel = Sentinel(sentinels, 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():
|
||||
customers = getCustomers()
|
||||
@ -97,3 +98,4 @@ def add_customer():
|
||||
customer = Customer( jsonData["customerID"], jsonData["firstName"], jsonData["lastName"])
|
||||
updateCustomer(customer)
|
||||
return "success", 200
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user