I've just developed the primitive API client for Instagram based on "requests" library. The next goal is to implement retry logic for 400+ and 500+ error codes. There is a bunch of examples how to do this, but I'm wondering how to test the solution. Should it be some function which randomly decides weither to pass request to API portal or to return some error code instead? Or maybe there is somewhere on earth the unreliable API to practice on? or some server that genarates errors intentionally?
You can use this startup script to create a small HTTP server:
from http.server import BaseHTTPRequestHandler, HTTPServer
hostName = "localhost"
serverPort = 8080
class MyServer(BaseHTTPRequestHandler):
def do_GET(self):
code = self.path.split('/')[1]
# do stuff here
self.send_response(int(code))
self.send_header("Content-type", "text/html")
self.end_headers()
if __name__ == '__main__':
webServer = HTTPServer((hostName, serverPort), MyServer)
try:
webServer.serve_forever()
except KeyboardInterrupt:
pass
webServer.server_close()
How to use:
import requests
r = requests.get('http://localhost:8080/403')
print(r.status_code)
# Output
403