How to use factory?
Cixo Develop editou esta página há 6 meses atrás

How to use API keys factory?

First, create simple factory.

We would create simple API keys factory with "test" prefix.

import cx_apikey as apikey

factory = apikey.apikey_factory("test")

Changing size of the API keys.

We would change size of the API keys to the 512 letters. That function return factory, and we could use it, or store it into variable

factory.set_size(512)

# It also return factory itself
new_factory = apikey.apikey_factory("test").set_size(512)

Generating new API keys.

We could generate new random API key. It would be apikey object. It could be converted to string, to store it in the database.

new_apikey = factory.generate()

# We could convert it to string
as_string = new_apikey.key
also_as_string = str(new_apikey)

Importing API keys from string with validation.

Other useable function is importing string to API key object. It checks also that given API key is valid, that mean have good lenght and prefix.

from_string = factory.load(as_string)

# When API key is not valid that would raise apikey_syntax_error
try:
    from_string = factory.load(as_string)
except apikey.apikey_syntax_error as error:
    print("It is not valid API key")

Geting API keys validator.

We could also get API key validator to validating API keys without importing.

validator = factory.get_validator()