from api.streams.models import StreamConfiguration
from django.http import JsonResponse
from django.http.request import HttpRequest
import requests

def get_stream_status(request: HttpRequest, stream_slug: str):
    stream = StreamConfiguration.objects.get(slug=stream_slug)

    r = requests.get('http://{stream.host}:{stream.port}/status-json.xsl'.format(stream=stream), timeout=5)
    r.raise_for_status()

    return JsonResponse(r.json())

from api.streams.models import StreamConfiguration
from django.http import JsonResponse, Http404
from django.http.request import HttpRequest
import requests

def get_stream_status(request: HttpRequest, stream_slug: str):
    try:
        stream = StreamConfiguration.objects.get(slug=stream_slug)
    except StreamConfiguration.DoesNotExist:
        raise Http404("Stream with slug {0} does not exist.".format(stream_slug))

    r = requests.get('http://{stream.host}:{stream.port}/status-json.xsl'.format(stream=stream), timeout=5)

    if r.status_code != requests.codes.ok:
        return JsonResponse({ "error": "Upstream request failed" }, status=500)

    return JsonResponse(r.json())

Improve error handling in stream status view
Improve error handling in stream status view

- Check if stream exists and raise a 404 otherwise
- Check if upstream returned a success status code and raise a 500 otherwise

  from api.streams.models import StreamConfiguration
- from django.http import JsonResponse
+ from django.http import JsonResponse, Http404
  from django.http.request import HttpRequest
  import requests
  
  def get_stream_status(request: HttpRequest, stream_slug: str):
+     try:
-     stream = StreamConfiguration.objects.get(slug=stream_slug)
+         stream = StreamConfiguration.objects.get(slug=stream_slug)
+     except StreamConfiguration.DoesNotExist:
+         raise Http404("Stream with slug {0} does not exist.".format(stream_slug))
  
      r = requests.get('http://{stream.host}:{stream.port}/status-json.xsl'.format(stream=stream), timeout=5)
-     r.raise_for_status()
+ 
+     if r.status_code != requests.codes.ok:
+         return JsonResponse({ "error": "Upstream request failed" }, status=500)
  
      return JsonResponse(r.json())
  
Improve error handling in stream status view
## Code Before:
from api.streams.models import StreamConfiguration
from django.http import JsonResponse
from django.http.request import HttpRequest
import requests

def get_stream_status(request: HttpRequest, stream_slug: str):
    stream = StreamConfiguration.objects.get(slug=stream_slug)

    r = requests.get('http://{stream.host}:{stream.port}/status-json.xsl'.format(stream=stream), timeout=5)
    r.raise_for_status()

    return JsonResponse(r.json())

## Instruction:
Improve error handling in stream status view
## Code After:
from api.streams.models import StreamConfiguration
from django.http import JsonResponse, Http404
from django.http.request import HttpRequest
import requests

def get_stream_status(request: HttpRequest, stream_slug: str):
    try:
        stream = StreamConfiguration.objects.get(slug=stream_slug)
    except StreamConfiguration.DoesNotExist:
        raise Http404("Stream with slug {0} does not exist.".format(stream_slug))

    r = requests.get('http://{stream.host}:{stream.port}/status-json.xsl'.format(stream=stream), timeout=5)

    if r.status_code != requests.codes.ok:
        return JsonResponse({ "error": "Upstream request failed" }, status=500)

    return JsonResponse(r.json())
