Skip to content

Commit

Permalink
writing migration guides/readmes
Browse files Browse the repository at this point in the history
  • Loading branch information
maxkahan committed Oct 18, 2024
1 parent cde28c2 commit 329d775
Show file tree
Hide file tree
Showing 6 changed files with 433 additions and 71 deletions.
89 changes: 84 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ This is the Python server SDK for Vonage's API. To use it you'll
need a Vonage account. Sign up [for free at vonage.com][signup].

- [Installation](#installation)
- [Migration Guides](#migration-guides)
- [Calling Vonage APIs](#calling-vonage-apis)
- [Usage](#usage)
- [Account API](#account-api)
- [Application API](#application-api)
- [HTTP Client](#http-client)
- [JWT Client](#jwt-client)
- [Messages API](#messages-api)
- [Network Number Verification API](#network-number-verification-api)
- [Network Sim Swap API](#network-sim-swap-api)
- [Number Insight API](#number-insight-api)
- [Numbers API](#numbers-api)
- [SMS API](#sms-api)
Expand All @@ -35,20 +38,36 @@ need a Vonage account. Sign up [for free at vonage.com][signup].

## Installation

To install the Python client library using pip:
To install the Python SDK package using pip:

pip install vonage
```bash
pip install vonage
```

To upgrade your installed client library using pip:

pip install vonage --upgrade
```bash
pip install vonage --upgrade
```

Alternatively, you can clone the repository via the command line:

git clone git@github.com:Vonage/vonage-python-sdk.git
```bash
git clone git@github.com:Vonage/vonage-python-sdk.git
```

or by opening it on GitHub desktop.

## Migration Guides

### V3 to V4

This version of the Vonage Python SDK (4.x+) works very differently to the previous SDK. See [the v3 -> v4 migration guide](V3_TO_V4_SDK_MIGRATION_GUIDE.md) for help migrating your application code using v3 of the SDK to the new structure.

### OpenTok to Vonage Video API

This SDK includes support for the [Vonage Video API](https://developer.vonage.com/en/video/overview). If you have an application that uses OpenTok for video and want to migrate (which is highly recommended!) then [A migration guide is available here](video/OPENTOK_TO_VONAGE_MIGRATION.md) which will help you to migrate your applications to use Vonage Video.

## Calling Vonage APIs

The Vonage Python SDK is a monorepo, with separate packages for each API. When you install the Python SDK, you'll see there's a top-level package, `vonage`, and then specialised packages for every API class.
Expand Down Expand Up @@ -365,7 +384,7 @@ Some message types have submodels with additional fields. In this case, import t
e.g.

```python
from vonage_messages import MessengerImage, MessengerOptions, MessengerResource
from vonage_messages.models import MessengerImage, MessengerOptions, MessengerResource

messenger = MessengerImage(
to='1234567890',
Expand Down Expand Up @@ -439,6 +458,66 @@ MessengerText, MessengerImage, MessengerAudio, MessengerVideo, MessengerFile
ViberText, ViberImage, ViberVideo, ViberFile
```

## Network Number Verification API

The Vonage Number Verification API uses Oauth2 authentication, which this SDK will also help you to do. Verifying a number has 3 stages:

1. Get an OIDC URL for use in your front-end application
2. Use this URL in your own application to get an authorization code
3. Make a Number Verification Request using this code to verify the number

This package contains methods to help with Steps 1 and 3.

### Get an OIDC URL

```python
from vonage_network_number_verification import CreateOidcUrl

url_options = CreateOidcUrl(
redirect_uri='https://example.com/redirect',
state='c9896ee6-4ff8-464c-b393-d56d6e638f88',
login_hint='+990123456',
)

url = number_verification.get_oidc_url(url_options)
print(url)
```

Get your user's device to follow this URL and a code to use for number verification will be returned in the final redirect query parameters. Note: your user must be connected to their mobile network.

### Make a Number Verification Request

```python
from vonage_network_number_verification import NumberVerificationRequest

response = number_verification.verify(
NumberVerificationRequest(
code='code',
redirect_uri='https://example.com/redirect',
phone_number='+990123456',
)
)
print(response.device_phone_number_verified)
```

## Network Sim Swap API

### Check if a SIM Has Been Swapped

```python
from vonage_network_sim_swap import SwapStatus
swap_status: SwapStatus = vonage_client.sim_swap.check(phone_number='MY_NUMBER')
print(swap_status.swapped)
```

### Get the Date of the Last SIM Swap

```python
from vonage_network_sim_swap import LastSwapDate
swap_date: LastSwapDate = vonage_client.sim_swap.get_last_swap_date
print(swap_date.last_swap_date)
```

## Number Insight API

### Make a Basic Number Insight Request
Expand Down
200 changes: 200 additions & 0 deletions V3_TO_V4_SDK_MIGRATION_GUIDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
# Vonage Python SDK v3 to v4 Migration Guide

This is a guide to help you migrate from using v3 of the Vonage Python SDK to using the new v4 `vonage` package. It has feature parity with the v3 package and contains many enhancements and structural changes. We will only be supporting v4 from its release.

The Vonage Python SDK (`vonage`) contains methods and data models to help you use many of Vonage's APIs. It also includes support for the new network APIs announced by Vonage.

## Contents

- [Structural Changes and Enhancements](#structural-changes-and-enhancements)
- [Installation](#installation)
- [Configuration](#configuration)
- [Accessing API Methods](#accessing-api-methods)
- [Accessing API Data Models](#accessing-api-data-models)
- [Response Objects](#response-objects)
- [API Changes](#API-changes)
- [Additional Resources](#additional-resources)

## Structural Changes and Enhancements

Here are some key changes to the SDK:

1. v4 of the Vonage Python SDK now uses a monorepo structure, with different packages for calling different Vonage APIs all using common code. You don't need to install the different packages directly as the top-level `vonage` package pulls them in and provides a common and consistent way to access methods.
2. The v4 SDK makes heavy use of [Pydantic data models](https://docs.pydantic.dev/latest/) to make it easier to call Vonage APIs and parse the results. This also enforces correct typing and makes it easier to pass the right objects to Vonage.
3. Docstrings have been added to methods and data models across the whole SDK to increase quality-of-life developer experience and make in-IDE development easier.
4. Many new custom errors have been added for finer-grained debugging. Error objects now contain more information and error messages give more information and context.
5. Support has been added for all [Vonage Video API](https://developer.vonage.com/en/video/overview) features, bringing it to feature parity with the OpenTok package. See [the OpenTok -> Vonage Video migration guide](video/OPENTOK_TO_VONAGE_MIGRATION.md) for migration assistance. If you're using OpenTok, migration to use v4 of the Vonage Python SDK rather than the `opentok` Python package is highly recommended.
6. APIs that have been deprecated by Vonage, e.g. Meetings API, have not been implemented in v4. Objects deprecated in v3 of the SDK have also not been implemented in v4.

## Installation

The most common way to use the new v4 package is by installing the top-level `vonage` package, similar to how you would install v3. The difference is that the new package will install the other Vonage API packages as dependencies.

To install the Python SDK package using pip:

```bash
pip install vonage
```

To upgrade your installed client library using pip:

```bash
pip install vonage --upgrade
```

You will notice that the dependent Vonage packages have been installed as well.

## Configuration

To get started with the v4 SDK, you'll need to initialize an instance of the `vonage.Vonage` class. This can then be used to access API methods. You need to provide authentication information and can optionally provide configuration options for the HTTP Client that's used (that is in the `vonage-http-client` package). This section will break all of this down then provide an example.

### Authentication

Depending on the Vonage API you want to use, you'll use different forms of authentication. You'll need to provide either an API key and secret or the ID of a Vonage Application and its corresponding private key. This is done by initializing an instance of `vonage.Auth`.

```python
from vonage import Auth

# API key/secret authentication
auth = Auth(api_key='your_api_key', api_secret='your_api_secret')

# Application ID/private key authentication
auth = Auth(application_id='your_api_key', private_key='your_api_secret')
```

This `auth` can then be used when initializing an instance of `vonage.Vonage` (example later in this section).

### Setting HTTP Client Options

The HTTP client used to make requests to Vonage comes with sensible default options, but if you need to change any of these, create a `vonage.HttpClientOptions` object and pass that in to `vonage.Vonage` when you create the object.

```python
# Create HttpClientOptions instance with some non-default settings
options = HttpClientOptions(api_host='new-api-host.example.com', timeout=100)
```

### Example

Putting this all together, to set up an instance of the `vonage.Vonage` class to call Vonage APIs, do this:

```python
from vonage import Vonage, Auth, HttpClientOptions

# Create an Auth instance
auth = Auth(api_key='your_api_key', api_secret='your_api_secret')

# Create HttpClientOptions instance
# (not required unless you want to change options from the defaults)
options = HttpClientOptions(api_host='new-api-host.example.com', timeout=100)

# Create a Vonage instance
vonage = Vonage(auth=auth, http_client_options=options)
```

## Accessing API Methods

To access methods relating to Vonage APIs, you'll create an instance of the `vonage.Vonage` class and access them via named attributes, e.g. if you have an instance of `vonage.Vonage` called `vonage_client`, use this syntax:

```python
vonage_client.vonage_api.api_method(...)

# E.g.
vonage_client.video.create_session(...)
```

This is very similar to the v3 SDK.

## Accessing API Data Models

Unlike the methods to call each Vonage API, the data models and errors specific to each API are not accessed through the `vonage` package, but are instead accessed through the specific API package.

For most APIs, data models and errors can be accessed from the top level of the API package, e.g. to send a Verify request, do this:

```python
from vonage_verify_v2 import VerifyRequest, SmsChannel

sms_channel = SmsChannel(to='1234567890')
verify_request = VerifyRequest(brand='Vonage', workflow=[sms_channel])

response = vonage_client.verify_v2.start_verification(verify_request)
print(response)
```

However, some APIs with a lot of models have them located under the `vonage_api_package.models` package, e.g. `vonage-messages`, `vonage-voice` and `vonage-video`. To access these, simply import from `.models`, e.g. to send an image via Facebook Messenger do this:

```python
from vonage_messages.models import MessengerImage, MessengerOptions, MessengerResource

messenger_image_model = MessengerImage(
to='1234567890',
from_='1234567890',
image=MessengerResource(url='https://example.com/image.jpg'),
messenger=MessengerOptions(category='message_tag', tag='invalid_tag'),
)

vonage_client.messages.send(message)
```

## Response Objects

In v3 of the SDK, the APIs returned Python dictionaries. In v4, almost all responses are now deserialized from the returned JSON into Pydantic models. Response data models are accessed in the same way as the other data models above and are also fully documented with useful docstrings.

If you want to convert the Pydantic responses into dictionaries, just use the `model_dump` method on the response. For example:

```python
from vonage_account import SettingsResponse

settings: SettingsResponse = vonage_client.account.update_default_sms_webhook(
mo_callback_url='https://example.com/sms_webhook',
dr_callback_url='https://example.com/delivery_receipt_webhook',
)

print(settings.model_dump())
```

Response fields are also converted into snake_case where applicable, so as to be more pythonic. This means they won't necessarily match the API one-to-one.

## API Changes

In v3, you access `vonage.Client`. In v4, it's `vonage.Vonage`.

The methods to get and set host attributes in v3 e.g. `vonage.Client.api_host` have been removed. You now get these options in v4 via the `vonage.Vonage.http_client`. Set these options in v4 by adding the options you want to the `vonage.HttpClientOptions` data model when initializing a `vonage.Vonage` object.

Rather than just returning a `ClientError` when an HTTP Client error is thrown, we now throw more specific errors with more information.

### Specific API Changes

The process for verifying a number using the Network Number Verification API has been simplified. In v3 it was required to exchange a code for a token then use this token in the verify request. In v4, these steps are combined so both functions are taken care of in the `NetworkNumberVerification.verify` method.

Verify v2 functionality is accessed from `verify2` in v3 and `verify_v2` in v4.

SMS message signing/verifying signatures code in `vonage.Client` in v3 has been moved into the `vonage-http-client` package in v4. This can be accessed via the `vonage` package as we import the `vonage-http-client.Auth` class into its namespace.

Old method -> new method
`vonage.Client.sign_params` -> `vonage.Auth.sign_params`
`vonage.Client.check_signature` -> `vonage.Auth.check_signature`

### Method Name Changes

Some methods from v3 have had their names changed in v4. Assuming you access all methods from the `vonage.Vonage` class in v4 with `vonage.Vonage.api_method` or the `vonage.Client` class in v3 with `vonage.Client.api_method`, this table details the changes:

| 3.x Method Name | 4.x Method Name |
|-----------------|-----------------|
| `account.topup` | `account.top_up` |
| `messages.send_message` | `messages.send` |
| `messages.revoke_outbound_rcs_message` | `messages.revoke_rcs_message` |
| `number_insight.get_basic_number_insight` | `number_insight.basic_number_insight` |
| `number_insight.get_standard_number_insight` | `number_insight.standard_number_insight` |
| `number_insight.get_advanced_number_insight` | `number_insight.advanced_sync_number_insight` |
| `number_insight.get_async_advanced_number_insight` | `number_insight.advanced_async_number_insight` |
| `numbers.get_account_numbers` | `numbers.list_owned_numbers` |
| `numbers.get_available_numbers` | `numbers.search_available_numbers` |
| `sms.send_message` | `sms.send` |
| `verify.psd2` | `verify.start_psd2_verification` |
| `verify.check` | `verify.check_code` |
| `verify.check` | `verify.check_code` |
| `verify2.new_request` | `verify_v2.start_verification` |
| `video.set_stream_layout` | `video.change_stream_layout` |

## Additional Resources

2 changes: 1 addition & 1 deletion messages/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Some message types have submodels with additional fields. In this case, import t
e.g.

```python
from vonage_messages import MessengerImage, MessengerOptions, MessengerResource
from vonage_messages.models import MessengerImage, MessengerOptions, MessengerResource

messenger = MessengerImage(
to='1234567890',
Expand Down
Loading

0 comments on commit 329d775

Please sign in to comment.