-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshortened-url-maintainer.py
107 lines (90 loc) · 3.78 KB
/
shortened-url-maintainer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
from locust import HttpUser, task, between
import os
from dotenv import load_dotenv
import random
import json
from os.path import exists
load_dotenv('.env')
lower_time_limit = 3.0
upper_time_limit = 7.0
class ShortenedUrl(HttpUser):
wait_time = between(lower_time_limit, upper_time_limit)
host = os.environ.get('api-host')
path_base = os.environ.get('path-base')
shortened_url_path = os.environ.get('path-shortened-urls')
def randomFromFile(self, instance = "shortened-urls"):
try:
files = os.listdir(self.path_base + instance)
if len(files) < 1:
return 0
filename = files[random.randint(0, len(files) - 1)]
f = open(self.path_base + instance + "/" + filename, "r")
item_data = f.read()
return item_data
except:
return "error randomFromFile"
def deleteFromFile(self, filename, instance = "shortened-urls"):
try:
files = os.listdir(self.path_base + instance)
if len(files) > 0 and exists(self.path_base + instance + "/" + filename + ".txt"):
os.remove(self.path_base + instance + "/" + filename + ".txt")
except ValueError as err:
return "error deleteFromFile" + err.args
@task(1)
def removeShortenedUrl(self):
shortened_info = self.randomFromFile("shortened-urls")
if shortened_info != 0:
headers = { "Content-Type": "application/json" }
shortened_array = str(shortened_info).split(";")
shortened_id = shortened_array[2]
rs = self.client.delete("/shortened_urls/{0}".format(shortened_id),
headers=headers,
name="/removeShortenedUrl")
if rs.status_code == 200:
self.deleteFromFile(shortened_id, "shortened-urls")
@task(1)
def editShortenedUrl(self):
shortened_info = self.randomFromFile("shortened-urls")
if shortened_info != 0:
headers = { "Content-Type": "application/json" }
shortened_array = str(shortened_info).split(";")
shortened_id = shortened_array[2]
rs = self.client.patch("/shortened_urls/{0}".format(shortened_id), json = {
"url": "https://www.google.com",
"name": "Edited",
"enabled": True
},
headers=headers,
name="/editShortenedUrl")
@task(2)
def getPaginatedShortenedUrls(self):
headers = { "Content-Type": "application/json" }
rs = self.client.get("/shortened-urls?page=1&limit=10",
headers=headers,
name="/getPaginatedShortenedUrls")
@task(3)
def getShortenedUrlById(self):
shortened_info = self.randomFromFile("shortened-urls")
if shortened_info != 0:
headers = { "Content-Type": "application/json" }
shortened_array = str(shortened_info).split(";")
shortened_id = shortened_array[2]
rs = self.client.get("/shortened_urls/{0}".format(shortened_id),
headers=headers,
name="/getShortenedUrlById")
@task(5)
def createShortenedUrl(self):
headers = { "Content-Type": "application/json" }
item_url = "https://www.mercadolibre.cl/"
item_name = "Meli"
rs = self.client.post("/shortened-urls", json = {
"url": item_url,
"name": item_name,
"enabled": True
}, headers = headers, name = "/createShortenedUrl")
response_json = rs.json()
shortened_info = item_name + ";" + item_url + ";" + response_json['shortened']
if rs.status_code == 201:
with open(self.shortened_url_path + response_json['shortened'] + ".txt", "w", newline='') as f:
f.write(shortened_info)
f.close()