1
+ #Importing required libraries
2
+ import fakerapi
3
+ import json
4
+ import requests
5
+
6
+ '''
7
+ Faker Library is used to create fake data
8
+ In it's syntax , Every resource accepts 3 common GET parameters:
9
+ _locale
10
+ _quantity
11
+ _seed
12
+
13
+
14
+ _Locale : This parameter means the language of the API response we want to get and accept the locale format "en_EN".
15
+ For example: https://fakerapi.it/api/v1/persons?_locale=en_EN . This example returns people with English names.
16
+
17
+ _quantity : This parameter means the number of rows we want to obtain and accept only integers.
18
+ If you request more than 1000 rows (maximum) the system will return 1000 rows anyway.
19
+ Default Value : 10
20
+ Min: 1 - Max: 1000
21
+ Example : https://fakerapi.it/api/v1/addresses?_quantity=5 This example returns 5 Addresses
22
+
23
+ _seed : This parameter accept an integer and allows to get always the same results.
24
+ So, executing the same request with _seed parameter set to the same value (ex. 12345) the results will never change.
25
+ Default Value : null
26
+ Example : https://fakerapi.it/api/v1/companies?_seed=12456
27
+
28
+ '''
29
+
30
+ #Now we will go through the basic example and discover some of the options Faker provides us to generate random data for
31
+
32
+ response = requests .get ('https://fakerapi.it/api/v1/addresses?_quantity=5' )
33
+ print (response .status_code )
34
+ #To check whether API call is successful(200) or not(404)
35
+
36
+ print (response .text )
37
+ # To access all the content of the website
38
+
39
+ print (response .url )
40
+ # print(response.url) # url of the website to which we made our API call
41
+
42
+ print ("\n \n " )
43
+ python_data = json .loads (response .text ) # Converting json data to python data
44
+ # print(type(python_data))
45
+ for key in python_data .keys ():
46
+ print (f'{ key } : { python_data [key ]} ' ) # Calling the whole data from the API
47
+
48
+ # In this example 5 Addresses were printed , apart from addresses we will see some other options Faker provides us with.
49
+
50
+ #Example 1 : Credit Cards
51
+ response = requests .get ('https://fakerapi.it/api/v1/credit_cards?_quantity=3' )
52
+ print ("\n \n " )
53
+ python_data = json .loads (response .text ) # Converting json data to python data
54
+ # print(type(python_data))
55
+ for key in python_data .keys ():
56
+ print (f'{ key } : { python_data [key ]} ' )
57
+
58
+ #In this example we got details of 3 fake credit cards
59
+
60
+ #Example 2 : Places
61
+ response = requests .get ('https://fakerapi.it/api/v1/places?_quantity=1' )
62
+ print ("\n \n " )
63
+ python_data = json .loads (response .text ) # Converting json data to python data
64
+ # print(type(python_data))
65
+ for key in python_data .keys ():
66
+ print (f'{ key } : { python_data [key ]} ' )
67
+ # In this example we are generating fake data for any random place with position of their longitude and longitude
68
+
69
+ #Example 3 : Persons
70
+ response = requests .get ('https://fakerapi.it/api/v1/users?_quantity=7&_gender=female' )
71
+ print ("\n \n " )
72
+ python_data = json .loads (response .text ) # Converting json data to python data
73
+ # print(type(python_data))
74
+ for key in python_data .keys ():
75
+ print (f'{ key } : { python_data [key ]} ' )
76
+ # In this data of 7 random females are generated , with their firstname, lastname,email,etc.
0 commit comments