Skip to content

Commit

Permalink
added more complex examples
Browse files Browse the repository at this point in the history
  • Loading branch information
avara1986 committed Jan 20, 2020
1 parent 0ed75f5 commit 38a0cca
Show file tree
Hide file tree
Showing 11 changed files with 287 additions and 75 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,5 @@ ENV/
.mypy_cache/

.idea

pylintReport.txt
db.sqlite3
_build
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ python manage.py runserver

## Check the result

Your default endpoint will be in this url:
Your default endpoints will be in this url:
```bash
http://127.0.0.1:5000/template/
http://127.0.0.1:5000/films/
http://127.0.0.1:5000/actors/
```

This URL is setted in your `config.yml`:
Expand All @@ -44,12 +45,12 @@ ms:
DEBUG: false
TESTING: false
APP_NAME: Template
APPLICATION_ROOT : /template # <!---
APPLICATION_ROOT : "" # <!---
```

You can acceded to a [swagger ui](https://swagger.io/tools/swagger-ui/) in the next url:
```bash
http://127.0.0.1:5000/template/ui/
http://127.0.0.1:5000/ui/
```

This PATH is setted in your `config.yml`:
Expand All @@ -69,10 +70,10 @@ https://microservices-scaffold.readthedocs.io/en/latest/
You can dockerize this microservice wit this steps:
* Create and push the image

docker build -t template -f Dockerfile .
docker build -t films -f Dockerfile .
* Run the image:

docker run -d -p 5000:5000 template
docker run -d -p 5000:5000 films


# Kubernetes
Expand Down
7 changes: 4 additions & 3 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ pyms:
path: "swagger"
file: "swagger.yaml"
url: "/ui/"
project_dir: "project.views"
tracer:
client: "jaeger"
host: "localhost"
component_name: "Python Microservice"
config:
DEBUG: false
TESTING: false
APP_NAME: Template
APPLICATION_ROOT : /template
APP_NAME: Films&Actors
APPLICATION_ROOT : ""
SQLALCHEMY_TRACK_MODIFICATIONS: true
SECRET_KEY: "gjr39dkjn344_!67#"
DATABASE: db.sqlite3
SQLALCHEMY_DATABASE_URI: sqlite:////tmp/db.sqlite3
SQLALCHEMY_DATABASE_URI: sqlite:///db.sqlite3
Binary file added project/db.sqlite3
Binary file not shown.
46 changes: 30 additions & 16 deletions project/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,43 @@

import datetime

from sqlalchemy import Column, Integer, String, DateTime
from sqlalchemy import Column, Integer, String, DateTime, Date, ForeignKey

from project.models.init_db import db


def dump_datetime(value: datetime) -> list:
"""Deserialize datetime object into string form for JSON processing."""
return [value.strftime("%Y-%m-%d"), value.strftime("%H:%M:%S")]


class Color(db.Model):
class Film(db.Model):
"""Example model"""
__tablename__ = 'colors'
__tablename__ = 'films'

id = Column(Integer, primary_key=True, autoincrement=True)
name = Column(String, nullable=False)
pub_date = Column(Date, default=datetime.datetime.now)
timestamp = Column(DateTime, default=datetime.datetime.now)
cast = db.relationship("Actor", secondary="film_cast")


class Actor(db.Model):
__tablename__ = 'actors'

@property
def serialize(self) -> dict:
"""Return object data in easily serializeable format"""
return {
'id': self.id,
'timestamp': dump_datetime(self.timestamp),
'name': self.name,
}
id = Column(Integer, primary_key=True, autoincrement=True)
name = Column(String, nullable=False)
surname = Column(String, nullable=False)
timestamp = Column(DateTime, default=datetime.datetime.now)
films = db.relationship("Film", secondary="film_cast")


class FilmCast(db.Model):
__tablename__ = "film_cast"

film_id = Column(
Integer, ForeignKey(Film.id, ondelete="CASCADE"), primary_key=True
)
actor_id = Column(
Integer,
ForeignKey(Actor.id, ondelete="CASCADE"),
key="id",
primary_key=True,
)
actor = db.relationship(Actor)
film = db.relationship(Film)
18 changes: 14 additions & 4 deletions project/serializers/serializers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
from marshmallow import fields
from marshmallow_sqlalchemy import ModelSchema

from project.models.models import Color
from project.models.models import Film, Actor


class ColorSchema(ModelSchema):
class ActorSchema(ModelSchema):
class Meta:
model = Color
fields = ('id', 'name', 'timestamp')
model = Actor
fields = ('id', 'name', 'surname')


class FilmSchema(ModelSchema):
cast = fields.Nested(ActorSchema, many=True)
pubDate = fields.String(required=True, data_key='pub_date', attribute="pub_date")

class Meta:
model = Film
fields = ('id', 'name', 'pub_date', 'cast')
154 changes: 121 additions & 33 deletions project/swagger/swagger.yaml
Original file line number Diff line number Diff line change
@@ -1,89 +1,177 @@
---
swagger: "2.0"
info:
description: "This is a sample server Color server. You can find out more about Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For this sample, you can use the api key `special-key` to test the authorization filters."
description: "This is a sample API server. See source code in [Python Microservices](https://github.com/python-microservices/microservices-scaffold)"
version: "1.0.0"
title: "Swagger Color list"
termsOfService: "http://swagger.io/terms/"
title: "API of Films and actors"
termsOfService: "https://github.com/python-microservices/microservices-scaffold/"
contact:
email: "apiteam@swagger.io"
email: "a.vara.1986@gmail.com"
license:
name: "Apache 2.0"
url: "http://www.apache.org/licenses/LICENSE-2.0.html"
tags:
- name: "colors"
description: "Everything about your colors"
- name: "Films"
description: "Everything about your films"
externalDocs:
description: "Find out more"
url: "http://swagger.io"
- name: "store"
description: "Example endpoint list of colors"
- name: "user"
description: "Operations about user"
url: "https://github.com/python-microservices/microservices-scaffold"
- name: "Actors"
description: "Everything about your actors"
externalDocs:
description: "Find out more about our store"
url: "http://swagger.io"
description: "Find out more"
url: "https://github.com/python-microservices/microservices-scaffold"
schemes:
- "http"
paths:
/:
/actors:
post:
tags:
- "colors"
summary: "Example endpoint return create a color"
- "Actors"
summary: "Example endpoint return create a actor"
description: ""
operationId: "create_view"
consumes:
- "application/json"
produces:
- "application/json"
parameters:
- in: "body"
name: "name"
description: "Pet object that needs to be added to the store"
name: "actor"
description: "Data of the actor"
required: true
schema:
$ref: "#/definitions/Color"
$ref: "#/definitions/Actor"
responses:
200:
description: "The color created"
description: "The actor created"
schema:
$ref: '#/definitions/Color'
$ref: '#/definitions/Actor'
405:
description: "Invalid input"
x-swagger-router-controller: "project.views.views"
get:
tags:
- "colors"
summary: "Example endpoint return a list of colors by palette"
- "Actors"
summary: "Example endpoint return a list of actors"
description: ""
operationId: "list_view"
consumes:
- "application/json"
produces:
- "application/json"
responses:
200:
description: "A list of colors (may be filtered by palette)"
description: "A list of actors"
schema:
$ref: '#/definitions/Color'
$ref: '#/definitions/Actors'
400:
description: "Invalid ID supplied"
404:
description: "Pet not found"
description: "Actor not found"
405:
description: "Validation exception"
x-swagger-router-controller: "project.views.views"
/films:
post:
tags:
- "Films"
summary: "Example endpoint return create a film"
description: ""
consumes:
- "application/json"
produces:
- "application/json"
parameters:
- in: "body"
name: "payload"
description: "Data of the film"
required: true
schema:
$ref: "#/definitions/Film"
responses:
200:
description: "The film created"
schema:
$ref: '#/definitions/Film'
405:
description: "Invalid input"
get:
tags:
- "Films"
summary: "Example endpoint return a list of films"
description: ""
consumes:
- "application/json"
produces:
- "application/json"
responses:
200:
description: "A list of actors"
schema:
$ref: '#/definitions/Films'
400:
description: "Invalid ID supplied"
404:
description: "Actor not found"
405:
description: "Validation exception"
/films/{id}:
put:
tags:
- "Films"
summary: "Example endpoint return create a film"
description: ""
consumes:
- "application/json"
produces:
- "application/json"
parameters:
- name: "id"
in: "path"
description: "ID of film to return"
required: true
type: "integer"
- in: "body"
name: "payload"
description: "Data of the film"
required: true
schema:
$ref: "#/definitions/Film"
responses:
200:
description: "The film created"
schema:
$ref: '#/definitions/Film'
405:
description: "Invalid input"
definitions:
Color:
Actor:
type: "object"
properties:
id:
type: "integer"
name:
type: "string"
timestamp:
surname:
type: "string"
Actors:
type: "array"
items:
$ref: '#/definitions/Actor'
Film:
type: "object"
properties:
id:
type: "integer"
name:
type: "string"
pubDate:
type: "string"
cast:
type: "array"
items:
$ref: '#/definitions/Actor'
Films:
type: "array"
items:
$ref: '#/definitions/Film'
externalDocs:
description: "Find out more about Swagger"
url: "http://swagger.io"
url: "https://github.com/python-microservices/microservices-scaffold"

0 comments on commit 38a0cca

Please sign in to comment.