"""Interface to the ows-account microservice.""" from http.client import HTTPException from flask import g from oto import response from owsrequest import request from project_manager import constant def is_distributor(vendor_id): """Check whether account is a distributor. Args: vendor_id (int): vendor unique identifier Returns: Response with status_code of 200 if distributor Not Found Response with status_code of 404 if not distributor Fatal Response if call to ows-account failed """ r = request.head('ows-account', '/{vendor}/distributor'.format( vendor=vendor_id)) if r.status_code == 200: return response.Response(message='account is a distributor') elif r.status_code == 204: return response.Response( message='account is not a distributor', status=204) else: message = 'ows-account returned unexpected status_code of {}'.\ format(r.status_code) g.log.error(message) return response.create_fatal_response(message) def get_vendor_id_for_subaccount_id(subaccount_id): """Get the vendor id based on subaccount id. Args: subaccount_id (int) Returns: vendor_id (int) if found None if not found """ r = request.get('ows-account', '/subaccount/{subaccount}'.format( subaccount=subaccount_id)) if r.status_code == 200: return r.json()[constant.field_const.VENDOR_ID] def is_subaccount_for_vendor(vendor_id, subaccount_id): """Determine whether subaccount belongs to this vendor. Args: vendor_id (int) subaccount_id (int) Returns: Response: response object with message and status 200 if subaccount belongs to vendor 403 if subaccount does not belong to vendor Fatal response if ows-account returns unexpected status_code """ r = request.head('ows-account', '/{vendor}/subaccount/{subaccount}'.format( vendor=vendor_id, subaccount=subaccount_id)) if r.status_code == 200: return response.Response(message='subaccount belongs to vendor') elif r.status_code == 403: return response.create_error_response( code=constant.error_const.AUTHORIZATION_ERROR, message='subaccount does not belong to vendor', status=403) else: message = 'ows-account returned unexpected status_code of {}'.\ format(r.status_code) g.log.error(message) return response.create_fatal_response(message) def get_subaccount_for_subaccount_id(subaccount_id): """Get a subaccount object for given subaccount_id. Args: subaccount_id (int): uid of subaccount Returns: dict: subaccount dictionary if found None: if not found """ response = request.get( 'ows-account', '/subaccount/{subaccount}'.format( subaccount=subaccount_id)) if response.status_code == 200: return response.json() def get_vendor_company_brand(vendor_id): """Get company brand for a vendor. Args: vendor_id (int): id of a vendor Returns: string: company brand name """ resp = request.get( 'ows-account', '/vendor/brand/{vendor_id}'.format(vendor_id=vendor_id) ) if resp.status_code == 200: return resp.text message = 'ows-account returned unexpected status_code of {}'.\ format(resp.status_code) g.log.error(message) return None def get_vendor(vendor_id): """Get vendor data.""" resp = request.get('ows-account', f'/vendor/{vendor_id}') if resp.status_code == 200: return resp.json() g.log.error( f'ows-account returned unexpected status_code of {resp.status_code}') raise HTTPException(resp.text)