[Python] Code Snippets

Joey Hong·2021년 9월 25일
0

Python

목록 보기
2/2

2021 카카오 신입공채 2차 온라인 코딩 테스트 for Tech developers 문제해설

REST API

import json
import requests

GET

baseURL/locations 을 get

def locations_api(auth_key):
    headers = {'Authorization': auth_key, 'Content-Type': 'application/json'}
    url = baseURL + '/locations'
    response = requests.get(url, headers = headers)
    return response.json()['locations']

POST

baseURL/start 에 post

baseURL = 
token = 
problem = 1

def start_api():
    headers = {'X-Auth-Token': token, 'Content-Type': 'application/json'}
    data = f'{{"problem": {problem}}}'
    url = baseURL + '/start'
    response = requests.post(url, headers = headers, data = data)
    if response.status_code == 200:
        auth_key = response.json()['auth_key']
    else:
        auth_key = "failed to get auth key"
    return auth_key

PUT

  • baseURL/simulate 에 put

def simulate_api(auth_key, simulations):
    headers = {'Authorization': auth_key, 'Content-Type': 'application/json'}
    url = baseURL + '/simulate'
    data = {'commands': simulations}
    response = requests.put(url, headers=headers, data=json.dumps(data))
    parsed = response.json()

    return parsed

String Literals

json.dumps()

data = {'problem': problem}
response = requests.put(url, headers=headers, data=json.dumps(data))

f-string

data = f'{{"problem": {problem}}}'
response = requests.post(url, headers = headers, data = data)
profile
개발기록

0개의 댓글