Skip to content

Commit 3925d97

Browse files
authored
Merge pull request #8 from Shashwat79802/main
feat: adding new python sample app
2 parents 6c3f49e + 7a6ad6d commit 3925d97

13 files changed

Lines changed: 853 additions & 0 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ This repo contains the samples for [Keploy's](https://keploy.io) Python-based Ap
1919

2020
3. [FastAPI-Postgres](https://github.com/keploy/samples-python/tree/main/fastapi-postgres) - This application is a student management API built using Python's FastAPI and PostgreSQL for data storage. It allows you to perform basic CRUD (Create, Read, Update, Delete) operations on student data.
2121

22+
4. [FastAPI-Twilio](https://github.com/keploy/samples-python/tree/main/fastapi-twilio) - This application is a SMS sending API built using Python's FastAPI and Twilio for their SMS sharing service.
23+
2224
## Community Support ❤️
2325

2426
We'd love to collaborate with you to make Keploy great. To get started:

fastapi-twilio/.env

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
TWILIO_ACCOUNT_SID=YOUR_ACCOUNT_SID
2+
TWILIO_AUTH_TOKEN=YOUR_ACCOUNT_AUTH_TOKEN
3+
TWILIO_NUMBER=YOUR_PURCHASED_PHONE_NUMBER

fastapi-twilio/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
./venv
2+
./__pycache__

fastapi-twilio/Dockerfile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM python:3.11.5-bullseye
2+
3+
WORKDIR /app
4+
5+
COPY . /app/
6+
7+
RUN pip install --no-cache-dir --upgrade -r /app/requirements.txt
8+
9+
EXPOSE 8000
10+
11+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]

fastapi-twilio/README.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# FastAPI-Twilio Application
2+
3+
A sample FastAPI-Twilio app to test Keploy integration capabilities using [FastAPI](https://fastapi.tiangolo.com/) and [Twilio](https://www.twilio.com/en-us). <br>
4+
5+
## Installation Setup
6+
7+
```bash
8+
git clone https://github.com/keploy/samples-python.git && cd samples-python/fastapi-twilio
9+
pip3 install -r requirements.txt
10+
```
11+
12+
## Installation Keploy
13+
14+
Keploy can be installed on Linux directly and on Windows with the help of WSL. Based on your system architecture, install the keploy latest binary release
15+
16+
**1. AMD Architecture**
17+
18+
```shell
19+
curl --silent --location "https://github.com/keploy/keploy/releases/latest/download/keploy_linux_amd64.tar.gz" | tar xz -C /tmp
20+
21+
sudo mkdir -p /usr/local/bin && sudo mv /tmp/keploy /usr/local/bin && keploy
22+
```
23+
24+
<details>
25+
<summary> 2. ARM Architecture </summary>
26+
27+
```shell
28+
curl --silent --location "https://github.com/keploy/keploy/releases/latest/download/keploy_linux_arm64.tar.gz" | tar xz -C /tmp
29+
30+
sudo mkdir -p /usr/local/bin && sudo mv /tmp/keploy /usr/local/bin && keploy
31+
```
32+
33+
</details>
34+
35+
### Get your Twilio Credentials
36+
37+
You can get your Twilio credentials by signing in to [Twilio Console](https://console.twilio.com/).
38+
Once you get the Twilio Account SID, Auth Token, and Phone Number, modify the `.env` file with your credentials.
39+
40+
### Capture the Testcases
41+
42+
This command will start the recording of API calls:-
43+
44+
```shell
45+
keploy record -c "uvicorn main:app --reload"
46+
```
47+
48+
Make API Calls using Hoppscotch, Postman or cURL command. Keploy with capture those calls to generate the test-suites containing testcases and data mocks.
49+
50+
### Make the POST requests
51+
52+
1. Replace the place holder below i.e. `YOUR_REGISTERED_PERSONAL_PHONE_NUMBER` with your registered personal phone number that you linked with Twilio.
53+
54+
```bash
55+
curl --location 'http://127.0.0.1:8000/send-sms/' \
56+
--header 'Content-Type: application/json' \
57+
--data '{
58+
"Body": "Test, testtt, testttttttssss :)",
59+
"To": "YOUR_REGISTERED_PERSONAL_PHONE_NUMBER",
60+
}'
61+
```
62+
63+
2. Replace the place holder below i.e. `SOME_WRONG_PHONE_NUMBER` with any wrong phone number and make the request.
64+
65+
```bash
66+
curl --location 'http://127.0.0.1:8000/send-sms/' \
67+
--header 'Content-Type: application/json' \
68+
--data '{
69+
"Body": "Test, testtt, testttttttssss :)",
70+
"To": "SOME_WRONG_PHONE_NUMBER",
71+
}'
72+
```
73+
74+
Now all these API calls were captured as **editable** testcases and written to `keploy/tests` folder. The keploy directory would also have `mocks` file that contains all the outputs of Twilio operations.
75+
76+
## Run the Testcases
77+
78+
Now let's run the application in test mode.
79+
80+
```shell
81+
keploy test -c "uvicorn main:app --reload" --delay 10
82+
```
83+
84+
So, no need to setup fake apis like Twilio or write mocks for them. Keploy automatically mocks them and, **The application thinks it's talking to Twilio 😄**

fastapi-twilio/keploy-config.yaml

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
record:
2+
path: ""
3+
# mandatory
4+
command: ""
5+
proxyport: 0
6+
containerName: ""
7+
networkName: ""
8+
delay: 5
9+
passThroughPorts: []
10+
test:
11+
path: ""
12+
# mandatory
13+
command: ""
14+
proxyport: 0
15+
containerName: ""
16+
networkName: ""
17+
testSets: []
18+
globalNoise: |-
19+
{
20+
"global": {
21+
"body": {},
22+
"header": {}
23+
},
24+
"test-sets": {
25+
"test-set-name": {
26+
"body": {},
27+
"header": {}
28+
}
29+
}
30+
}
31+
delay: 5
32+
apiTimeout: 5
33+
passThroughPorts: []
34+
#
35+
# Example on using globalNoise
36+
# globalNoise: |-
37+
# {
38+
# "global": {
39+
# "body": {
40+
# # to ignore some values for a field,
41+
# # pass regex patterns to the corresponding array value
42+
# "url": ["https?://\S+", "http://\S+"],
43+
# },
44+
# "header": {
45+
# # to ignore the entire field, pass an empty array
46+
# "Date: [],
47+
# }
48+
# },
49+
# # to ignore fields or the corresponding values for a specific test-set,
50+
# # pass the test-set-name as a key to the "test-sets" object and
51+
# # populate the corresponding "body" and "header" objects
52+
# "test-sets": {
53+
# "test-set-1": {
54+
# "body": {
55+
# # ignore all the values for the "url" field
56+
# "url": []
57+
# },
58+
# "header": {
59+
# # we can also pass the exact value to ignore for a field
60+
# "User-Agent": ["PostmanRuntime/7.34.0"]
61+
# }
62+
# }
63+
# }
64+
# }
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
version: api.keploy.io/v1beta1
2+
kind: Http
3+
name: mocks
4+
spec:
5+
metadata:
6+
name: Http
7+
operation: POST
8+
type: HTTP_CLIENT
9+
req:
10+
method: POST
11+
proto_major: 1
12+
proto_minor: 1
13+
url: /2010-04-01/Accounts/AC19413687d9ce28c80cda944730f8b286/Messages.json
14+
header:
15+
Accept: '*/*'
16+
Accept-Encoding: gzip, deflate
17+
Authorization: Basic QUMxOTQxMzY4N2Q5Y2UyOGM4MGNkYTk0NDczMGY4YjI4NjpjMTc0MDc5YzU2NTA0N2FmYWJmNDk5MWI2ZGQ1MmFiYg==
18+
Connection: keep-alive
19+
Content-Length: "82"
20+
Content-Type: application/x-www-form-urlencoded
21+
User-Agent: python-requests/2.31.0
22+
body: Body=Test%2C+testtt%2C+testttttttssss+%3A%29&From=%2B16413324066&To=%2B91700004379
23+
body_type: ""
24+
timestamp: 0001-01-01T00:00:00Z
25+
resp:
26+
status_code: 400
27+
header:
28+
Access-Control-Allow-Credentials: "true"
29+
Access-Control-Allow-Headers: Accept, Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, Idempotency-Key
30+
Access-Control-Allow-Methods: GET, POST, DELETE, OPTIONS
31+
Access-Control-Allow-Origin: '*'
32+
Access-Control-Expose-Headers: ETag
33+
Connection: keep-alive
34+
Content-Length: 158,158
35+
Content-Type: application/json
36+
Date: Tue, 14 Nov 2023 09:26:29 GMT
37+
Strict-Transport-Security: max-age=31536000
38+
Twilio-Concurrent-Requests: "1"
39+
Twilio-Request-Duration: "0.051"
40+
Twilio-Request-Id: RQec3c4676524fe2951583489e90bc1b33
41+
X-Api-Domain: api.twilio.com
42+
X-Home-Region: us1
43+
X-Powered-By: AT-5000
44+
X-Shenanigans: none
45+
body: '{"code": 21211, "message": "The ''To'' number +9170000XXXX is not a valid phone number", "more_info": "https://www.twilio.com/docs/errors/21211", "status": 400}'
46+
body_type: ""
47+
status_message: ""
48+
proto_major: 0
49+
proto_minor: 0
50+
timestamp: 0001-01-01T00:00:00Z
51+
objects: []
52+
created: 1699953989
53+
reqTimestampMock: 2023-11-14T14:56:28.791864295+05:30
54+
resTimestampMock: 2023-11-14T14:56:29.304057844+05:30
55+
---
56+
version: api.keploy.io/v1beta1
57+
kind: Http
58+
name: mocks
59+
spec:
60+
metadata:
61+
name: Http
62+
operation: POST
63+
type: HTTP_CLIENT
64+
req:
65+
method: POST
66+
proto_major: 1
67+
proto_minor: 1
68+
url: /2010-04-01/Accounts/AC19413687d9ce28c80cda944730f8b286/Messages.json
69+
header:
70+
Accept: '*/*'
71+
Accept-Encoding: gzip, deflate
72+
Authorization: Basic QUMxOTQxMzY4N2Q5Y2UyOGM4MGNkYTk0NDczMGY4YjI4NjpjMTc0MDc5YzU2NTA0N2FmYWJmNDk5MWI2ZGQ1MmFiYg==
73+
Connection: keep-alive
74+
Content-Length: "83"
75+
Content-Type: application/x-www-form-urlencoded
76+
User-Agent: python-requests/2.31.0
77+
body: Body=Test%2C+testtt%2C+testttttttssss+%3A%29&From=%2B16413324066&To=%2B917000043797
78+
body_type: ""
79+
timestamp: 0001-01-01T00:00:00Z
80+
resp:
81+
status_code: 201
82+
header:
83+
Access-Control-Allow-Credentials: "true"
84+
Access-Control-Allow-Headers: Accept, Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, Idempotency-Key
85+
Access-Control-Allow-Methods: GET, POST, DELETE, OPTIONS
86+
Access-Control-Allow-Origin: '*'
87+
Access-Control-Expose-Headers: ETag
88+
Connection: keep-alive
89+
Content-Length: 834,834
90+
Content-Type: application/json
91+
Date: Tue, 14 Nov 2023 09:26:35 GMT
92+
Twilio-Concurrent-Requests: "1"
93+
Twilio-Request-Duration: "0.131"
94+
Twilio-Request-Id: RQ2f5288499581f3fe12524b11bbb3d866
95+
X-Api-Domain: api.twilio.com
96+
X-Home-Region: us1
97+
X-Powered-By: AT-5000
98+
X-Shenanigans: none
99+
body: '{"body": "Sent from your Twilio trial account - Test, testtt, testttttttssss :)", "num_segments": "1", "direction": "outbound-api", "from": "+16413324066", "date_updated": "Tue, 14 Nov 2023 09:26:35 +0000", "price": null, "error_message": null, "uri": "/2010-04-01/Accounts/AC19413687d9ce28c80cda944730f8b286/Messages/SM2f5288499581f3fe12524b11bbb3d866.json", "account_sid": "AC19413687d9ce28c80cda944730f8b286", "num_media": "0", "to": "+917000043797", "date_created": "Tue, 14 Nov 2023 09:26:35 +0000", "status": "queued", "sid": "SM2f5288499581f3fe12524b11bbb3d866", "date_sent": null, "messaging_service_sid": null, "error_code": null, "price_unit": "USD", "api_version": "2010-04-01", "subresource_uris": {"media": "/2010-04-01/Accounts/AC19413687d9ce28c80cda944730f8b286/Messages/SM2f5288499581f3fe12524b11bbb3d866/Media.json"}}'
100+
body_type: ""
101+
status_message: ""
102+
proto_major: 0
103+
proto_minor: 0
104+
timestamp: 0001-01-01T00:00:00Z
105+
objects: []
106+
created: 1699953995
107+
reqTimestampMock: 2023-11-14T14:56:34.833506359+05:30
108+
resTimestampMock: 2023-11-14T14:56:35.27188354+05:30
109+
---
110+
version: api.keploy.io/v1beta1
111+
kind: Http
112+
name: mocks
113+
spec:
114+
metadata:
115+
name: Http
116+
operation: POST
117+
type: HTTP_CLIENT
118+
req:
119+
method: POST
120+
proto_major: 1
121+
proto_minor: 1
122+
url: /2010-04-01/Accounts/AC19413687d9ce28c80cda944730f8b286/Messages.json
123+
header:
124+
Accept: '*/*'
125+
Accept-Encoding: gzip, deflate
126+
Authorization: Basic QUMxOTQxMzY4N2Q5Y2UyOGM4MGNkYTk0NDczMGY4YjI4NjpjMTc0MDc5YzU2NTA0N2FmYWJmNDk5MWI2ZGQ1MmFiYg==
127+
Connection: keep-alive
128+
Content-Length: "81"
129+
Content-Type: application/x-www-form-urlencoded
130+
User-Agent: python-requests/2.31.0
131+
body: Body=Test%2C+testtt%2C+testttttttssss+%3A%29&From=%2B16413324066&To=%2B9170000437
132+
body_type: ""
133+
timestamp: 0001-01-01T00:00:00Z
134+
resp:
135+
status_code: 400
136+
header:
137+
Access-Control-Allow-Credentials: "true"
138+
Access-Control-Allow-Headers: Accept, Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, Idempotency-Key
139+
Access-Control-Allow-Methods: GET, POST, DELETE, OPTIONS
140+
Access-Control-Allow-Origin: '*'
141+
Access-Control-Expose-Headers: ETag
142+
Connection: keep-alive
143+
Content-Length: 335,335
144+
Content-Type: application/json
145+
Date: Tue, 14 Nov 2023 09:27:21 GMT
146+
Twilio-Concurrent-Requests: "1"
147+
Twilio-Request-Duration: "0.080"
148+
Twilio-Request-Id: RQb54d7f05d29e83bc89889cc136bcd99d
149+
X-Api-Domain: api.twilio.com
150+
X-Home-Region: us1
151+
X-Powered-By: AT-5000
152+
X-Shenanigans: none
153+
body: '{"code": 21608, "message": "The number +917000XXXX is unverified. Trial accounts cannot send messages to unverified numbers; verify +917000XXXX at twilio.com/user/account/phone-numbers/verified, or purchase a Twilio number to send messages to unverified numbers", "more_info": "https://www.twilio.com/docs/errors/21608", "status": 400}'
154+
body_type: ""
155+
status_message: ""
156+
proto_major: 0
157+
proto_minor: 0
158+
timestamp: 0001-01-01T00:00:00Z
159+
objects: []
160+
created: 1699954041
161+
reqTimestampMock: 2023-11-14T14:57:20.914415283+05:30
162+
resTimestampMock: 2023-11-14T14:57:21.298027703+05:30

0 commit comments

Comments
 (0)