Django个人博客搭建教程---使用类视图手写一个API(二)

接上一篇,加入异常捕获和错误码处理

def queryweatherinfo():
    url = "https://restapi.amap.com/v3/weather/weatherInfo"
    key = '9bae4d790cfacdf4b54188b7758edf23'
    data = {'key': key, "city": '320100'}
    req = requests.post(url, data)
    return req.json()


class GetWeatherInfo(View):

    def get(self, request):
        result = {}
        try:
            info = dict(queryweatherinfo())
            weatherinfo = info['lives'][0]
            result['status'] = 000
            result['message'] = 'success'
            result['info'] = {
                'location': weatherinfo['province'] + weatherinfo['city'],
                'weather': weatherinfo['weather'],
                'temperature': weatherinfo['temperature'] + '℃',
                'winddirection': weatherinfo['winddirection'],
                'windpower': weatherinfo['windpower'],
                'humidity': weatherinfo['humidity'],
                'reporttime': weatherinfo['reporttime'],
            }
            return JsonResponse(
                result,
                json_dumps_params={'ensure_ascii': False}
            )
        except:
            result['status'] = 500
            result['message'] = '请求异常,请稍后重试'
            result['info'] = {}
            return JsonResponse(
                result,
                json_dumps_params={'ensure_ascii': False}
            )

    @csrf_exempt
    def post(self, request):
        # 查询到的数据是二进制
        json_bytes = request.body
        # 需要进行解码,转成字符串
        json_str = json_bytes.decode()
        # 在把字符串转换成json字典
        weather_dict = json.loads(json_str)
        city = weather_dict.get('city')
        result = {}
        try:
            url = "https://restapi.amap.com/v3/weather/weatherInfo"
            key = '9bae4d790cfacdf4b54188b7758edf23'
            data = {'key': key, "city": city}
            req = requests.post(url, data)
            info = dict(req.json())
            weatherinfo = info['lives'][0]
            result['status'] = 000
            result['message'] = 'success'
            result['info'] = {
                'location': weatherinfo['province'] + weatherinfo['city'],
                'weather': weatherinfo['weather'],
                'temperature': weatherinfo['temperature'] + '℃',
                'winddirection': weatherinfo['winddirection'],
                'windpower': weatherinfo['windpower'],
                'humidity': weatherinfo['humidity'],
                'reporttime': weatherinfo['reporttime'],
            }
            return JsonResponse(
                result,
                json_dumps_params={'ensure_ascii': False}
            )
        except:
            result = {
                'status': 500,
                'message': '请求异常,请稍后重试',
                'info': {}
            }
            return JsonResponse(
                result,
                json_dumps_params={'ensure_ascii': False}
            )

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值