-
Notifications
You must be signed in to change notification settings - Fork 122
feat: Add Identity Insights API #336
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
Open
alnacle
wants to merge
5
commits into
Vonage:main
Choose a base branch
from
alnacle:identity-insights
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| resource(name='pyproject', source='pyproject.toml') | ||
| file(name='readme', source='README.md') | ||
|
|
||
| files(sources=['tests/data/*']) | ||
|
|
||
| python_distribution( | ||
| name='vonage-identity-insights', | ||
| dependencies=[ | ||
| ':pyproject', | ||
| ':readme', | ||
| 'identity_insights/src/vonage_identity_insights', | ||
| ], | ||
| provides=python_artifact(), | ||
| generate_setup=False, | ||
| repositories=['@pypi'], | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| # 1.0.0 | ||
| - Initial upload |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| # Vonage Identity Insights Package | ||
|
|
||
| This package contains the code to use the [Vonage Identity Insights API](https://developer.vonage.com/en/identity-insights/overview) in Python. The API provides real-time access to a broad range of attributes related to the carrier, subscriber, or device associated with a phone number. To use it you will need a Vonage account. Sign up [for free at vonage.com][signup]. | ||
|
|
||
| ## Usage | ||
|
|
||
| It is recommended to use this as part of the main `vonage` package. The examples below assume you've created an instance of the `vonage.Vonage` class called `vonage_client`. | ||
|
|
||
| ### Make a Standard Identity Insights Request | ||
|
|
||
| ```python | ||
| from vonage import Vonage, Auth, HttpClientOptions | ||
| from vonage_identity_insights import ( | ||
| IdentityInsightsRequest, | ||
| InsightsRequest, | ||
| EmptyInsight, | ||
| SimSwapInsight, | ||
| ) | ||
|
|
||
| options = HttpClientOptions(api_host="api-eu.vonage.com", timeout=30) | ||
|
|
||
| client = Vonage(auth=auth, http_client_options=options) | ||
|
|
||
| request = IdentityInsightsRequest( | ||
| phone_number="1234567890", | ||
| purpose="FraudPreventionAndDetection", | ||
| insights=InsightsRequest( | ||
| format=EmptyInsight(), sim_swap=SimSwapInsight(period=240) | ||
| ), | ||
| ) | ||
|
|
||
| response = client.identity_insights.get_insights(request) | ||
|
|
||
| ``` | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| [project] | ||
| name = 'vonage-identity-insights' | ||
| dynamic = ["version"] | ||
| description = 'Vonage Identity Insights package' | ||
| readme = "README.md" | ||
| authors = [{ name = "Vonage", email = "[email protected]" }] | ||
| requires-python = ">=3.9" | ||
| dependencies = [ | ||
| "vonage-http-client>=1.5.0", | ||
| "vonage-utils>=1.1.4", | ||
| "pydantic>=2.9.2", | ||
| ] | ||
| classifiers = [ | ||
| "Programming Language :: Python", | ||
| "Programming Language :: Python :: 3", | ||
| "Programming Language :: Python :: 3.9", | ||
| "Programming Language :: Python :: 3.10", | ||
| "Programming Language :: Python :: 3.11", | ||
| "Programming Language :: Python :: 3.12", | ||
| "Programming Language :: Python :: 3.13", | ||
| "License :: OSI Approved :: Apache Software License", | ||
| ] | ||
|
|
||
| [project.urls] | ||
| homepage = "https://github.com/Vonage/vonage-python-sdk" | ||
|
|
||
| [tool.setuptools.dynamic] | ||
| version = { attr = "vonage_identity_insights._version.__version__" } | ||
|
|
||
| [build-system] | ||
| requires = ["setuptools>=61.0", "wheel"] | ||
| build-backend = "setuptools.build_meta" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| python_sources() |
30 changes: 30 additions & 0 deletions
30
identity_insights/src/vonage_identity_insights/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| from . import errors | ||
| from .identity_insights import IdentityInsights | ||
| from .requests import ( | ||
| EmptyInsight, | ||
| IdentityInsightsRequest, | ||
| InsightsRequest, | ||
| Location, | ||
| LocationCenter, | ||
| LocationVerificationInsight, | ||
| SimSwapInsight, | ||
| SubscriberMatchInsight, | ||
| ) | ||
| from .responses import IdentityInsightsResponse, InsightStatus | ||
|
|
||
| __all__ = [ | ||
| "IdentityInsights", | ||
| # Requests | ||
| "IdentityInsightsRequest", | ||
| "InsightsRequest", | ||
| "EmptyInsight", | ||
| "SimSwapInsight", | ||
| "SubscriberMatchInsight", | ||
| "LocationVerificationInsight", | ||
| "Location", | ||
| "LocationCenter", | ||
| # Responses | ||
| "IdentityInsightsResponse", | ||
| "InsightStatus", | ||
| "errors", | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| __version__ = "1.0.0" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| from vonage_utils.errors import VonageError | ||
|
|
||
|
|
||
| class IdentityInsightsError(VonageError): | ||
| """Indicates an error when using the Vonage Identity Insights API.""" | ||
|
|
||
|
|
||
| class EmptyInsightsRequestException(VonageError): | ||
| """At least one insight must be provided.""" |
88 changes: 88 additions & 0 deletions
88
identity_insights/src/vonage_identity_insights/identity_insights.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| from logging import getLogger | ||
|
|
||
| from pydantic import validate_call | ||
| from vonage_http_client.http_client import HttpClient | ||
|
|
||
| from .errors import EmptyInsightsRequestException, IdentityInsightsError | ||
| from .requests import IdentityInsightsRequest | ||
| from .responses import IdentityInsightsResponse | ||
|
|
||
| logger = getLogger("vonage_identity_insights") | ||
|
|
||
|
|
||
| class IdentityInsights: | ||
| """Calls Vonage's Identity Insights API.""" | ||
|
|
||
| def __init__(self, http_client: HttpClient) -> None: | ||
| """Initialize the IdentityInsights client. | ||
|
|
||
| Args: | ||
| http_client (HttpClient): Configured HTTP client used to make | ||
| authenticated requests to the Vonage API. | ||
| """ | ||
| self._http_client = http_client | ||
| self._auth_type = "jwt" | ||
|
|
||
| @property | ||
| def http_client(self) -> HttpClient: | ||
| """The HTTP client used to make requests to the Vonage Indentity Insights API. | ||
|
|
||
| Returns: | ||
| HttpClient: The HTTP client used to make requests to the Identity Insights API. | ||
| """ | ||
| return self._http_client | ||
|
|
||
| @validate_call | ||
| def get_insights( | ||
| self, insights_request: IdentityInsightsRequest | ||
| ) -> IdentityInsightsResponse: | ||
| """Retrieve identity insights for a phone number. | ||
|
|
||
| Sends an aggregated request to the Identity Insights API and returns | ||
| the results for each requested insight. | ||
|
|
||
| Args: | ||
| insights_request (IdentityInsightsRequest): The request object | ||
| containing the phone number and the set of identity insights | ||
| to retrieve. | ||
|
|
||
| Returns: | ||
| IdentityInsightsResponse: The response object containing the results | ||
| and status of each requested insight. | ||
|
|
||
| Raises: | ||
| IdentityInsightsError: If the API returns an error response in | ||
| `application/problem+json` format. | ||
| """ | ||
| payload = insights_request.model_dump(exclude_none=True) | ||
|
|
||
| insights = payload.get("insights") | ||
| if not insights or not isinstance(insights, dict): | ||
| raise EmptyInsightsRequestException() | ||
|
|
||
| response = self._http_client.post( | ||
| self._http_client.api_host, | ||
| "/v0.1/identity-insights", | ||
| payload, | ||
| auth_type=self._auth_type, | ||
| ) | ||
| self._check_for_error(response) | ||
|
|
||
| return IdentityInsightsResponse(**response) | ||
|
|
||
| def _check_for_error(self, response: dict) -> None: | ||
| """Check whether the API response represents an error. | ||
|
|
||
| The Identity Insights API returns errors using the | ||
| `application/problem+json` format. If such an error is detected, this | ||
| method raises an IdentityInsightsError. | ||
|
|
||
| Args: | ||
| response (dict): Raw response returned by the HTTP client. | ||
|
|
||
| Raises: | ||
| IdentityInsightsError: If the response contains an error payload. | ||
| """ | ||
| if "title" in response and "detail" in response: | ||
| error_message = f"Error with the following details: {response}" | ||
| raise IdentityInsightsError(error_message) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.