class AuthorDTO: api = api.namespace('authors', description='Operations related to authors.') base = api.model( "Author_Base", { 'name': fields.String(description='The name of the author.'), }) create = api.clone( "Author_Create", base, { 'series_ids': fields.List( fields.Integer(required=False, description="The author's series.")) }) update = api.clone('Author_Update', create, { 'id': fields.Integer(description='The ID of the book.'), }) fk_books = api.model( "Author_Books", { 'id': fields.Integer(description='The ID of the book.'), 'title': fields.String(description='The title of the book.'), 'description': fields.String(required=True, description='The description of the book.'), }) fk_series = api.model( "Author_Books", { 'id': fields.Integer(description='The ID of the series.'), 'title': fields.String(required=True, description='The title of the series.'), 'description': fields.String(required=True, description='The description of the series.') }) list = api.model( 'Author_List', { 'id': fields.Integer(description='The ID of the author.'), 'name': fields.String(description='The name of the author.'), 'books': fields.Nested(fk_books), 'series': fields.Nested(fk_series) }) query = api.model( 'Author_Query', { 'items': fields.List(fields.Nested(list)), 'total': fields.Integer(), 'page': fields.Integer(), 'per_page': fields.Integer() })
class ServiceUpdateSpec: model = api.model('ServiceUpdateMetadata', { 'selector': fields.Nested(ServiceUpdateSelector.model), }) def __init__(self, spec): self.selector = ServiceUpdateSelector(spec['selector'])
class PodUpdateDto: model = api.model('PodUpdate', { 'metadata': fields.Nested(PodUpdateMetadata.model), }) def __init__(self, pod): self.metadata = PodUpdateMetadata(pod['metadata'])
class NamespaceMetadata: model = api.model('NamespaceMetadata', { 'name': fields.String(), }) def __init__(self, metadata): self.name = metadata['name']
class ServiceUpdateDto: model = api.model('ServiceUpdate', { 'spec': fields.Nested(ServiceUpdateSpec.model), }) def __init__(self, service): self.spec = ServiceUpdateSpec(service['spec'])
class PodInputSpec: model = api.model( 'PodInputSpec', {'containers': fields.List(fields.Nested(PodInputContainer.model))}) def __init__(self, spec): self.containers = spec['containers']
class PodInputMetadata: model = api.model('PodInputMetadata', { 'labels': fields.Nested(PodInputLabel.model), }) def __init__(self, metadata): self.labels = PodInputLabel(metadata['labels'])
class ServiceMetadata: model = api.model('ServiceMetadata', { 'name': fields.String(), 'namespace': fields.String() }) def __init__(self, metadata): self.name = metadata['name'] self.namespace = metadata['namespace']
class ServiceInputSpec: model = api.model( 'ServiceInputSpec', { 'selector': fields.Nested(ServiceInputSelector.model), 'ports': fields.List(fields.Nested(ServiceInputPort.model)) }) def __init__(self, spec): self.containers = spec['selector'] self.ports = [ServiceInputPort(port) for port in spec['ports']]
class PodInputDto: model = api.model( 'PodInput', { 'metadata': fields.Nested(PodInputMetadata.model), 'spec': fields.Nested(PodInputSpec.model), }) def __init__(self, pod): self.metadata = PodInputMetadata(pod['metadata']) self.spec = PodInputSpec(pod['spec'])
class PodLabel: model = api.model( 'PodLabel', { 'app': fields.String() } ) def __init__(self, labels): self.app = labels['app']
class ListOfResourcesDto: model = api.model( 'ListOfResources', { 'resources': fields.List(fields.String()), } ) def __init__(self, resources): self.resources = [resource['metadata']['name'] for resource in resources['items']]
class PodInputContainer: model = api.model( 'PodInputContainer', { 'name': fields.String(), 'image': fields.String(), 'ports': fields.List(fields.Nested(PodInputPort.model)) }) def __init__(self, container): self.name = container['name'] self.image = container['image'] self.ports = [PodInputPort(port) for port in container['ports']]
class ServicePort: model = api.model( 'ServicePort', { 'port': fields.String(), 'protocol': fields.String(), 'target_port': fields.String() }) def __init__(self, port): self.port = port['port'] self.protocol = port['protocol'] self.target_port = port['targetPort']
class PodContainerStatus: model = api.model( 'PodContainerStatus', { 'ready': fields.String(), 'image': fields.String() } ) def __init__(self, container_status): self.ready = container_status['ready'] self.image = container_status['image']
class GenreDTO: api = api.namespace('genres', description='Operations related to genres.') base = api.model( 'Genre_Base', { 'name': fields.String(required=True, description='The name of the genre.'), }) create = api.clone( 'Genre_Update', base, { 'books_ids': fields.List( fields.Integer(required=False, description="The genre books.")), }) update = api.clone( 'Genre_Create', create, { 'id': fields.Integer(description='The ID of the genre.'), }) fk_books = api.model( 'Genre_Books', { 'id': fields.Integer(description='The ID of the book.'), 'title': fields.String(description='The title of the book.'), }) list = api.clone( 'Genre_List', base, { 'id': fields.Integer(description='The ID of the genre.'), 'books': fields.Nested(fk_books) }) query = api.model( 'Genre_Query', { 'items': fields.List(fields.Nested(list)), 'total': fields.Integer(), 'page': fields.Integer(), 'per_page': fields.Integer() })
class ServiceDto: model = api.model( 'Service', { 'metadata': fields.Nested(ServiceMetadata.model), 'kind': fields.String(), 'spec': fields.Nested(ServiceSpec.model), 'api_version': fields.String() }) def __init__(self, service): self.metadata = ServiceMetadata(service['metadata']) self.kind = service['kind'] self.spec = ServiceSpec(service['spec']) self.api_version = service['apiVersion']
class ServiceSpec: model = api.model( 'ServiceSpec', { 'selector': fields.Nested(ServiceSelector.model), 'cluster_ip': fields.String(), 'type': fields.String(), 'ports': fields.List(fields.Nested(ServicePort.model)) }) def __init__(self, spec): self.selector = ServiceSelector(spec['selector']) self.cluster_ip = spec['clusterIP'] self.type = spec['type'] self.ports = [ServicePort(port) for port in spec['ports']]
class PodMetadata: model = api.model( 'PodMetadata', { 'name': fields.String(), 'labels': fields.Nested(PodLabel.model), 'namespace': fields.String() } ) def __init__(self, metadata): self.name = metadata['name'] self.labels = PodLabel(metadata['labels']) self.namespace = metadata['namespace']
class PodStatus: model = api.model( 'PodStatus', { 'phase': fields.String(), 'container_statuses': fields.List(fields.Nested(PodContainerStatus.model)), 'pod_ip': fields.String(), 'host_ip': fields.String() } ) def __init__(self, status): self.phase = status['phase'] self.container_statuses = [PodContainerStatus(container) for container in status['containerStatuses']] self.pod_ip = status['podIP'] self.host_ip = status['hostIP']
class NamespaceDto: model = api.model( 'Namespace', { 'metadata': fields.Nested(NamespaceMetadata.model), 'status': fields.String(), 'kind': fields.String(), 'spec': fields.Nested(NamespaceSpec.model), 'api_version': fields.String() }) def __init__(self, namespace): self.metadata = NamespaceMetadata(namespace['metadata']) self.status = namespace['status'] self.kind = namespace['kind'] self.spec = NamespaceSpec(namespace['spec']) self.api_version = namespace['apiVersion']
class PodSpec: model = api.model( 'PodSpec', { 'service_account': fields.String(), 'node_name': fields.String(), 'security_context': fields.String(), 'service_account_name': fields.String() } ) def __init__(self, spec): self.service_account = spec['serviceAccount'] self.node_name = spec['nodeName'] self.security_context = spec['securityContext'] self.service_account_name = spec['serviceAccountName']
class PodDto: model = api.model( 'Pod', { 'metadata': fields.Nested(PodMetadata.model), 'status': fields.Nested(PodStatus.model), 'kind': fields.String(), 'spec': fields.Nested(PodSpec.model), 'api_version': fields.String() } ) def __init__(self, pod): self.metadata = PodMetadata(pod['metadata']) self.status = PodStatus(pod['status']) self.kind = pod['kind'] self.spec = PodSpec(pod['spec']) self.api_version = pod['apiVersion']
class ServiceSelector: model = api.model('ServiceSelector', {'app': fields.String()}) def __init__(self, selector): self.app = selector['app']
from main import api, Resource, fields from accessToken import token import requests #requests library import os import password #file containing the generated password import timeformat #file containing the function to generate timestamp in the following format YYYYMMDDHHmmss generated_password = password.the_decoded_password generated_timestamp = timeformat.the_formatted_time # create a namespace for the stk resource ns_stkpush = api.namespace('stkpush', description="MPESA STK PUSH") stk_payload = api.model('Stk', { "phone_number": fields.String, "amount": fields.String }) @ns_stkpush.route('') class Stkpush(Resource): @api.expect(stk_payload) def post(self): """Use this API endpoint to initiate online payment on behalf of a customer.""" # get the payload data = api.payload # format the phone number in the right order formated_number = '254{}'.format(data['phone_number'][1:])
class BookDTO: api = api.namespace('books', description='Operations related to books.') base = api.model( 'Book_Base', { 'title': fields.String(required=True, description='The title of the book.'), 'description': fields.String(required=True, description='The description of the book.'), 'start_date': fields.DateTime(description='The start date of the reading.'), 'end_date': fields.DateTime(description='The end date of the reading.') }) create = api.clone( 'Book_Update', base, { 'genre_ids': fields.List(fields.Integer(required=False)), 'author_id': fields.Integer(required=True, description="The book's author."), 'series_id': fields.Integer(required=False, description="The book's series.") }) update = api.clone('Book_Create', create, { 'id': fields.Integer(description='The ID of the book.'), }) fk_author = api.model( 'Book_Author', { 'id': fields.Integer(description='The ID of the author.'), 'name': fields.String(description='The name of the author.'), }) fk_genre = api.model( 'Book_Genre', { 'id': fields.Integer(description='The ID of the genre.'), 'name': fields.String(description='The name of the genre.'), }) fk_series = api.model( 'Book_Series', { 'id': fields.Integer(description='The ID of the series.'), 'title': fields.String(description='The name of the series.'), 'description': fields.String(description='The description of the series.'), }) list = api.clone( 'Book_List', base, { 'id': fields.Integer(description='The ID of the book.'), 'author': fields.Nested(fk_author), 'series': fields.Nested(fk_series, allow_null=True), 'genres': fields.Nested(fk_genre) }) query = api.model( 'Book_Query', { 'items': fields.List(fields.Nested(list)), 'total': fields.Integer(), 'page': fields.Integer(), 'per_page': fields.Integer() })
from main import api, fields, Resource, jwt_required, get_jwt_identity from models.tasksmodel import TaskModel, task_schema, tasks_schema from models.usersmodel import UserModel ns_tasks = api.namespace('tasks', description="All Task Operations") # models Documenting the nature of your Payload task_model = api.model( 'Task', { 'title': fields.String(), 'description': fields.String(), 'completed': fields.String() }) @ns_tasks.route('') class Tasks(Resource): @jwt_required def get(self): """Use this Endpoint to get all Tasks""" uid = get_jwt_identity() user = UserModel.get_userby_id(uid) user_tasks = user.tasks return tasks_schema.dump(user_tasks), 200 @api.expect(task_model) @jwt_required def post(self): """Use this Endpoint to add all Tasks""" data = api.payload
from main import Resource, api, fields, create_access_token from models.usermodel import UserModel, user_schema, users_schema from werkzeug.security import generate_password_hash ns_registration = api.namespace('registration', description='User sign up') ns_login = api.namespace('login', description='Login details') login_model = api.model('Login_credentials', { 'email': fields.String(), 'password': fields.String() }) registration_model = api.model( 'Signup_credentials', { 'full_name': fields.String(), 'email': fields.String(), 'password': fields.String() }) @ns_login.route('') class Login(Resource): @api.expect(login_model) def post(self): ''' use this to authenticate users''' data = api.payload email = data['email'] if UserModel.check_if_mail_exists(email): if UserModel.check_passoword(data['password'], email): user_id = UserModel.get_user_id(email) access_token = create_access_token(identity=user_id)
from main import api from flask_restx import fields progress_model = api.model( 'progress_model', { 'user_id': fields.String(description='IdP provided user-id', readonly=True), 'track_date': fields.Date( description='Date of tracking', required=True, format='%Y-%m-%d'), 'weight': fields.Float(description='Current weight', required=True), 'mood': fields.String(description='Mood value. ' 'Can be one of the following' ' (neutral, bad, good)', required=True, default="neutral"), 'diet': fields.String(description='Diet value. ' 'Can be one of the following ' '(neutral, bad, good)', required=True, default="neutral") }) progress_list_model = api.model( 'progress_list_model', { 'user_id': fields.String(description='IdP provided user-id'), 'progresses': fields.List(fields.Nested(progress_model), default=[]), 'count': fields.Integer(readonly=True, description='Progress counts')
from flask_restplus import fields from main import api # income service incomeStructure = api.model( 'Income', { 'id': fields.Integer, 'name': fields.String, 'amount': fields.String, 'date': fields.DateTime, 'usernumber': fields.String, 'barcodeId': fields.Integer, 'categoryId': fields.Integer }) categoryStructure = api.model('Category', { 'id': fields.Integer, 'name': fields.String, 'budget': fields.Float }) barcodeStructure = api.model( 'Barcode', { 'id': fields.Integer, 'code': fields.String, 'productName': fields.String, 'amount': fields.Float }) # users service userStructure = api.model('user', { 'id': fields.String,