Package: barkus-func-toolkit
Barkus Organization Python Cloud Functions ToolkitOverviewThis public Python package provides a set of utilities to streamline the development and management of Cloud Functions within the Barkus Organization.These utilities cover various aspects of Cloud Function development, including parsing cloud events, logging, error handling, Pub/Sub event handling, managing database clients, and testing.InstallationTo use this package, you need to have Python installed. You can install the package using pip:pipenvinstallbarkus-func-toolkitAdditional installations:pipenvinstallbarkus-func-toolkit[firestore]pipenvinstallbarkus-func-toolkit[bigquery]pipenvinstallbarkus-func-toolkit[pubsub]API1. Main Decorators1.1. decorators.main.httpFor HTTP-triggered Functions OnlyWraps the function withfunctions_framework.httpdecorator, as well as intercepts any errors and treats them into a standardized format.Example:importbarkus.functions.toolkitasbft@bft.decorators.main.http()defmain(request):# Your HTTP-triggered function logic here...1.2. decorators.main.cloud_eventFor Cloud Event-triggered Functions OnlyWraps the function withfunctions_framework.cloud_eventdecorator, as well as intercepts any errors and treats them into a standardized format.Example:importbarkus.functions.toolkitasbft@bft.decorators.main.cloud_event()defmain(request):# Your Cloud Event-triggered function logic here...2. Request ParsingThere are two functions defined, one for HTTP and one for CloudEvents. They both behave the exact same way.They follow the signature:Example:defparse(request,Model,**kwargs):...Where:Request is either the cloud_event or http requestModel is thedataclasses.dataclassorpydantic.BaseModelModel used to parse/validate the classFor dataclasses, we usedacite.from_dictto instantiate nested data, so whatever named arguments defined after will be used as dacite.from_dict's Config2.1. parse_request.http -HTTP Request ParsingFor HTTP-triggered Functions OnlyParses the request's content, assuming the request is of type HTTPExample:importbarkus.functions.toolkitasbftimportpydanticclassRequestData(pydantic.BaseModel):message:strbatata:int@bft.decorators.main.http()defmain(request):data:RequestData=bft.parse_request.http(request,RequestData)2.2. parse_request.cloud_event -Cloud Event Request ParsingFor Cloud Event-triggered Functions OnlyExample:importbarkus.functions.toolkitasbftfromdataclassesimportdataclass@dataclassclassRequestData:message:strbatata:int@bft.decorators.main.cloud_event()defperform_cloud_event(request):data:RequestData=bft.parse_request.cloud_event(request,RequestData)@bft.decorators.main.cloud_event()defperform_cloud_event_without_validation(request):data:RequestData=bft.parse_request.cloud_event(request,RequestData,type_check=False)3. Logging DecoratorWraps the function with logging.
Information logged:Function was calledFunction finished without errorsFunction raised an exceptionExample:importbarkus.functions.toolkitasbft@bft.decorators.log(0,name="alternate name")defsome_function(whatever,something):# Your function logic here...@bft.decorators.log(1,name="alternate name")defsome_nested_function(whatever,something):# Your nested function logic here...4. AuthorizationFor HTTP-triggered Functions OnlyPerform authentication for an HTTP Request.Authenticator.AUTH_TOKENis the token every request's token should check against.Authenticator.header_keyis the key of the header, the request's token is located | default = 'Api-Token'This function will:Raisebft.errors.UnauthenticatedErrorif the token is missingRaisebft.errors.UnauthorizedErrorif the token is invalidExample:frombarkus.functions.toolkit.authenticatorimportAuthenticatorimportosauth=Authenticator(AUTH_TOKEN=os.getenv("AUTH_TOKEN"),header_key="Api-Token")# orauth=Authenticator(AUTH_TOKEN=os.getenv("AUTH_TOKEN"))# orauth=Authenticator(AUTH_TOKEN=os.getenv("AUTH_TOKEN"),header_key="Authorization")defperform(request):auth.authenticate(request)# Will raise AuthenticationError if unauthenticated5. Publishing to a TopicExample:frombarkus.functions.toolkit.publish_to_topicimportpublish_to_topicdefperform(request):publish_to_topic("project","topic",{})6. Google Cloud Client ManagerReturns an instance of google_cloud [service] 's Client.
Each requires an additional instalation.To use firestore client:pipenvinstallbarkus.functions.toolkit[firestore]To use bigquery client:pipenvinstallbarkus.functions.toolkit[bigquery]To use pubsub client:pipenvinstallbarkus.functions.toolkit[pubsub]Example:importbarkus.functions.toolkitasbftdeffunction_that_uses_firestore():# Requires you to install [firestore] dependenciesbft.CloudClientManager.firestore.batchdeffunction_that_uses_bigquery():# Requires you to install [bigquery] dependenciesbft.CloudClientManager.bigquerydeffunction_that_uses_pubsub():# Requires you to install [pubsub] dependenciesbft.CloudClientManager.pubsub.publishError HandlingThe package defines classes for known errors, providing a standardized way to handle and respond to errors within your Google Cloud Functions.Example:importbarkus.functions.toolkitasbftbft.errors.UnauthenticatedErrorPublishing New VersionTo publish a new version you have to follow these steps:1. You must make sure you have the dev-dependencies installedpipenvinstall-d2. You must configure~/.pypircfile with credentials:It should look something like this:[distutils]
index-servers =
    pypi
    testpypi

[pypi]
username = __token__
password = pypi-*********

[testpypi]
username = __token__
password = pypi-*********Note: thetokenis actually correct, it's not an example's placeholder3. Update the package's version:At setup.py, change theversionto the desired value4. Generate the build:pipenvrunbuild5. Publish the package:pipenvrunpublishLicenseThis toolkit is proprietary software developed by Barkus Organization. Unauthorized use, reproduction, or distribution is prohibited.