4

How to set the default value for a path variable with Flask restplus?

For example, with the below:

@api.route('/calendars/coordinates/<string:latitude>/<string:longitude>/years/<string:year>')
class DailyCalendarHandler(Resource):
  get_parser = reqparse.RequestParser()
  get_parser.add_argument('timezone', type=str, default='Asia/Calcutta', help='Example: Asia/Calcutta', location='args', required=True)
  get_parser.add_argument('encoding', type=str, default='devanagari', help='Example: iast, devanagari, kannada, tamil', location='args',
                          required=True)

  @api.expect(get_parser)
  def get(self, latitude, longitude, year):
    args = self.get_parser.parse_args()
    city = City("", latitude, longitude, args['timezone'])

How do I set default for year, latitude and longitude?

If it were a request variable, one could do the below:

get_parser = reqparse.RequestParser()
get_parser.add_argument('output_transliteration', location='args', default='devanagari', help='devanagari/slp1/iast/hk/wx/itrans/kolkata/velthuis')
vishvAs vAsuki
  • 2,421
  • 2
  • 18
  • 19

1 Answers1

2

You can define multiple URL rules above your class and define defaults for arguments. Something like:

@api.route('/calendars/coordinates', defaults={'latitude':123, 'longitude': 56})
@api.route('/calendars/latitude/<string:latitude>/longitude/<string:longitude>/years/<string:year>')
    class DailyCalendarHandler(Resource):

  def get(self, latitude, longitude, year='2017'):
Meir Tseitlin
  • 1,878
  • 2
  • 17
  • 28
  • 1
    While this method works in terms of API calls with `curl` ... it really bugs me that the generated swagger ui still displays the second parameter with a `*required` tag. – Debanjan Basu Sep 27 '18 at 08:06
  • @DebanjanBasu Path parameters are always required. You can't document a default value for them. There needs to be separate routes. – Jérôme Apr 14 '19 at 19:06