class ItemPostRequest: brand = doc.String() name = doc.String(required=True) item_size = doc.Float(required=True) unit = doc.String(required=True) count = doc.Float(required=True) category = doc.String() subcategory = doc.String() expiration_date = doc.Date()
class ItemPutRequest: id = doc.Integer() # Ignored in favor of URL brand = doc.String() name = doc.String(required=True) item_size = doc.Float(required=True) unit = doc.String(required=True) count = doc.Float(required=True) serving = doc.Float() category = doc.String() subcategory = doc.String() expiration_date = doc.Date() purchase_date = doc.Date() created_at = doc.DateTime() deleted_at = doc.DateTime() inventory_id = doc.Integer(required=True)
class Model: name = doc.String(required=True) count = doc.Integer(required=True) fish = doc.Tuple() on = doc.Boolean() obj = doc.Dictionary() many = doc.Float()
class Event: event = doc.Integer("Event Number", required=True) dm = doc.Float("DM", required=True) snr = doc.Float("snr of the brightest beam", required=True) beams = [doc.Integer("beam number", required=True)] data_paths = doc.List() transfer_status = doc.String( "status of the transfer to zooniverse.", required=False, choices=["COMPLETE", "INCOMPLETE", "FAILED", "CLEANED"], ) zooniverse_classification = doc.String( "classification of the event from zooniverse", required=False, choices=["GOOD", "BAD", "INCOMPLETE"], ) expert_classification = doc.String( "classification of the event from tsars.", required=False, choices=["GOOD", "BAD", "INCOMPLETE"], )
class ExampleModel: """ Example domain object documentation that is used in swagger definition """ field_a = doc.Float(description="field a description") field_b = doc.Integer(description="field b description") field_c = doc.Date(description="field c description") field_d = doc.Boolean(description="field d description") field_e = doc.String(description="field e description") def _validation_schema(self): return { 'field_a': {'type': 'float', 'required': True, 'empty': False}, 'field_b': {'type': 'int', 'required': True, 'empty': False}, 'field_c': {'type': 'str', 'required': True, 'empty': False}, 'field_d': {'type': 'bool', 'required': True, 'empty': False}, 'field_e': {'type': 'str', 'required': True, 'empty': False}, }
def test_float_field(app): field = doc.Float() assert field.serialize() == {"type": "number", "format": "double"} @app.get("/") @doc.consumes(field, location="body", required=True) def test(request): return text("test") _, response = app.test_client.get("/swagger/swagger.json") assert response.status == 200 assert response.content_type == "application/json" swagger_json = response.json path = swagger_json["paths"]["/"]["get"] assert path["parameters"][0] == { "required": True, "in": "body", "name": None, "type": "number", "format": "double", }
class TestSchema: pass @pytest.mark.parametrize( "schema, expected_schema", [ (doc.Field, {}), (doc.Field(), {}), (int, {"type": "integer", "format": "int64"}), (doc.Integer, {"type": "integer", "format": "int64"}), (doc.Integer(), {"type": "integer", "format": "int64"}), (float, {"type": "number", "format": "double"}), (doc.Float, {"type": "number", "format": "double"}), (doc.Float(), {"type": "number", "format": "double"}), (str, {"type": "string"}), (doc.String, {"type": "string"}), (doc.String(), {"type": "string"}), (bool, {"type": "boolean"}), (doc.Boolean, {"type": "boolean"}), (doc.Boolean(), {"type": "boolean"}), (date, {"type": "string", "format": "date"}), (doc.Date, {"type": "string", "format": "date"}), (doc.Date(), {"type": "string", "format": "date"}), (datetime, {"type": "string", "format": "date-time"}), (doc.DateTime, {"type": "string", "format": "date-time"}), (doc.DateTime(), {"type": "string", "format": "date-time"}), (TestSchema, {"$ref": "#/definitions/TestSchema"}), (dict, {"type": "object", "properties": {}}), ({"foo": "bar"}, {"type": "object", "properties": {"foo": {}}}),
app = Sanic("water") app.blueprint(swagger_blueprint) db_conn = pymysql.Connect(host="127.0.0.1", user="******", password="******", db="auto_water", cursorclass=pymysql.cursors.DictCursor) @app.get("/temp") @doc.produces( { "ID": doc.Integer("record's id"), "IOT_ID": doc.Integer("iot's id"), "air_temp": doc.Float("air temperature"), "air_hum": doc.Float("air hum"), "dirt_hum": doc.Float("dirt hum"), "ts": doc.DateTime("timestamp") }, content_type="application/json") async def get_record(request): result = None with db_conn.cursor() as cur: sql = "SELECT * FROM `record` LIMIT 100" cur.execute(sql) result = cur.fetchall() for line in result: line["ts"] = line["ts"].strftime("%Y-%m-%d %H:%M:%S") return json(result)
class ZooniverseClassificationReport: event = doc.Integer("Event Number", required=True) beam = doc.Integer("beam number", required=True) ml_prediction = doc.Float("zooniverse ML prediction", required=True) retired = doc.Boolean("retired") t0_astro = doc.Float("t0_astro") t1_blank = doc.Float("t1_blank") t1_overlapping = doc.Float("t1_overlapping") t1_repeating = doc.Float("t1_repeating") t0_rfi = doc.Float("t0_rfi") t0_cant_answer = doc.Float("t0_cant-answer") t1_something_weird = doc.Float("t0_something-weird") t0_total = doc.Float("t0_total") t1_total = doc.Float("t1_total") t0_astro_fraction = doc.Float("t0_astro_fraction") t0_rfi_fraction = doc.Float("t0_rfi_fraction") t0_cant_answer_fraction = doc.Float("t0_cant-answer_fraction") t1_blank_fraction = doc.Float("t1_blank_fraction") t1_overlapping_fraction = doc.Float("t1_overlapping_fraction") t1_repeating_fraction = doc.Float("t1_repeating_fraction") t1_something_weird_fraction = doc.Float("t1_something-weird_fraction")
"type": "integer", "format": "int64" }), (doc.Integer(), { "type": "integer", "format": "int64" }), (float, { "type": "number", "format": "double" }), (doc.Float, { "type": "number", "format": "double" }), (doc.Float(), { "type": "number", "format": "double" }), (str, { "type": "string" }), (doc.String, { "type": "string" }), (doc.String(), { "type": "string" }), (bool, { "type": "boolean" }),