4. 타임라인

JSON data

{
    "timeline": [
        {
            "tweet": "Hello World",
            "user_id": 1
        },
        {
            "tweet": "언젠가는 훌륭한 백엔드 개발자가 될 거야",
            "user_id": 2
        }
    ],
    "user_id": 1
}

 

트윗 엔드포인트에서 사용자들의 트윗을 app.tweets리스트에 저장하는 것을 기억함

app.tweets 리스트에서 해당 사용자 그리고 사용자가 팔로우한느 사용자들의 트윗들을 찾은 후에 전송

 

@app.route("/timeline/<int:user_id>", methods=["GET"])  # 1
def timeline(user_id):
    if user_id not in app.users:
        return "사용자가 존재하지 않습니다.", 400

    follow_list = app.users[user_id].get("follow", set())
    follow_list.add(user_id)
    timeline = [tweet for tweet in app.tweets if tweet["user_id"] in follow_list]

    return jsonify({"user_id": user_id, "timeline": timeline})

 

$ http -v POST localhost:5000/sign-up name="현욱" && http -v POST localhost:5000/sign-up name="라이언" && http -v POST localhost:5000/tweet id=1 tweet="Hello World" && http -v POST localhost:5000/tweet id=2 tweet="언젠가는 훌륭한 백엔드 개발자
가 될 거야" && http -v POST localhost:5000/follow id=1 follow=2 && http -v GET localhost:5000/timeline/1
POST /sign-up HTTP/1.1
Accept: application/json, */*;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Length: 24
Content-Type: application/json
Host: localhost:5000
User-Agent: HTTPie/2.4.0

{
    "name": "현욱"
}


HTTP/1.0 200 OK
Content-Length: 41
Content-Type: application/json
Date: Tue, 16 Feb 2021 14:22:09 GMT
Server: Werkzeug/1.0.1 Python/3.8.5

{
    "id": 2,
    "name": "현욱"
}


POST /sign-up HTTP/1.1
Accept: application/json, */*;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Length: 30
Content-Type: application/json
Host: localhost:5000
User-Agent: HTTPie/2.4.0

{
    "name": "라이언"
}


HTTP/1.0 200 OK
Content-Length: 47
Content-Type: application/json
Date: Tue, 16 Feb 2021 14:22:13 GMT
Server: Werkzeug/1.0.1 Python/3.8.5

{
    "id": 3,
    "name": "라이언"
}


POST /tweet HTTP/1.1
Accept: application/json, */*;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Length: 35
Content-Type: application/json
Host: localhost:5000
User-Agent: HTTPie/2.4.0

{
    "id": "1",
    "tweet": "Hello World"
}


HTTP/1.0 200 OK
Content-Length: 0
Content-Type: text/html; charset=utf-8
Date: Tue, 16 Feb 2021 14:22:16 GMT
Server: Werkzeug/1.0.1 Python/3.8.5




POST /tweet HTTP/1.1
Accept: application/json, */*;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Length: 131
Content-Type: application/json
Host: localhost:5000
User-Agent: HTTPie/2.4.0

{
    "id": "2",
    "tweet": "언젠가는 훌륭한 백엔드 개발자가 될 거야"
}


HTTP/1.0 200 OK
Content-Length: 0
Content-Type: text/html; charset=utf-8
Date: Tue, 16 Feb 2021 14:22:19 GMT
Server: Werkzeug/1.0.1 Python/3.8.5




POST /follow HTTP/1.1
Accept: application/json, */*;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Length: 26
Content-Type: application/json
Host: localhost:5000
User-Agent: HTTPie/2.4.0

{
    "follow": "2",
    "id": "1"
}


HTTP/1.0 200 OK
Content-Length: 67
Content-Type: application/json
Date: Tue, 16 Feb 2021 14:22:22 GMT
Server: Werkzeug/1.0.1 Python/3.8.5

{
    "follow": [
        2
    ],
    "id": 1,
    "name": "현욱"
}


GET /timeline/1 HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Host: localhost:5000
User-Agent: HTTPie/2.4.0



HTTP/1.0 200 OK
Content-Length: 263
Content-Type: application/json
Date: Tue, 16 Feb 2021 14:22:26 GMT
Server: Werkzeug/1.0.1 Python/3.8.5

{
    "timeline": [
        {
            "tweet": "Hello World",
            "user_id": 1
        },
        {
            "tweet": "언젠가는 훌륭한 백엔드 개발자가 될 거야",
            "user_id": 2
        }
    ],
    "user_id": 1
}


정리

- 데이터를 수정하는 기능의 엔드포인트는 POST를 사용

- 데이터를 읽는 기능의 엔드포인트는 GET 사용

- POST 엔드포인트에 데이터를 전송할 때 body에 JSON형식으로 전송

- URL인자를 전송하고 싶을 때는 <type:value> 형식으로 구성

   ex) /timeline/<int:user_id>

- 중복된 값이 없어야하는 데이터라면 set을 사용

- 순서나 순차가 중요하다면 list

- 키와 값을 표현해야하는 데이터의 경우 dict

반응형
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기