Created
October 24, 2021 04:44
-
-
Save franzramadhan/09839e876d052e9f0bc4506a91ad01f1 to your computer and use it in GitHub Desktop.
Mock HTTP Request and set custom message for it's responses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3.9.7 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Buat script/tools untuk melakukan cek respons status dari suatu WEB API https://mock.codes/{status.code}, dengan requirement: | |
# - cek status dilakukan secara periodic (setiap 2 detik) | |
# - jika respon status unexpected (!= 200/OK) maka print message "err: status NNN" | |
# - jika respon status kembali expected (= 200/OK) maka print message "ok: recovered" | |
# | |
# contoh skenario: | |
# time response message | |
# 13:00:00 200 | |
# 13:00:05 500 err: status 500 | |
# 13:00:10 500 | |
# 13:00:15 200 ok: recovered | |
# 13:00:20 200 | |
# 13:00:25 404 err: status 404 | |
# | |
#### | |
import urllib3 | |
import time | |
url = "https://mock.codes/" | |
def request(): | |
http = urllib3.PoolManager() | |
status = [200, 500, 500, 200, 200, 500, 404, 200] | |
last = 200 | |
for stat in status: | |
time.sleep(2) | |
response = http.request("GET", url + str(stat)) | |
if response.status == last: | |
continue | |
else: | |
last = response.status | |
if response.status == 200: | |
print("ok: recovered") | |
elif response.status != 200: | |
print("err: status " + str(response.status)) | |
if __name__ == "__main__": | |
request() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[[source]] | |
url = "https://pypi.org/simple" | |
verify_ssl = true | |
name = "pypi" | |
[packages] | |
urllib3 = "*" | |
[dev-packages] | |
[requires] | |
python_version = "3.9" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"_meta": { | |
"hash": { | |
"sha256": "7776f372e3411c742b3e8a4209e67f0832f9e249d4c86ce28ece146afaef68d1" | |
}, | |
"pipfile-spec": 6, | |
"requires": { | |
"python_version": "3.9" | |
}, | |
"sources": [ | |
{ | |
"name": "pypi", | |
"url": "https://pypi.org/simple", | |
"verify_ssl": true | |
} | |
] | |
}, | |
"default": { | |
"urllib3": { | |
"hashes": [ | |
"sha256:4987c65554f7a2dbf30c18fd48778ef124af6fab771a377103da0585e2336ece", | |
"sha256:c4fdf4019605b6e5423637e01bc9fe4daef873709a7973e195ceba0a62bbc844" | |
], | |
"index": "pypi", | |
"version": "==1.26.7" | |
} | |
}, | |
"develop": {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment