示例#1
0
文件: views.py 项目: jlegs/django-api
def fizzbuzz(request):
    '''
    Takes a GET request at /api with the parameters 'word' and 'max_value', and returns
    a json-formatted response containing the numbers correlating to the fizzbuzz test for the provided word and 
    max_value, plus a status code.
    '''

    # Set a 400 error first because we change this var fewer times if we do that
    stat = {'code': status.HTTP_400_BAD_REQUEST, 'message': 'error'}

    try:
        word = request.query_params['word']
        max_value = int(request.query_params['max_value'])
    except (MultiValueDictKeyError, ValueError):
        # MultiValueDictKeyError in case the keys we're looking for don't exist in .query_params
        # ValueError in case people use quotes, floats or other junk in the request parameters
        word = None
        max_value = None

    if word in ('fizz', 'buzz', 'fizzbuzz') and max_value > 0: # check for valid param vals here
        stat = {'code': status.HTTP_200_OK, 'message': 'ok'}

    fizzbuzz = FizzBuzz(max_value=max_value, word=word)
    fizzbuzz_dict = fizzbuzz.create_fizzbuzz_dict()
    fizzbuzz_dict['status'] = stat['message']

    return Response(json.dumps(fizzbuzz_dict), status=stat['code'])
示例#2
0
def fizzbuzz(request):
    '''
    Takes a GET request at /api with the parameters 'word' and 'max_value', and returns
    a json-formatted response containing the numbers correlating to the fizzbuzz test for the provided word and 
    max_value, plus a status code.
    '''

    # Set a 400 error first because we change this var fewer times if we do that
    stat = {'code': status.HTTP_400_BAD_REQUEST, 'message': 'error'}

    try:
        word = request.query_params['word']
        max_value = int(request.query_params['max_value'])
    except (MultiValueDictKeyError, ValueError):
        # MultiValueDictKeyError in case the keys we're looking for don't exist in .query_params
        # ValueError in case people use quotes, floats or other junk in the request parameters
        word = None
        max_value = None

    if word in ('fizz', 'buzz', 'fizzbuzz'
                ) and max_value > 0:  # check for valid param vals here
        stat = {'code': status.HTTP_200_OK, 'message': 'ok'}

    fizzbuzz = FizzBuzz(max_value=max_value, word=word)
    fizzbuzz_dict = fizzbuzz.create_fizzbuzz_dict()
    fizzbuzz_dict['status'] = stat['message']

    return Response(json.dumps(fizzbuzz_dict), status=stat['code'])
示例#3
0
文件: tests.py 项目: jlegs/django-api
 def test_fizz_15(self):
     fb = FizzBuzz(word="fizz", max_value=15)
     self.assertEqual(fb.create_fizzbuzz_dict(), {"numbers": [3, 6, 9, 12, 15]})
示例#4
0
文件: tests.py 项目: jlegs/django-api
 def test_fizz_0(self):
     fb = FizzBuzz(word="fizz", max_value=0)
     self.assertEqual(fb.create_fizzbuzz_dict(), {"numbers": []})
示例#5
0
文件: tests.py 项目: jlegs/django-api
 def test_wrong_15(self):
     fb = FizzBuzz(word="wrong", max_value=15)
     self.assertEqual(fb.create_fizzbuzz_dict(), {"numbers": []})
示例#6
0
文件: tests.py 项目: jlegs/django-api
 def test_fizz_30(self):
     fb = FizzBuzz(word="fizz", max_value=30)
     self.assertEqual(fb.create_fizzbuzz_dict(), {"numbers": [3, 6, 9, 12, 15, 18, 21, 24, 27, 30]})