import click from services.requester import make_request from services.logger import get_logger import sys logger = get_logger(loggername='spanglish') api_name = 'spanglish' # a dict to show all the options and their api functions. funcs = { 1: ('get all categories', 'get_categories'), 2: ('get a category', 'get_category'), 3: ('add a category', 'add_category'), 4: ('update a category', 'update_category'), 5: ('delete category', 'delete_category'), } @click.command() @click.option('--function_id', default=1, type=click.IntRange(1, len(funcs)), prompt='function option: ') def run_function(function_id): """ based on the function number chosen by the user, it will execute the function """ category = sys.modules[__name__] # to be used by the getattr global funcs funcName = funcs[function_id][ 1] # get the function name from the global dictionary funcs
from configs import settings as conf, apis import requests import redis from services.logger import get_logger from datetime import datetime, timedelta logger = get_logger(loggername='tokens') cache = redis.Redis(host=conf.CACHE['REDIS']['URL'], password=conf.CACHE['REDIS']['PASSWORD']) url = conf.API_URL def get_token(user='******'): """ returns the token an active to to be used in the request header. It checks first for the token in the cache. If it's available, it will return it. If not not, it will call the token api to and get the access token and refreshed token. Both will be stored in the cache. """ token = cache.get('token') # if the token is found in the cache, check the last_verified. if it's less then 10 min, return # the token. else verify it. if token: # token in the cache. check verify timestamp token = token.decode('ASCII') logger.debug("token found in the cache") # if the token has been verified less than 10 minutes ago, return it if get_token_cached_times_diff(cached_key='last_verified',
import click from services.logger import get_logger from services.requester import make_request import sys logger = get_logger(loggername='currency') api_name = 'currency' funcs = { 1: ('currency code', 'get_code'), 2: ('currency amount converter', 'convert_currency'), 3: ('currency current rate', 'get_rate'), 4: ('currency rate for all currencies', 'get_all_rates'), } @click.command() @click.option('--function_id', default=1, type=click.IntRange(1, len(funcs)), prompt='function option: ') def run_function(function_id): """ based on the function number chosen by the user, it will execute the function """ currency = sys.modules[__name__] # to be used by the getattr global funcs funcName = funcs[function_id][ 1] # get the function name from the global dictionary funcs getattr(currency, funcName)() #execute the chosen function
from configs import settings as conf, apis import requests from services.logger import get_logger from datetime import datetime, timedelta from services.tokens import get_token logger = get_logger(loggername='requester') def make_request(*args, action='str', api=dict, **kwargs): """ makes the http request and returns a response. It takes the following args: 1- *args: this is an optional args tuple used for the get parameters request. values will be split by a / at the end of the api. all values have to be a string. 2- action: this is mandatory str type value that determines the action used in the request. possible actions are GET, POST, PUT and DELETE. The value is case insensative. 3- api: this is a mandatory dict type value that determines the api and it's path to be used. 4- **kwargs: a key value pair of values used for the params arg in the request. """ api = apis.API[api[0]][api[1]] # get the api url from the config logger.debug("api: %s", api) api_request = conf.API_URL + api # append the api path to the url token = 'Bearer '.__add__( get_token()) # get the token from the get_token method. # create a header object which can hold other values than the token header = { "Authorization": token, }