Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM python:latest
COPY . .
ENV DB_TYPE=postgresql
ENV DB_USERNAME=postgres
ENV DB_PASSWORD=postgres
ENV DB_ENDPOINT=postgres:5432
ENV DB_TEST_NAME=bit_schema_test
ENV POSTGRES_HOST=postgres
ENV POSTGRES_PORT=5432
RUN pip install --no-cache-dir -r requirements.txt
CMD python run.py
60 changes: 34 additions & 26 deletions app/api/request_api_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
from app.utils.decorator_utils import http_response_namedtuple_converter


BASE_MS_API_URL = "http://127.0.0.1:4000"
BASE_MS_API_URL = "http://MS:5000"
AUTH_COOKIE = cookies.SimpleCookie()


def post_request(request_string, data):
request_url = f"{BASE_MS_API_URL}{request_string}"
request_url = f"{BASE_MS_API_URL}{request_string}"
try:
response = requests.post(
request_url, json=data, headers={"Accept": "application/json"}
Expand All @@ -37,55 +37,62 @@ def post_request(request_string, data):
access_expiry_cookie = response_message.get("access_expiry")
AUTH_COOKIE["Authorization"] = f"Bearer {access_token_cookie}"
AUTH_COOKIE["Authorization"]["expires"] = access_expiry_cookie
set_user = http_response_checker(get_user(AUTH_COOKIE["Authorization"].value))
set_user = http_response_checker(
get_user(AUTH_COOKIE["Authorization"].value)
)
if set_user.status_code != 200:
response_message = set_user.message
response_code = set_user.status_code
else:
response_message = {"access_token": response_message.get("access_token"), "access_expiry": response_message.get("access_expiry")}
response_message = {
"access_token": response_message.get("access_token"),
"access_expiry": response_message.get("access_expiry"),
}
logging.fatal(f"{response_message}")
return response_message, response_code


def get_user(token):
request_url = "/user"
request_url = "/user"
return get_request(request_url, token, params=None)


def get_headers(request_string, params):
if request_string == "/user":
return {"Authorization": AUTH_COOKIE["Authorization"].value, "Accept": "application/json"}
return {
"Authorization": AUTH_COOKIE["Authorization"].value,
"Accept": "application/json",
}
if request_string == "/users/verified":
return {
"Authorization": AUTH_COOKIE["Authorization"].value,
"Authorization": AUTH_COOKIE["Authorization"].value,
"search": params["search"],
"page": str(params["page"]),
"per_page": str(params["per_page"]),
"Accept": "application/json"
"Accept": "application/json",
}
if request_string == "/organizations":
return {
"Authorization": AUTH_COOKIE["Authorization"].value,
"Authorization": AUTH_COOKIE["Authorization"].value,
"name": params["name"],
"page": str(params["page"]),
"per_page": str(params["per_page"]),
"Accept": "application/json"
"Accept": "application/json",
}
return {
"Authorization": AUTH_COOKIE["Authorization"].value,
"Accept": "application/json"
"Accept": "application/json",
}


def get_request(request_string, token, params):
request_url = f"{BASE_MS_API_URL}{request_string}"
request_url = f"{BASE_MS_API_URL}{request_string}"
is_wrong_token = validate_token(token)

if not is_wrong_token:
try:
try:
response = requests.get(
request_url,
headers=get_headers(request_string, params)
request_url, headers=get_headers(request_string, params)
)
response.raise_for_status()
response_message = response.json()
Expand All @@ -110,15 +117,18 @@ def get_request(request_string, token, params):


def put_request(request_string, token, data):
request_url = f"{BASE_MS_API_URL}{request_string}"
request_url = f"{BASE_MS_API_URL}{request_string}"
is_wrong_token = validate_token(token)

if not is_wrong_token:
try:
try:
response = requests.put(
request_url,
json=data,
headers={"Authorization": AUTH_COOKIE["Authorization"].value, "Accept": "application/json"},
request_url,
json=data,
headers={
"Authorization": AUTH_COOKIE["Authorization"].value,
"Accept": "application/json",
},
)
response.raise_for_status()
response_message = response.json()
Expand Down Expand Up @@ -146,20 +156,20 @@ def validate_token(token):
if AUTH_COOKIE:
if token != AUTH_COOKIE["Authorization"].value:
return messages.TOKEN_IS_INVALID, HTTPStatus.UNAUTHORIZED
if datetime.utcnow().timestamp() > AUTH_COOKIE["Authorization"]["expires"]:
if datetime.utcnow().timestamp() > AUTH_COOKIE["Authorization"]["expires"]:
return messages.TOKEN_HAS_EXPIRED, HTTPStatus.UNAUTHORIZED


@http_response_namedtuple_converter
def http_response_checker(result):
# TO DO: REMOVE ALL IF CONDITIONS ONCE ALL BIT-MS HTTP ERROR ISSUES ON MS ARE FIXED
# TO DO: REMOVE ALL IF CONDITIONS ONCE ALL BIT-MS HTTP ERROR ISSUES ON MS ARE FIXED
# if result.status_code == HTTPStatus.OK:
# result = http_ok_status_checker(result)
# # if result.status_code == HTTPStatus.BAD_REQUEST:
# result = http_bad_request_status_checker(result)
# if result.status_code == HTTPStatus.NOT_FOUND:
# result = http_not_found_status_checker(result)
# if result.status_code == json.dumps(HTTPStatus.INTERNAL_SERVER_ERROR) and not AUTH_COOKIE:
# if result.status_code == json.dumps(HTTPStatus.INTERNAL_SERVER_ERROR) and not AUTH_COOKIE:
# # if not AUTH_COOKIE:
# return messages.TOKEN_IS_INVALID, HTTPStatus.UNAUTHORIZED
return result
Expand All @@ -184,5 +194,3 @@ def http_response_checker(result):
# # TO DO: REMOVE ONCE ISSUE#624 ON MS BACKEND IS FIXED
# if result.message == messages.WRONG_USERNAME_OR_PASSWORD:
# return result._replace(status_code = HTTPStatus.UNAUTHORIZED)


78 changes: 44 additions & 34 deletions config.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import os
from datetime import timedelta


def get_mock_email_config() -> bool:
MOCK_EMAIL = os.getenv("MOCK_EMAIL")

#if MOCK_EMAIL env variable is set
if MOCK_EMAIL:
# if MOCK_EMAIL env variable is set
if MOCK_EMAIL:
# MOCK_EMAIL is case insensitive
MOCK_EMAIL = MOCK_EMAIL.lower()
if MOCK_EMAIL=="true":

if MOCK_EMAIL == "true":
return True
elif MOCK_EMAIL=="false":
elif MOCK_EMAIL == "false":
return False
else:
else:
# if MOCK_EMAIL env variable is set a wrong value
raise ValueError(
"MOCK_EMAIL environment variable is optional if set, it has to be valued as either 'True' or 'False'"
Expand All @@ -22,11 +23,12 @@ def get_mock_email_config() -> bool:
# Default behaviour is to send the email if MOCK_EMAIL is not set
return False


class BaseConfig(object):
DEBUG = False
TESTING = False
SQLALCHEMY_TRACK_MODIFICATIONS = False
SQLALCHEMY_TRACK_MODIFICATIONS = False

# Flask JWT settings
JWT_ACCESS_TOKEN_EXPIRES = timedelta(weeks=1)
JWT_REFRESH_TOKEN_EXPIRES = timedelta(weeks=4)
Expand All @@ -36,7 +38,7 @@ class BaseConfig(object):
SECRET_KEY = os.getenv("SECRET_KEY", None)
BCRYPT_LOG_ROUNDS = 13
WTF_CSRF_ENABLED = True

# mail settings
MAIL_SERVER = os.getenv("MAIL_SERVER")
MAIL_PORT = 465
Expand All @@ -50,67 +52,74 @@ class BaseConfig(object):
# mail accounts
MAIL_DEFAULT_SENDER = os.getenv("MAIL_DEFAULT_SENDER")

DB_TYPE = os.getenv("DB_TYPE"),
DB_USERNAME = os.getenv("DB_USERNAME"),
DB_PASSWORD = os.getenv("DB_PASSWORD"),
DB_ENDPOINT = os.getenv("DB_ENDPOINT"),
DB_TYPE = (os.getenv("DB_TYPE"),)
DB_USERNAME = (os.getenv("DB_USERNAME"),)
DB_PASSWORD = (os.getenv("DB_PASSWORD"),)
DB_ENDPOINT = (os.getenv("DB_ENDPOINT"),)
DB_NAME = os.getenv("DB_NAME")
DB_TEST_NAME = os.getenv("DB_TEST_NAME")

@staticmethod
def build_db_uri(
db_type_arg = os.getenv("DB_TYPE"),
db_user_arg = os.getenv("DB_USERNAME"),
db_password_arg = os.getenv("DB_PASSWORD"),
db_endpoint_arg = os.getenv("DB_ENDPOINT"),
db_name_arg = os.getenv("DB_NAME"),
db_type_arg=os.getenv("DB_TYPE"),
db_user_arg=os.getenv("DB_USERNAME"),
db_password_arg=os.getenv("DB_PASSWORD"),
db_endpoint_arg=os.getenv("DB_ENDPOINT"),
db_name_arg=os.getenv("DB_NAME"),
):
return f"{db_type_arg}://{db_user_arg}:{db_password_arg}@{db_endpoint_arg}/{db_name_arg}"

@staticmethod
def build_db_test_uri(
db_type_arg = os.getenv("DB_TYPE"),
db_user_arg = os.getenv("DB_USERNAME"),
db_password_arg = os.getenv("DB_PASSWORD"),
db_endpoint_arg = os.getenv("DB_ENDPOINT"),
db_name_arg = os.getenv("DB_TEST_NAME"),
db_type_arg=os.getenv("DB_TYPE"),
db_user_arg=os.getenv("DB_USERNAME"),
db_password_arg=os.getenv("DB_PASSWORD"),
db_endpoint_arg=os.getenv("DB_ENDPOINT"),
db_name_arg=os.getenv("DB_TEST_NAME"),
):
return f"{db_type_arg}://{db_user_arg}:{db_password_arg}@{db_endpoint_arg}/{db_name_arg}"


class LocalConfig(BaseConfig):
"""Local configuration."""

DEBUG = True

# Using a local postgre database
SQLALCHEMY_DATABASE_URI = "postgresql:///bit_schema"

# SQLALCHEMY_DATABASE_URI = BaseConfig.build_db_uri()



class DevelopmentConfig(BaseConfig):
DEBUG = True

SQLALCHEMY_DATABASE_URI = BaseConfig.build_db_uri()


# SQLALCHEMY_DATABASE_URI = BaseConfig.build_db_uri()
SQLALCHEMY_DATABASE_URI = "postgresql://postgres:postgres@postgres:5432/bit_schema"


class TestingConfig(BaseConfig):
TESTING = True
MOCK_EMAIL = True

# Using a local postgre database
SQLALCHEMY_DATABASE_URI = "postgresql:///bit_schema_test"

SQLALCHEMY_DATABASE_URI = (
"postgresql://postgre:postgres@postgres:5432/bit_schema_test"
)

# SQLALCHEMY_DATABASE_URI = BaseConfig.build_db_test_uri()


class StagingConfig(BaseConfig):
"""Staging configuration."""

DEBUG = True
SQLALCHEMY_DATABASE_URI = BaseConfig.build_db_uri()


class ProductionConfig(BaseConfig):
SQLALCHEMY_DATABASE_URI = BaseConfig.build_db_uri()


def get_env_config() -> str:
flask_config_name = os.getenv("FLASK_ENVIRONMENT_CONFIG", "dev")
Expand All @@ -120,6 +129,7 @@ def get_env_config() -> str:
)
return CONFIGURATION_MAPPER[flask_config_name]


CONFIGURATION_MAPPER = {
"dev": "config.DevelopmentConfig",
"prod": "config.ProductionConfig",
Expand Down
37 changes: 37 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

services:
postgres:
image: postgres
environment:
POSTGRES_PASSWORD: postgres
POSTGRES_DB: bit_schema
#options:
#--health-cmd pg_isready
#--health-interval 10s
#--health-timeout 5s
#--health-retries 5
ports:
- 5432:5432
networks:
- custom_network

bit:
container_name: BIT
build: .
ports:
- 5000:5000
networks:
- custom_network

ms:
container_name: MS
build: ../mentorship-backend
ports:
- 4000:4000
networks:
- custom_network

networks:
custom_network:
name: custom_network

Loading