DataHub Utils

Performs an adviser DataHub API search.

Parameters:
Returns:

A subset of fields for each Adviser found

Return type:

list

Performs a Company name search using DataHub API.

Parameters:
Returns:

A subset of fields for each company found

Return type:

list

app.enquiries.common.datahub_utils.dh_contact_create(request, access_token, enquirer, company_id, primary=False)

Create a DataHub contact and associate it with the given company.

Parameters:
Returns:

A (response_json_dict, None) or (None, error_json_dict) tuple

Return type:

tuple

app.enquiries.common.datahub_utils.dh_enquiry_readiness(request, access_token, enquiry)

Check whether the given enquiry is ready to be submitted to DataHub

Criteria is that the company should exist in DataHub, client relationship manager is a valid user, given user is available etc.

Parameters:
Returns:

JSON-parsed DataHub API response

Return type:

dict

app.enquiries.common.datahub_utils.dh_get_company_contact_list(request, access_token, company_id)

Performs a contact search for a specific company using DataHub API.

Parameters:
Returns:

A subset of fields for each company found

Return type:

list

app.enquiries.common.datahub_utils.dh_get_matching_company_contact(first_name, last_name, email, company_contacts)

Performs check identifying whether an enquirer exists in their company’s contact list in DataHub API.

Parameters:
  • first_name (str) –

  • last_name (str) –

  • email (str) –

  • company_contacts (list) –

Returns:

The first matching contact if any exist

Return type:

dict or None

app.enquiries.common.datahub_utils.dh_get_matching_company_contact_by_email(email, company_contacts)

Performs check identifying whether an enquirer’s email exists in their company’s contact list in DataHub API.

Parameters:
  • email (str) –

  • company_contacts (list) –

Returns:

The first matching contact if any exist

Return type:

dict or None

app.enquiries.common.datahub_utils.dh_get_user_details(request, access_token)

Gets the currently logged in user details

Parameters:
app.enquiries.common.datahub_utils.dh_investment_create(request, enquiry)

Creates a DataHub investment using the data from the given app.enquiries.models.Enquiry.

Investment is only created if the company corresponding to the enquiry exists in DataHub otherwise error is returned. Enquirer details are added to the list of contacts for this company. If this is the only contact then it will be made primary.

Parameters:
Returns:

The parsed response JSON

Return type:

dict

app.enquiries.common.datahub_utils.dh_prepare_contact(request, access_token, enquirer, company_id)

Prepares contact for the Data Hub request based on enquirer details. Prompts adding a new company contact to Data Hub if needed.

Parameters:
Returns:

A (uuid, None) or (None, error_json_dict) tuple

Return type:

tuple

app.enquiries.common.datahub_utils.dh_prepare_payload(enquiry, company_id, contact_id, adviser_id, client_relationship_manager_id)

Prepares the payload for investment create request

Parameters:
Returns:

A (payload, 'primary_sector') or (payload, None), where payload is a dict.

app.enquiries.common.datahub_utils.dh_request(request, access_token, method, url, payload=None, request_headers=None, params=None, timeout=15)

Makes a DataHub API request.

Parameters:
  • request (django.http.HttpRequest) – A Django request

  • access_token (str) – A valid OAuth 2.0 access_token

  • method (str) – HTTP method

  • url (str) – A full DataHub API URL

  • payload (A JSON serializable value, optional) – The request payload

  • request_headers (dict, optional) – Request headers

  • params (dict, optional) – GET request querystring params

  • timeout (int, optional) – A timeout after which the function throws an error

Returns:

A requests.Response instance

app.enquiries.common.datahub_utils.fetch_metadata(name)

Fetches DataHub API metadata by name. The function is memoized for one hour in Django’s cache.

Parameters:

name (str) – The trailing part of the DataHub API metadata endpoint e.g. https://api.dev.datahub.uktrade.digital/v4/metadata/<name>.

Returns:

The parsed metadata as a list of dictionaries.

app.enquiries.common.datahub_utils.resolve_metadata_id(title, metadata)

Resolves an ID of the specified metadata by its title.

Parameters:
  • title (str) – Metadata title

  • metadata (TypedDict('Metadata', {'name': str, 'id': str})) – DataHub metadata list

Returns:

DataHub uuid for the given title if found, else None