# Copyright (c) 2019 SUSE LLC. All rights reserved. # # This file is part of mash. # # mash is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # mash is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with mash. If not, see <http://www.gnu.org/licenses/> # from flask_restx import Namespace from mash.services.api.v1.schema.accounts import delete_account api = Namespace( 'accounts', description='Account related operations' ) delete_account_request = api.schema_model( 'delete_account_request', delete_account )
from CTFd.utils.decorators import admins_only files_namespace = Namespace("files", description="Endpoint to retrieve Files") FileModel = sqlalchemy_to_pydantic(Files) class FileDetailedSuccessResponse(APIDetailedSuccessResponse): data: FileModel class FileListSuccessResponse(APIListSuccessResponse): data: List[FileModel] files_namespace.schema_model("FileDetailedSuccessResponse", FileDetailedSuccessResponse.apidoc()) files_namespace.schema_model("FileListSuccessResponse", FileListSuccessResponse.apidoc()) @files_namespace.route("") class FilesList(Resource): @admins_only @files_namespace.doc( description="Endpoint to get file objects in bulk", responses={ 200: ("Success", "FileListSuccessResponse"), 400: ( "An error occured processing the provided or stored data", "APISimpleErrorResponse",
from CTFd.utils.user import get_current_user, is_admin hints_namespace = Namespace("hints", description="Endpoint to retrieve Hints") HintModel = sqlalchemy_to_pydantic(Hints) class HintDetailedSuccessResponse(APIDetailedSuccessResponse): data: HintModel class HintListSuccessResponse(APIListSuccessResponse): data: List[HintModel] hints_namespace.schema_model("HintDetailedSuccessResponse", HintDetailedSuccessResponse.apidoc()) hints_namespace.schema_model("HintListSuccessResponse", HintListSuccessResponse.apidoc()) @hints_namespace.route("") class HintList(Resource): @admins_only @hints_namespace.doc( description="Endpoint to list Hint objects in bulk", responses={ 200: ("Success", "HintListSuccessResponse"), 400: ( "An error occured processing the provided or stored data", "APISimpleErrorResponse",
from flask import request from flask_restx import Namespace, Resource, fields, reqparse from src.schemas import payload_validation_failure, internal_server_error from src.code.resume import get_resume api = Namespace("JOResume", description="Get Joe's Resume") payload_val_failed = api.schema_model("payload_val_failed", payload_validation_failure) int_server_error = api.schema_model("internal_server_error", internal_server_error) @api.route("resume") class ClassResumeEndpoint(Resource): """ """ @classmethod @api.response(400, "Payload error", payload_val_failed) @api.response(500, "Internal Server Error", int_server_error) def get(cls): """ """ resp = get_resume.get_resume() return resp
unlocks_namespace = Namespace("unlocks", description="Endpoint to retrieve Unlocks") UnlockModel = sqlalchemy_to_pydantic(Unlocks) TransientUnlockModel = sqlalchemy_to_pydantic(Unlocks, exclude=["id"]) class UnlockDetailedSuccessResponse(APIDetailedSuccessResponse): data: UnlockModel class UnlockListSuccessResponse(APIListSuccessResponse): data: List[UnlockModel] unlocks_namespace.schema_model("UnlockDetailedSuccessResponse", UnlockDetailedSuccessResponse.apidoc()) unlocks_namespace.schema_model("UnlockListSuccessResponse", UnlockListSuccessResponse.apidoc()) @unlocks_namespace.route("") class UnlockList(Resource): @admins_only @unlocks_namespace.doc( description="Endpoint to get unlock objects in bulk", responses={ 200: ("Success", "UnlockListSuccessResponse"), 400: ( "An error occured processing the provided or stored data", "APISimpleErrorResponse",
def test_schema_model(self): api = Namespace('test') api.schema_model('Person', {}) assert 'Person' in api.models
submissions_namespace = Namespace( "submissions", description="Endpoint to retrieve Submission") SubmissionModel = sqlalchemy_to_pydantic(Submissions) TransientSubmissionModel = sqlalchemy_to_pydantic(Submissions, exclude=["id"]) class SubmissionDetailedSuccessResponse(APIDetailedSuccessResponse): data: SubmissionModel class SubmissionListSuccessResponse(PaginatedAPIListSuccessResponse): data: List[SubmissionModel] submissions_namespace.schema_model("SubmissionDetailedSuccessResponse", SubmissionDetailedSuccessResponse.apidoc()) submissions_namespace.schema_model("SubmissionListSuccessResponse", SubmissionListSuccessResponse.apidoc()) @submissions_namespace.route("") class SubmissionsList(Resource): @admins_only @submissions_namespace.doc( description="Endpoint to get submission objects in bulk", responses={ 200: ("Success", "SubmissionListSuccessResponse"), 400: ( "An error occured processing the provided or stored data", "APISimpleErrorResponse",
Contains a single Ping endpoint which is used for sanity checks and demonstrating how a simple endpoint should be implemented. This file contains most of the documentation of what the ping endpoint does. - Implements the JSON-schema's for validation - Provides descriptions for each endpoint - Implements the call for each endpoint to its relevant *_composer.py file """ __author__ = 'Martijn Gevaert' from flask_restx import Namespace, Resource, fields from json_schemas import base_schemas from .ping_composer import PingComposer pingComposer = PingComposer() ns = Namespace('ping', description='Provides a simple ping, usefull during development', validate=True) pingGETresponse_schema = ns.schema_model( "pingGETresponse", base_schemas.pingGETresponse) @ns.route("/") class MainClass(Resource): @ns.doc(description='Returns a ping message') @ns.response(200, 'Succes', pingGETresponse_schema) def get(self): return pingComposer.GET()
aliyun_account_update ) from mash.services.api.v1.utils.accounts.aliyun import ( create_aliyun_account, get_aliyun_account, get_aliyun_accounts, delete_aliyun_account, update_aliyun_account ) from mash.services.database.routes.accounts.aliyun import aliyun_account_response api = Namespace( 'Aliyun Accounts', description='Aliyun account operations' ) add_aliyun_account_request = api.schema_model('aliyun_account', add_account_aliyun) update_aliyun_account_request = api.schema_model( 'aliyun_account_update', aliyun_account_update ) validation_error_response = api.schema_model( 'validation_error', validation_error ) api.models['aliyun_account_response'] = aliyun_account_response @api.route('/') @api.doc(security='apiKey') @api.response(401, 'Unauthorized', default_response) @api.response(422, 'Not processable', default_response)
def test_schema_model(self): api = Namespace("test") api.schema_model("Person", {}) assert "Person" in api.models
from flask import request, jsonify, make_response, current_app from flask_restx import Namespace, Resource from flask_jwt_extended import jwt_required, get_jwt_identity from mash.services.api.v1.schema import (default_response, validation_error, job_list) from mash.services.api.v1.utils.jobs import delete_job, get_job, get_jobs from mash.services.database.routes.jobs import job_response, job_data api = Namespace('Jobs', description='Job operations') api.models['job_data'] = job_data api.models['job_response'] = job_response job_list_request = api.schema_model('job_list_request', job_list) validation_error_response = api.schema_model('validation_error', validation_error) @api.route('/') @api.doc(security='apiKey') @api.response(401, 'Unauthorized', default_response) @api.response(422, 'Not processable', default_response) class JobList(Resource): """ Handles list jobs. """ @api.doc('get_jobs') @jwt_required
from flask import request from flask_restx import Resource, Namespace, reqparse from app.main.collectors_item.services import collectors_item_service from app.main.collectors_item.schemas import collectors_item_schema from app.utils import loggers logger = loggers.get_basic_logger(__name__) api = Namespace("collectors_items", description="Represents the collectors_items") get_collectors_items_dto = api.schema_model( "GetCollectorsItems", collectors_item_schema.get_collectors_items_schema) get_collectors_item_dto = api.schema_model( "GetCollectorsItem", collectors_item_schema.get_collectors_item_schema) create_collectors_item_dto = api.schema_model( "CreateCollectorsItem", collectors_item_schema.create_collectors_item_schema) update_collectors_item_dto = api.schema_model( "UpdateCollectorsItem", collectors_item_schema.update_collectors_item_schema) @api.route("") class CollectorsItems(Resource): @api.doc("Get collectors_items") @api.response(200, "Success", get_collectors_items_dto) def get(self): return collectors_item_service.get_collectors_items()
comments_namespace = Namespace("comments", description="Endpoint to retrieve Comments") CommentModel = sqlalchemy_to_pydantic(Comments) class CommentDetailedSuccessResponse(APIDetailedSuccessResponse): data: CommentModel class CommentListSuccessResponse(APIListSuccessResponse): data: List[CommentModel] comments_namespace.schema_model("CommentDetailedSuccessResponse", CommentDetailedSuccessResponse.apidoc()) comments_namespace.schema_model("CommentListSuccessResponse", CommentListSuccessResponse.apidoc()) def get_comment_model(data): model = Comments if "challenge_id" in data: model = ChallengeComments elif "user_id" in data: model = UserComments elif "team_id" in data: model = TeamComments elif "page_id" in data: model = PageComments
from datetime import timedelta from flask import request from flask_restx import Namespace, Resource import jsonref from .schema_models import Token, UserCreation, UserLogin from .security import ACCESS_TOKEN_EXPIRE_MINUTES, create_user, fake_users_db, create_access_token, authenticate_user api = Namespace('user', description='Users related operations') token_schema = api.schema_model("Token", jsonref.loads(Token.schema_json())) login_user_schema = api.schema_model("UserLogin", jsonref.loads(UserLogin.schema_json())) user_creationg_schema = api.schema_model( "User", jsonref.loads(UserCreation.schema_json())) @api.route("/token") class TokenResource(Resource): @api.doc('get_jwt_token') @api.expect(login_user_schema, validate=True) @api.response(201, "this is my response of token", token_schema) def post(self): data = request.get_json() username = data['username'] password = data['password'] user = authenticate_user(fake_users_db, username, password) if not user: return "bad request", 401
awards_namespace = Namespace("awards", description="Endpoint to retrieve Awards") AwardModel = sqlalchemy_to_pydantic(Awards) class AwardDetailedSuccessResponse(APIDetailedSuccessResponse): data: AwardModel class AwardListSuccessResponse(APIListSuccessResponse): data: List[AwardModel] awards_namespace.schema_model("AwardDetailedSuccessResponse", AwardDetailedSuccessResponse.apidoc()) awards_namespace.schema_model("AwardListSuccessResponse", AwardListSuccessResponse.apidoc()) @awards_namespace.route("") class AwardList(Resource): @admins_only @awards_namespace.doc( description="Endpoint to list Award objects in bulk", responses={ 200: ("Success", "AwardListSuccessResponse"), 400: ( "An error occured processing the provided or stored data", "APISimpleErrorResponse",
class CitiesTransportsList(Resource): @api.doc('list_transports') def get(self): """Fetch all cities transports""" response = requests.get('https://tr.transport.data.gouv.fr/') return jsonify(response.json()) transport = api.schema_model( 'transport', { 'bus': [{ 'id': fields.Integer, 'trip_id': fields.String, 'vehicle_id': fields.Integer, 'position': { 'latitude': fields.Integer, 'longitude': fields.Integer, }, 'speed': fields.String, }], 'metro': [{ 'id': fields.Integer, }], }) @api.route('/<city>') @api.param('city', 'City of transport data') @api.response(200, transport) @api.response(404, 'City not found') class CityTransportsList(Resource): @api.doc('get_transport_type')
flags_namespace = Namespace("flags", description="Endpoint to retrieve Flags") FlagModel = sqlalchemy_to_pydantic(Flags) class FlagDetailedSuccessResponse(APIDetailedSuccessResponse): data: FlagModel class FlagListSuccessResponse(APIListSuccessResponse): data: List[FlagModel] flags_namespace.schema_model( "FlagDetailedSuccessResponse", FlagDetailedSuccessResponse.apidoc() ) flags_namespace.schema_model( "FlagListSuccessResponse", FlagListSuccessResponse.apidoc() ) @flags_namespace.route("") class FlagList(Resource): @admins_only @flags_namespace.doc( description="Endpoint to list Flag objects in bulk", responses={ 200: ("Success", "FlagListSuccessResponse"), 400: (
from flask import jsonify, request, make_response, current_app from flask_restx import Namespace, Resource from flask_jwt_extended import (jwt_required, get_jwt_identity) from mash.services.api.v1.schema import (default_response, validation_error) from mash.services.api.v1.schema.accounts.gce import (add_account_gce, gce_account_update) from mash.services.api.v1.utils.accounts.gce import (create_gce_account, get_gce_account, get_gce_accounts, delete_gce_account, update_gce_account) from mash.services.database.routes.accounts.gce import gce_account_response api = Namespace('GCE Accounts', description='GCE account operations') add_gce_account_request = api.schema_model('gce_account', add_account_gce) update_gce_account_request = api.schema_model('gce_account_update', gce_account_update) validation_error_response = api.schema_model('validation_error', validation_error) api.models['gce_account_response'] = gce_account_response @api.route('/') @api.doc(security='apiKey') @api.response(401, 'Unauthorized', default_response) @api.response(422, 'Not processable', default_response) class GCEAccountCreateAndList(Resource): """ Handles list accounts and create accounts for GCE.
from flask_restx import fields from flask_restx import Namespace, Resource from flask_restx import reqparse from flask_restx import fields, marshal import json from myapi import api ns = Namespace('res', description='Reservations') Return1 = ns.schema_model('Return1', { 'session': 'string', 'message': 'string', 'retcode': 'string' }) Category = api.model('Category', { 'id': fields.Integer(description='Category ID'), 'name': fields.String(description='Category Name'), 'count': fields.Integer(description='Number of hosts') }) Reservation = api.model('Reservation', { 'description': fields.String(description='notes'), 'start': fields.DateTime(dt_format='iso8601', description='from...'), 'time': fields.Integer(description='minutes'), 'category_id': fields.Integer(description='Category'),
from flask import jsonify, request, make_response, current_app from flask_restx import Namespace, Resource from flask_jwt_extended import (jwt_required, get_jwt_identity) from mash.services.api.v1.schema import (add_account, default_response, validation_error, password_change, password_reset) from mash.services.api.v1.utils.users import (add_user, get_user_by_id, delete_user, reset_user_password, change_user_password) from mash.services.database.routes.users import user_response api = Namespace('User', description='User operations') add_account_request = api.schema_model('add_account_request', add_account) validation_error_response = api.schema_model('validation_error', validation_error) password_reset_request = api.schema_model('password_reset_request', password_reset) password_change_request = api.schema_model('password_change_request', password_change) api.models['user_response'] = user_response api.models['default_response'] = default_response @api.route('/') class Account(Resource): @api.doc('create_mash_account') @api.expect(add_account_request)
teams_namespace = Namespace("teams", description="Endpoint to retrieve Teams") TeamModel = sqlalchemy_to_pydantic(Teams) TransientTeamModel = sqlalchemy_to_pydantic(Teams, exclude=["id"]) class TeamDetailedSuccessResponse(APIDetailedSuccessResponse): data: TeamModel class TeamListSuccessResponse(PaginatedAPIListSuccessResponse): data: List[TeamModel] teams_namespace.schema_model( "TeamDetailedSuccessResponse", TeamDetailedSuccessResponse.apidoc() ) teams_namespace.schema_model( "TeamListSuccessResponse", TeamListSuccessResponse.apidoc() ) @teams_namespace.route("") class TeamList(Resource): @check_account_visibility @teams_namespace.doc( description="Endpoint to get Team objects in bulk", responses={ 200: ("Success", "TeamListSuccessResponse"), 400: (
ValuelessTokenModel = sqlalchemy_to_pydantic(Tokens, exclude=["value"]) class TokenDetailedSuccessResponse(APIDetailedSuccessResponse): data: TokenModel class ValuelessTokenDetailedSuccessResponse(APIDetailedSuccessResponse): data: ValuelessTokenModel class TokenListSuccessResponse(APIListSuccessResponse): data: List[TokenModel] tokens_namespace.schema_model("TokenDetailedSuccessResponse", TokenDetailedSuccessResponse.apidoc()) tokens_namespace.schema_model( "ValuelessTokenDetailedSuccessResponse", ValuelessTokenDetailedSuccessResponse.apidoc(), ) tokens_namespace.schema_model("TokenListSuccessResponse", TokenListSuccessResponse.apidoc()) @tokens_namespace.route("") class TokenList(Resource): @require_verified_emails @authed_only @tokens_namespace.doc(
users_namespace = Namespace("users", description="Endpoint to retrieve Users") UserModel = sqlalchemy_to_pydantic(Users) TransientUserModel = sqlalchemy_to_pydantic(Users, exclude=["id"]) class UserDetailedSuccessResponse(APIDetailedSuccessResponse): data: UserModel class UserListSuccessResponse(PaginatedAPIListSuccessResponse): data: List[UserModel] users_namespace.schema_model("UserDetailedSuccessResponse", UserDetailedSuccessResponse.apidoc()) users_namespace.schema_model("UserListSuccessResponse", UserListSuccessResponse.apidoc()) @users_namespace.route("") class UserList(Resource): @check_account_visibility @users_namespace.doc( description="Endpoint to get User objects in bulk", responses={ 200: ("Success", "UserListSuccessResponse"), 400: ( "An error occured processing the provided or stored data", "APISimpleErrorResponse",
pages_namespace = Namespace("pages", description="Endpoint to retrieve Pages") PageModel = sqlalchemy_to_pydantic(Pages) TransientPageModel = sqlalchemy_to_pydantic(Pages, exclude=["id"]) class PageDetailedSuccessResponse(APIDetailedSuccessResponse): data: PageModel class PageListSuccessResponse(APIListSuccessResponse): data: List[PageModel] pages_namespace.schema_model("PageDetailedSuccessResponse", PageDetailedSuccessResponse.apidoc()) pages_namespace.schema_model("PageListSuccessResponse", PageListSuccessResponse.apidoc()) @pages_namespace.route("") @pages_namespace.doc(responses={ 200: "Success", 400: "An error occured processing your data" }) class PageList(Resource): @admins_only @pages_namespace.doc( description="Endpoint to get page objects in bulk", responses={
NotificationModel = sqlalchemy_to_pydantic(Notifications) TransientNotificationModel = sqlalchemy_to_pydantic(Notifications, exclude=["id"]) class NotificationDetailedSuccessResponse(APIDetailedSuccessResponse): data: NotificationModel class NotificationListSuccessResponse(APIListSuccessResponse): data: List[NotificationModel] notifications_namespace.schema_model( "NotificationDetailedSuccessResponse", NotificationDetailedSuccessResponse.apidoc()) notifications_namespace.schema_model("NotificationListSuccessResponse", NotificationListSuccessResponse.apidoc()) @notifications_namespace.route("") class NotificantionList(Resource): @notifications_namespace.doc( description="Endpoint to get notification objects in bulk", responses={ 200: ("Success", "NotificationListSuccessResponse"), 400: ( "An error occured processing the provided or stored data", "APISimpleErrorResponse",
__author__ = 'Martijn Gevaert' from flask import request from flask_restx import Namespace, Resource, fields from json_schemas import applications_schemas, generic_schemas import logging from .applications_composer import ApplicationsComposer applicationsComposer = ApplicationsComposer() ns = Namespace('applications', description='Resources related to application management', validate=True) successANYresponse_schema = ns.schema_model("successANYresponse", generic_schemas.successANYresponse) containersGETresponse_schema = ns.schema_model( "containersGETresponse", applications_schemas.containersGETresponse) containersPOST_schema = ns.schema_model("containersPOST", applications_schemas.containersPOST) @ns.route("/containers") class Containers(Resource): @ns.response(200, 'Success', containersGETresponse_schema) @ns.doc( description='Returns a list with data for each container on the system' ) def get(self): return applicationsComposer.containers_GET()
challenges_namespace = Namespace("challenges", description="Endpoint to retrieve Challenges") ChallengeModel = sqlalchemy_to_pydantic(Challenges) TransientChallengeModel = sqlalchemy_to_pydantic(Challenges, exclude=["id"]) class ChallengeDetailedSuccessResponse(APIDetailedSuccessResponse): data: ChallengeModel class ChallengeListSuccessResponse(APIListSuccessResponse): data: List[ChallengeModel] challenges_namespace.schema_model("ChallengeDetailedSuccessResponse", ChallengeDetailedSuccessResponse.apidoc()) challenges_namespace.schema_model("ChallengeListSuccessResponse", ChallengeListSuccessResponse.apidoc()) @challenges_namespace.route("") class ChallengeList(Resource): @check_challenge_visibility @during_ctf_time_only @require_verified_emails @challenges_namespace.doc( description="Endpoint to get Challenge objects in bulk", responses={ 200: ("Success", "ChallengeListSuccessResponse"), 400: (
import json from flask import jsonify, request, make_response, current_app from flask_restx import Namespace, Resource from flask_jwt_extended import jwt_required, get_jwt_identity from mash.mash_exceptions import MashException from mash.services.api.v1.schema import (default_response, validation_error) from mash.services.api.v1.routes.jobs import job_response from mash.services.api.v1.schema.jobs.ec2 import ec2_job_message from mash.services.api.v1.utils.jobs.ec2 import validate_ec2_job from mash.services.api.v1.utils.jobs import create_job api = Namespace('EC2 Jobs', description='EC2 Job operations') ec2_job = api.schema_model('ec2_job', ec2_job_message) validation_error_response = api.schema_model('validation_error', validation_error) @api.route('/') class EC2JobCreate(Resource): @api.doc('add_ec2_job', security='apiKey') @jwt_required @api.expect(ec2_job) @api.response(201, 'Job added', job_response) @api.response(400, 'Validation error', validation_error_response) @api.response(401, 'Unauthorized', default_response) @api.response(422, 'Not processable', default_response) def post(self): """
configs_namespace = Namespace("configs", description="Endpoint to retrieve Configs") ConfigModel = sqlalchemy_to_pydantic(Configs) class ConfigDetailedSuccessResponse(APIDetailedSuccessResponse): data: ConfigModel class ConfigListSuccessResponse(APIListSuccessResponse): data: List[ConfigModel] configs_namespace.schema_model("ConfigDetailedSuccessResponse", ConfigDetailedSuccessResponse.apidoc()) configs_namespace.schema_model("ConfigListSuccessResponse", ConfigListSuccessResponse.apidoc()) @configs_namespace.route("") class ConfigList(Resource): @admins_only @configs_namespace.doc( description="Endpoint to get Config objects in bulk", responses={ 200: ("Success", "ConfigListSuccessResponse"), 400: ( "An error occured processing the provided or stored data", "APISimpleErrorResponse",
from flask import jsonify, request, make_response, current_app from flask_restx import Namespace, Resource from flask_jwt_extended import (jwt_required, get_jwt_identity) from mash.services.api.v1.utils.accounts.ec2 import (create_ec2_account, get_ec2_accounts, get_ec2_account, delete_ec2_account, update_ec2_account) from mash.services.api.v1.schema import (default_response, validation_error) from mash.services.api.v1.schema.accounts.ec2 import (add_account_ec2, ec2_account_update) from mash.services.database.routes.accounts.ec2 import ec2_account_response, region, group api = Namespace('EC2 Accounts', description='EC2 account operations') add_ec2_account_request = api.schema_model('add_ec2_account_request', add_account_ec2) update_ec2_account_request = api.schema_model('ec2_account_update', ec2_account_update) validation_error_response = api.schema_model('validation_error', validation_error) api.models['region'] = region api.models['group'] = group api.models['ec2_account_response'] = ec2_account_response api.models['default_response'] = default_response @api.route('/') @api.doc(security='apiKey') @api.response(401, 'Unauthorized', default_response) @api.response(422, 'Not processable', default_response)