Last active
February 16, 2020 22:17
-
-
Save gustavorv86/5a59d67366bc656909caee3288cdbbfd to your computer and use it in GitHub Desktop.
Pythonic Rest time service using Flask
This file contains hidden or 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
#!/usr/bin/env python3 | |
# | |
# REQUIREMENTS | |
# | |
# Flask-RESTful 0.3.8 | |
# pyOpenSSL 19.1.0 | |
# | |
import datetime | |
import time | |
from flask import Flask | |
from flask_restful import Api, Resource, reqparse | |
## Tests: | |
## Install curl: apt install curl | |
# | |
# curl https://127.0.0.1:4443/api/time | |
# | |
# curl https://127.0.0.1:4443/api/time?format=rfc2822 | |
# | |
# curl https://127.0.0.1:4443/api/time?format=datetime | |
# | |
# curl https://127.0.0.1:4443/api/time?format=ctime | |
# | |
# curl --header "Content-Type: application/json" --request POST --data '{"format": "rfc2822"}' "http://127.0.0.1:4443/api/time" | |
# | |
class TIME_FORMAT: | |
UNIX = "unix" | |
RFC2822 = "rfc2822" | |
DATETIME = "datetime" | |
CTIME = "ctime" | |
class TimeService(Resource): | |
NAME = "/api/time" | |
FORMAT_LIST = "{}, {}, {}, {}".format( | |
TIME_FORMAT.UNIX, | |
TIME_FORMAT.RFC2822, | |
TIME_FORMAT.DATETIME, | |
TIME_FORMAT.CTIME | |
) | |
def __init__(self): | |
self.parser = reqparse.RequestParser() | |
self.parser.add_argument("format", type=str, required=False, | |
help="Accept values: {}".format(TimeService.FORMAT_LIST)) | |
def get(self): | |
return self.process_request() | |
def post(self): | |
return self.process_request() | |
@staticmethod | |
def fmt_unix(): | |
unix_time = time.time() | |
unix_sec = int(unix_time) | |
unix_usec = int((unix_time - unix_sec) * 1e6) | |
return {"sec": unix_sec, "usec": unix_usec} | |
@staticmethod | |
def fmt_rfc2822(): | |
time_value = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()) | |
return {"time": time_value} | |
@staticmethod | |
def fmt_datetime(): | |
time_value = "{}".format(datetime.datetime.now()) | |
return {"time": time_value} | |
@staticmethod | |
def fmt_ctime(): | |
time_value = time.ctime() | |
return {"time": time_value} | |
def process_request(self): | |
args = self.parser.parse_args() | |
ret_val = {} | |
if args["format"] == TIME_FORMAT.UNIX or args["format"] == None: | |
# Default format option | |
return TimeService.fmt_unix() | |
elif args["format"] == TIME_FORMAT.RFC2822: | |
return TimeService.fmt_rfc2822() | |
elif args["format"] == TIME_FORMAT.DATETIME: | |
return TimeService.fmt_datetime() | |
elif args["format"] == TIME_FORMAT.CTIME: | |
return TimeService.fmt_ctime() | |
else: | |
return {"error": "Invalid format '{}'".format(args["format"])} | |
def main(): | |
app = Flask(__name__) | |
api = Api(app) | |
api.add_resource(TimeService, TimeService.NAME) | |
app.run(host="0.0.0.0", port=4443, ssl_context="adhoc", debug=True) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment