Exemplo n.º 1
0
class TemperatureResource(Resource):
    def __init__(self):
        self.temperature_resource_db = TemperatureResourceDatabase()
        self.parser = reqparse.RequestParser()
        # 서버에서 parsing하기 위해 받는 정보들 추가
        self.parser.add_argument('temperature')
        self.parser.add_argument('datetime')
        self.parser.add_argument('location')

    # get 요청을 받았을 경우
    def get(self, sensor_id):

        res1 = requests.get(url=KAKAO_BASE_URL +
                            "/v3/search/book?target=title&query=자이스토리",
                            headers=headers)

        if res1.status_code == 200:
            books = res1.json()
        else:
            books = {}

        return books, 200
        # temperature = self.temperature_resource_db.readBySensorId(sensor_id=sensor_id)
        # if temperature is None:
        #     abort(404, message="Sensor id {0} doesn't exist".format(sensor_id))
        # else:
        #     # Resource 부모 클래스에서 Serializable 개념을 통해 자동적으로 json 파일로 반환해줌
        #     return temperature, 200

    # put 요청을 받았을 경우
    def put(self, sensor_id):
        # parser add 된 인자 3개가 반환
        args = self.parser.parse_args()
        temperature = self.temperature_resource_db.readBySensorId(
            sensor_id=sensor_id)
        if temperature is None:
            abort(
                404,
                message="Error! Sensor id {0} doesn't exist".format(sensor_id))
        else:
            self.temperature_resource_db.update(
                sensor_id=sensor_id,
                temperature=args['temperature'],
                datetime=args['datetime'],
                location=args['location'])
            return {"sensor_id": sensor_id}, 200

    # delete 요청을 받았을 경우
    def delete(self, sensor_id):
        temperature = self.temperature_resource_db.readBySensorId(
            sensor_id=sensor_id)
        if temperature is None:
            # 이 방법이 좀 더 이론적
            return {"sensor_id": sensor_id}, 204
            # abort(404, message="Error! Sensor id {0} doesn't exist".format(sensor_id))
        else:
            self.temperature_resource_db.delete(sensor_id=sensor_id)
            return {"sensor_id": sensor_id}, 204  # delete 성공 시에는 204가 약속
Exemplo n.º 2
0
class TemperatureResource(Resource):
    def __init__(self):
        self.temperature_resource_db = TemperatureResourceDatabase()
        self.parser = reqparse.RequestParser()
        self.parser.add_argument('temperature')
        self.parser.add_argument('datetime')
        self.parser.add_argument('location')

    def get(self, sensor_id):
        res1 = requests.get(url=KAKAO_BASE_URL +
                            "/v3/search/book?target=title&query=미움받을 용기",
                            headers=headers)

        if res1.status_code == 200:
            books = res1.json()
        else:
            books = {}

        return books, 200

        #temperature = self.temperature_resource_db.readBySensorId(sensor_id=sensor_id)
        #if temperature is None:
        #    abort(404, message="Sensor id {0} doesn't exist".format(sensor_id))
        #else:
        #    return temperature, 200

    def put(self, sensor_id):
        args = self.parser.parse_args()
        temperature = self.temperature_resource_db.readBySensorId(
            sensor_id=sensor_id)
        if temperature is None:
            abort(
                404,
                message="Error! Sensor id {0} doesn't exist".format(sensor_id))
        else:
            self.temperature_resource_db.update(
                sensor_id=sensor_id,
                temperature=args['temperature'],
                datetime=args['datetime'],
                location=args['location'])
            return {"sensor_id": sensor_id}, 200

    def delete(self, sensor_id):
        temperature = self.temperature_resource_db.readBySensorId(
            sensor_id=sensor_id)
        if temperature is None:
            return {"sensor_id": sensor_id}, 204
        else:
            self.temperature_resource_db.delete(sensor_id=sensor_id)
            return {"sensor_id": sensor_id}, 204
Exemplo n.º 3
0
class TemperatureResource(Resource):
    def __init__(self):
        self.temperature_resource_db = TemperatureResourceDatabase()
        self.parser = reqparse.RequestParser()
        self.parser.add_argument('temperature')
        self.parser.add_argument('datetime')
        self.parser.add_argument('location')

    def get(self, sensor_id):
        # Ctrl + / : 여러 줄 주석
        temperature = self.temperature_resource_db.readBySensorId(
            sensor_id=sensor_id)
        if temperature is None:
            abort(404, message="Sensor id {0} doesn't exist".format(sensor_id))
        else:
            return temperature, 200

    def put(self, sensor_id):
        args = self.parser.parse_args()
        temperature = self.temperature_resource_db.readBySensorId(
            sensor_id=sensor_id)
        if temperature is None:
            abort(
                404,
                message="Error! Sensor id {0} doesn't exist".format(sensor_id))
        else:
            self.temperature_resource_db.update(
                sensor_id=sensor_id,
                temperature=args['temperature'],
                datetime=args['datetime'],
                location=args['location'])
            return {"sensor_id": sensor_id}, 200

    def delete(self, sensor_id):
        temperature = self.temperature_resource_db.readBySensorId(
            sensor_id=sensor_id)
        if temperature is None:
            abort(
                404,
                message="Error! Sensor id {0} doesn't exist".format(sensor_id))
        else:
            self.temperature_resource_db.delete(sensor_id=sensor_id)
            return {"sensor_id": sensor_id}, 204