-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrp_1.py
More file actions
54 lines (40 loc) · 1.32 KB
/
rp_1.py
File metadata and controls
54 lines (40 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# with marshal and marshmallow
from flask import Flask, request
from flask_restplus import Resource, Api, fields
from marshmallow import Schema, fields as ma_fields, post_load
app = Flask(__name__)
api = Api(app)
class TheLanguage(object):
def __init__(self, language, framework):
self.language = language
self.framework = framework
def __repr__(self):
return '{} is the language. {} is the framework'.format(self.language, self.framework)
class LanguagesSchema(Schema):
language = ma_fields.String()
framework = ma_fields.String()
@post_load
def create_language(self, data):
return TheLanguage(**data)
a_language = api.model('Language',
{
'language' : fields.String('The language.'),
'framework' : fields.String('The framework.')
}
)
languages = []
python = TheLanguage(language='python', framework='Django')
languages.append(python)
@api.route('/language')
class Language(Resource):
def get(self):
schema = LanguagesSchema(many=True)
return schema.dump(languages)
@api.expect(a_language)
def post(self):
schema = LanguagesSchema()
new_language = schema.load(api.payload)
languages.append(new_language.data)
return {'result':'language added.'}
if __name__ == '__main__':
app.run(debug=True)