Handle config not set cases more gracefully

This commit is contained in:
PCoder 2019-10-13 09:08:40 +05:30
parent ab1714e733
commit 1897cc087c
2 changed files with 28 additions and 12 deletions

View File

@ -1,7 +1,7 @@
import logging
from etcd3_wrapper import Etcd3Wrapper
from decouple import config
from decouple import config, UndefinedValueError
logging.basicConfig(
level=logging.DEBUG,
@ -11,7 +11,11 @@ logging.basicConfig(
datefmt='%Y-%m-%d:%H:%M:%S',
)
etcd_client = Etcd3Wrapper(host=config("ETCD_HOST"), port=config("ETCD_PORT"))
# load configs
APP_PORT = config("APP_PORT", 5000)
STRIPE_API_PRIVATE_KEY = config("STRIPE_API_PRIVATE_KEY")
try:
etcd_client = Etcd3Wrapper(host=config("ETCD_HOST"), port=config("ETCD_PORT"))
STRIPE_API_PRIVATE_KEY = config("STRIPE_API_PRIVATE_KEY")
except UndefinedValueError as uve:
print(str(uve))
exit(1)

View File

@ -2,7 +2,7 @@ import binascii
import json
import requests
from decouple import config, Csv
from decouple import config, Csv, UndefinedValueError
from datetime import datetime
from flask import Flask, request
from flask_restful import Resource, Api
@ -16,13 +16,26 @@ import time
app = Flask(__name__)
api = Api(app)
# load configs
AUTH_NAME = config("AUTH_NAME", "")
AUTH_TOKEN = TOTP(config("AUTH_SEED", "")).now()
AUTH_REALM = config("AUTH_REALM", "")
OTP_SERVER = config("OTP_SERVER", "")
OTP_VERIFY_ENDPOINT = config("OTP_VERIFY_ENDPOINT", "verify/")
try:
INIT_ORDER_ID = config("INIT_ORDER_ID")
REALM_ALLOWED = config("REALM_ALLOWED", cast=Csv(str))
except UndefinedValueError as uve:
print(str(uve))
exit(1)
def check_otp(name, realm, token):
try:
data = {
"auth_name": config("AUTH_NAME", ""),
"auth_token": TOTP(config("AUTH_SEED", "")).now(),
"auth_realm": config("AUTH_REALM", ""),
"auth_name": AUTH_NAME,
"auth_token": AUTH_TOKEN,
"auth_realm": AUTH_REALM,
"name": name,
"realm": realm,
"token": token,
@ -32,8 +45,8 @@ def check_otp(name, realm, token):
response = requests.post(
"{OTP_SERVER}{OTP_VERIFY_ENDPOINT}".format(
OTP_SERVER=config("OTP_SERVER", ""),
OTP_VERIFY_ENDPOINT=config("OTP_VERIFY_ENDPOINT", "verify/"),
OTP_SERVER=OTP_SERVER,
OTP_VERIFY_ENDPOINT=OTP_VERIFY_ENDPOINT,
),
data=data,
)
@ -50,7 +63,7 @@ def get_order_id():
if order_id_kv is not None:
order_id = int(order_id_kv.value) + 1
else:
order_id = config("INIT_ORDER_ID")
order_id = INIT_ORDER_ID
client.put("/v1/last_order_id", str(order_id))
return "OR-{}".format(order_id)
@ -97,7 +110,6 @@ class AddProduct(Resource):
def post():
data = request.json
logging.debug("Got data: {}".format(str(data)))
REALM_ALLOWED = config("REALM_ALLOWED", cast=Csv(str))
logging.debug("REALM_ALLOWED = {}".format(REALM_ALLOWED))
if data["realm"] not in REALM_ALLOWED:
logging.error(