import os
import aiohttp
import json
from aiohttp import web

from .consts import KUDAGO_API_BASE_URL, CLIENT_DIR


async def serve_api(request):
    url = '{}/{}/?{}'.format(
        KUDAGO_API_BASE_URL,
        request.match_info['path'],
        request.query_string,
    )
    response = await aiohttp.get(url)
    body = await response.json()

    if isinstance(body, dict):
        for field in ('next', 'previous'):
            value = body.get(field)
            if value:
                body[field] = value.replace(KUDAGO_API_BASE_URL, '/api')

    return web.Response(body=json.dumps(body).encode('utf-8'))


async def serve_client(request):
    filepath = os.path.join(CLIENT_DIR, 'index.html')
    stat = os.stat(filepath)
    chunk_size = 256 * 1024

    response = web.StreamResponse()
    response.content_type = 'text/html'
    response.last_modified = stat.st_mtime
    response.content_length = stat.st_size

    response.start(request)
    with open(filepath, 'rb') as f:
        chunk = f.read(chunk_size)
        while chunk:
            response.write(chunk)
            chunk = f.read(chunk_size)

    return response

import os
import aiohttp
import json
from aiohttp import web

from .consts import KUDAGO_API_BASE_URL, CLIENT_DIR


async def serve_api(request):
    url = '{}/{}/?{}'.format(
        KUDAGO_API_BASE_URL,
        request.match_info['path'],
        request.query_string,
    )
    response = await aiohttp.get(url)
    body = await response.json()

    if isinstance(body, dict):
        for field in ('next', 'previous'):
            value = body.get(field)
            if value:
                body[field] = value.replace(KUDAGO_API_BASE_URL, '/api')

    return web.Response(
        text=json.dumps(body),
        content_type='application/json'
    )


async def serve_client(request):
    filepath = os.path.join(CLIENT_DIR, 'index.html')
    stat = os.stat(filepath)
    chunk_size = 256 * 1024

    response = web.StreamResponse()
    response.content_type = 'text/html'
    response.last_modified = stat.st_mtime
    response.content_length = stat.st_size

    response.start(request)
    with open(filepath, 'rb') as f:
        chunk = f.read(chunk_size)
        while chunk:
            response.write(chunk)
            chunk = f.read(chunk_size)

    return response

Add content type to API responses
Add content type to API responses

It doesn’t make much of a difference, but it’s the nice thing to do

  import os
  import aiohttp
  import json
  from aiohttp import web
  
  from .consts import KUDAGO_API_BASE_URL, CLIENT_DIR
  
  
  async def serve_api(request):
      url = '{}/{}/?{}'.format(
          KUDAGO_API_BASE_URL,
          request.match_info['path'],
          request.query_string,
      )
      response = await aiohttp.get(url)
      body = await response.json()
  
      if isinstance(body, dict):
          for field in ('next', 'previous'):
              value = body.get(field)
              if value:
                  body[field] = value.replace(KUDAGO_API_BASE_URL, '/api')
  
-     return web.Response(body=json.dumps(body).encode('utf-8'))
+     return web.Response(
+         text=json.dumps(body),
+         content_type='application/json'
+     )
  
  
  async def serve_client(request):
      filepath = os.path.join(CLIENT_DIR, 'index.html')
      stat = os.stat(filepath)
      chunk_size = 256 * 1024
  
      response = web.StreamResponse()
      response.content_type = 'text/html'
      response.last_modified = stat.st_mtime
      response.content_length = stat.st_size
  
      response.start(request)
      with open(filepath, 'rb') as f:
          chunk = f.read(chunk_size)
          while chunk:
              response.write(chunk)
              chunk = f.read(chunk_size)
  
      return response
  
Add content type to API responses
## Code Before:
import os
import aiohttp
import json
from aiohttp import web

from .consts import KUDAGO_API_BASE_URL, CLIENT_DIR


async def serve_api(request):
    url = '{}/{}/?{}'.format(
        KUDAGO_API_BASE_URL,
        request.match_info['path'],
        request.query_string,
    )
    response = await aiohttp.get(url)
    body = await response.json()

    if isinstance(body, dict):
        for field in ('next', 'previous'):
            value = body.get(field)
            if value:
                body[field] = value.replace(KUDAGO_API_BASE_URL, '/api')

    return web.Response(body=json.dumps(body).encode('utf-8'))


async def serve_client(request):
    filepath = os.path.join(CLIENT_DIR, 'index.html')
    stat = os.stat(filepath)
    chunk_size = 256 * 1024

    response = web.StreamResponse()
    response.content_type = 'text/html'
    response.last_modified = stat.st_mtime
    response.content_length = stat.st_size

    response.start(request)
    with open(filepath, 'rb') as f:
        chunk = f.read(chunk_size)
        while chunk:
            response.write(chunk)
            chunk = f.read(chunk_size)

    return response

## Instruction:
Add content type to API responses
## Code After:
import os
import aiohttp
import json
from aiohttp import web

from .consts import KUDAGO_API_BASE_URL, CLIENT_DIR


async def serve_api(request):
    url = '{}/{}/?{}'.format(
        KUDAGO_API_BASE_URL,
        request.match_info['path'],
        request.query_string,
    )
    response = await aiohttp.get(url)
    body = await response.json()

    if isinstance(body, dict):
        for field in ('next', 'previous'):
            value = body.get(field)
            if value:
                body[field] = value.replace(KUDAGO_API_BASE_URL, '/api')

    return web.Response(
        text=json.dumps(body),
        content_type='application/json'
    )


async def serve_client(request):
    filepath = os.path.join(CLIENT_DIR, 'index.html')
    stat = os.stat(filepath)
    chunk_size = 256 * 1024

    response = web.StreamResponse()
    response.content_type = 'text/html'
    response.last_modified = stat.st_mtime
    response.content_length = stat.st_size

    response.start(request)
    with open(filepath, 'rb') as f:
        chunk = f.read(chunk_size)
        while chunk:
            response.write(chunk)
            chunk = f.read(chunk_size)

    return response
