Skip to content

Commit 612fa08

Browse files
authored
Merge pull request #4 from sinch/DEVEXP-963-Numbers_Snippets_update
DEVEXP-963: Numbers Snippets update
2 parents f9160ea + 16c5b27 commit 612fa08

File tree

11 files changed

+99
-36
lines changed

11 files changed

+99
-36
lines changed

.env

Lines changed: 0 additions & 3 deletions
This file was deleted.

.env.example

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# The project ID where are defined the resources you want to use.
2+
SINCH_PROJECT_ID=
3+
4+
# The API key ID and secret to authenticate your requests to the Sinch API.
5+
SINCH_KEY_ID=
6+
SINCH_KEY_SECRET=
7+
8+
# The virtual phone number you have rented from Sinch or planning to rent.
9+
SINCH_PHONE_NUMBER=
10+
11+
# The service plan ID for your Sinch account to configure the SMS plan associated with your virtual phone number.
12+
SINCH_SERVICE_PLAN_ID=

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ celerybeat.pid
9393
*.sage.py
9494

9595
# Environments
96+
.env
9697
.venv
9798
env/
9899
venv/

README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,58 @@ Sinch Python SDK Code Snippets Repository
44

55
This repository contains code snippets demonstrating usage of the
66
[Sinch Python SDK](https://github.com/sinch/sinch-sdk-python).
7+
8+
## Requirements
9+
- Python 3.9 or later
10+
- [Poetry](https://python-poetry.org/) for dependency management
11+
- [Sinch account](https://dashboard.sinch.com)
12+
- [Sinch package](https://pypi.org/project/sinch/)
13+
14+
15+
## Snippets execution settings
16+
When executing a snippet, you will need to provide some information about your Sinch account (credentials, Sinch virtual phone number, ...)
17+
18+
These settings can be placed directly in the snippet source code, **or** you can use an environment file (`.env`). Using an environment file allows the settings to be shared and used automatically by every snippet.
19+
20+
### Setting Up Your Environment File
21+
22+
#### 1. Rename the example file
23+
24+
**Linux / Mac:**
25+
```bash
26+
cp .env.example .env
27+
```
28+
29+
**Windows (Command Prompt):**
30+
```cmd
31+
copy .env.example .env
32+
```
33+
34+
Windows (PowerShell):
35+
```powershell
36+
Copy-Item .env.example .env
37+
```
38+
39+
#### 2. Fill in your credentials
40+
41+
Open the newly created [.env](.env) file in your preferred text editor and fill in the required values (e.g., SINCH_PROJECT_ID=your_project_id).
42+
43+
Note: Do not share your .env file or credentials publicly.
44+
45+
46+
### Install dependencies using Poetry:
47+
48+
```bash
49+
poetry install
50+
```
51+
52+
53+
## Running snippets
54+
55+
All available code snippets are located in the `snippets/` directory, structured by feature and corresponding actions.
56+
57+
To execute a specific snippet, navigate to the appropriate subdirectory and run:
58+
59+
```shell
60+
python run python snippet.py
61+
```

pyproject.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[tool.poetry]
2+
name = "sinch-sdk-python-snippets"
3+
version = "0.1.0"
4+
description = "Code snippets demonstrating usage of the Sinch Python SDK"
5+
readme = "README.md"
6+
packages = [{include = "snippets"}]
7+
8+
[tool.poetry.dependencies]
9+
python = "^3.9"
10+
python-dotenv = "^1.0.0"
11+
# sinch = "^2.0.0" # Uncomment once v2.0 is released
12+
13+
[build-system]
14+
requires = ["poetry-core"]
15+
build-backend = "poetry.core.masonry.api"

snippets/numbers/active_numbers/get/snippet.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
key_secret=os.environ.get("SINCH_KEY_SECRET") or "MY_KEY_SECRET"
1717
)
1818

19-
phone_number = "MY_SINCH_PHONE_NUMBER"
19+
phone_number = os.environ.get("SINCH_PHONE_NUMBER") or "MY_SINCH_PHONE_NUMBER"
2020
response = sinch_client.numbers.get(phone_number=phone_number)
2121

2222
print(f"Rented number details:\n{response}")

snippets/numbers/active_numbers/release/snippet.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
key_secret=os.environ.get("SINCH_KEY_SECRET") or "MY_KEY_SECRET"
1717
)
1818

19-
phone_number = "PHONE_NUMBER_TO_BE_RELEASED"
19+
phone_number = os.environ.get("SINCH_PHONE_NUMBER") or "MY_SINCH_PHONE_NUMBER"
2020
released_number = sinch_client.numbers.release(
2121
phone_number=phone_number
2222
)

snippets/numbers/active_numbers/update/snippet.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import os
88
from dotenv import load_dotenv
99
from sinch import SinchClient
10-
from sinch.domains.numbers.models.v1.types import VoiceConfigurationDictType
1110

1211
load_dotenv()
1312

@@ -17,18 +16,12 @@
1716
key_secret=os.environ.get("SINCH_KEY_SECRET") or "MY_KEY_SECRET"
1817
)
1918

20-
phone_number = "PHONE_NUMBER"
21-
app_id = "APP_ID"
22-
display_name = "DISPLAY_NAME"
23-
voice_configuration: VoiceConfigurationDictType = {
24-
"app_id": app_id,
25-
"type": "RTC"
26-
}
19+
phone_number_to_update = os.environ.get("SINCH_PHONE_NUMBER") or "MY_SINCH_PHONE_NUMBER"
20+
updated_display_name = "Updated DISPLAY_NAME"
2721

2822
response = sinch_client.numbers.update(
29-
phone_number=phone_number,
30-
display_name=display_name,
31-
voice_configuration=voice_configuration
23+
phone_number=phone_number_to_update,
24+
display_name=updated_display_name
3225
)
3326

3427
print("Updated Number:\n", response)

snippets/numbers/available_numbers/check_availability/snippet.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@
2121
phone_number=phone_number
2222
)
2323

24-
print("Released Number:\n", response)
24+
print("The phone number is available:\n", response)

snippets/numbers/available_numbers/rent/snippet.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717
key_secret=os.environ.get("SINCH_KEY_SECRET") or "MY_KEY_SECRET"
1818
)
1919

20-
phone_number = "AVAILABLE_PHONE_NUMBER_TO_BE_RENTED"
21-
service_plan_id = "SERVICE_PLAN_ID"
20+
phone_number_to_be_rented = "AVAILABLE_PHONE_NUMBER_TO_BE_RENTED"
21+
service_plan_id_to_associate_with_the_number = os.environ.get("SINCH_SERVICE_PLAN_ID") or "MY_SERVICE_PLAN_ID"
2222
sms_configuration: SmsConfigurationDict = {
23-
"service_plan_id": service_plan_id
23+
"service_plan_id": service_plan_id_to_associate_with_the_number
2424
}
2525

2626
rented_number = sinch_client.numbers.rent(
27-
phone_number=phone_number,
27+
phone_number=phone_number_to_be_rented,
2828
sms_configuration=sms_configuration
2929
)
3030
print("Rented Number:\n", rented_number)

0 commit comments

Comments
 (0)