Skip to content

024 brand creating #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
165 changes: 165 additions & 0 deletions app/eg024_brand_creating.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
"""Example 024: Creating a brand"""

import json
from os import path

from docusign_esign import ApiClient, AccountsApi, Brand
from docusign_esign.client.api_exception import ApiException
from flask import render_template, url_for, redirect, session, flash, request

from app import app, ds_config, views

eg = 'eg024' # reference and url for this example

LANGUAGES = {
"Arabic": "ar",
"Armenian": "hy",
"Bahasa Indonesia": "id",
"Bahasa Malay": "ms",
"Bulgarian": "bg",
"Chinese Simplified": "zh_CN",
"Chinese Traditional": "zh_TW",
"Croatian": "hr",
"Czech": "cs",
"Danish": "da",
"Dutch": "nl",
"English UK": "en_GB",
"English US": "en",
"Estonian": "et",
"Farsi": "fa",
"Finnish": "fi",
"French": "fr",
"French Canada": "fr_CA",
"German": "de",
"Greek": "el",
"Hebrew": "he",
"Hindi": "hi",
"Hungarian": "hu",
"Italian": "it",
"Japanese": "ja",
"Korean": "ko",
"Latvian": "lv",
"Lithuanian": "lt",
"Norwegian": "no",
"Polish": "pl",
"Portuguese": "pt",
"Portuguese Brasil": "pt_BR",
"Romanian": "ro",
"Russian": "ru",
"Serbian": "sr",
"Slovak": "sk",
"Slovenian": "sl",
"Spanish": "es",
"Spanish Latin America": "es_MX",
"Swedish": "sv",
"Thai": "th",
"Turkish": "tr",
"Ukrainian": "uk",
"Vietnamese": "vi"
}


def controller():
"""Controller router using the HTTP method"""
if request.method == "GET":
return get_controller()
elif request.method == "POST":
return create_controller()
else:
return render_template("404.html"), 404


def create_controller():
"""
1. Check the token
2. Call the worker method
3. Render response
"""
minimum_buffer_min = 3
if views.ds_token_ok(minimum_buffer_min):
# Step 1: Obtain your OAuth token
args = {
'account_id': '213', # represent your {ACCOUNT_ID}
'base_path': session['ds_base_path'],
'access_token': session['ds_access_token'], # represent your {ACCESS_TOKEN}
'brand_name': request.form.get('brand_name'),
'default_language': request.form.get('default_language')
}
try:
# Step 2: Call the worker method to create a new brand
response = worker(args)
brand_id = response.brands[0].brand_id
app.logger.info(f"Brand has been created. Brand id {brand_id}")

# Step 3: Render response
return render_template('example_done.html',
title='Brand creating',
h1='Brand creating',
message=f"""The brand has been created and sent!<br/>
Brand ID {brand_id}."""
)

except ApiException as err:
error_body_json = err and hasattr(err, 'body') and err.body
# We can pull the DocuSign error code and message from the response body
error_body = json.loads(error_body_json)
error_code = error_body and 'errorCode' in error_body and error_body['errorCode']
error_message = error_body and "message" in error_body and error_body["message"]
# In production, you may want to provide customized error messages and
# remediation advice to the user
return render_template('error.html',
err=err,
error_code=error_code,
error_message=error_message
)

else:
flash("Sorry, you need to re-authenticate.")
# We could store the parameters of the requested operation so it could be restarted
# automatically. But since it should be rare to have a token issue here,
# we'll make the user re-enter the form data after authentication
session["eg"] = url_for(eg)
return redirect(url_for("ds_must_authenticate"))


def worker(args):
"""
1. Create api client with headers
2. Create a brand object
3. Post the brand using SDK
"""

# Step 1: create API client
api_client = ApiClient()
api_client.host = args['base_path']
api_client.set_default_header(header_name="Authorization", header_value=f"Bearer {args['access_token']}")

# Step 2: Create a brand object
brand = Brand(
brand_name=args['brand_name'],
default_brand_language=args['default_language'],
)

# Step 3: a) Call the eSignature SDK
# b) Display the JSON response
account_api = AccountsApi(api_client)
response = account_api.create_brand(account_id=args['account_id'], brand=brand)
return response


def get_controller():
"""Responds with the form for the example"""

if views.ds_token_ok():
return render_template("eg024_brand_creating.html",
title="Brand creating",
source_file=path.basename(__file__),
source_url=ds_config.DS_CONFIG["github_example_url"] + path.basename(__file__),
documentation=ds_config.DS_CONFIG["documentation"] + eg,
show_doc=ds_config.DS_CONFIG["documentation"],
languages=LANGUAGES
)
else:
# Save the current operation so it will be resumed after authentication
session["eg"] = url_for(eg)
return redirect(url_for("ds_must_authenticate"))
46 changes: 46 additions & 0 deletions app/templates/eg024_brand_creating.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<!-- extend base layout --> {% extends "base.html" %} {% block content %}

<h4>24. Create a new brand</h4>
<p>
The brand includes a Brand Name <a href="https://developers.docusign.com/esign-rest-api/code-examples/brands-creating" target="_blank">Configure Brands</a>
</p>
<p>
This code example demonstrates how to create a brand.
</p>

{% if show_doc %}
<p><a target='_blank' href='{{ documentation | safe }}'>Documentation</a> about this example.</p>
{% endif %}

<p>
API method used:
<a href="https://developers.docusign.com/esign-rest-api/reference/Accounts/AccountBrands/create" target="_blank">AccountBrands::create</a>.
</p>

<p>
View source file <a target="_blank" href="{{ source_url | safe }}">{{ source_file }}</a> on GitHub.
</p>

<form class="eg" action="" method="post" data-busy="form">
<div class="form-group">
<label for="brand_name">Brand name</label>
<input type="text" class="form-control" id="brand_name" placeholder="New Brand" name="brand_name"
required>
</div>

<div class="form-group">
<label for="default_language">Default Brand Language</label>
<select id="default_language" name="default_language" class="form-control">
{% for key, value in languages.items() %}
<option value="{{ value }}">{{ key }}</option>
{% endfor %}
</select>
</div>

<div class="form-group">
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>

{% endblock %}
12 changes: 12 additions & 0 deletions app/templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,18 @@ <h4 id="example023">23. <a href="eg023">Send an envelope with ID Verification Au
<a target='_blank' href="https://developers.docusign.com/esign-rest-api/reference/Envelopes/Envelopes/create">Envelopes::create</a>.
</p>


<h2>Brands</h2>

<h4 id="example024">24. <a href="eg024">Create a new Brand</a></h4>
<p>
Creating a brand
</p>
<p>
API method used:
<a target='_blank' href="https://developers.docusign.com/esign-rest-api/reference/Accounts/AccountBrands/create">AccountBrands::create</a>.
</p>

</div>

<!-- anchor-js is only for the index page -->
Expand Down
8 changes: 7 additions & 1 deletion app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
eg016_set_tab_values, eg017_set_template_tab_values, \
eg018_envelope_custom_field_data, eg019_access_code_authentication, \
eg020_sms_authentication, eg021_phone_authentication, \
eg022_kba_authentication, eg023_idv_authentication
eg022_kba_authentication, eg023_idv_authentication, \
eg024_brand_creating


@app.route("/")
Expand Down Expand Up @@ -149,6 +150,11 @@ def eg023():
return eg023_idv_authentication.controller()


@app.route("/eg024", methods=["GET", "POST"])
def eg024():
return eg024_brand_creating.controller()


@app.route("/ds_return")
def ds_return():
event = request.args.get("event")
Expand Down