Return different type of response based on Accept header I'm a novice to Scala and Akka-Http and am experimenting with Akka-Http for writing rest services. I have to return JSON or protobuf based on the Accept-Header.

optionalHeaderValueByName("Accept"){ contentType =&gt;
  if(contentType == Some(protoEncode)) {
    complete {
      NewsService.getNewsList().map {
        case stories: List[Story] =&gt;    HttpResponse(entity = HttpEntity(ContentType(protoEncoding),  StoryList(stories).toProto().build().toByteArray))
      }
    }
  } else {
      complete {
        NewsService.getNewsList().map {
          case stories: List[Story] =&gt; StoryList(stories)
          }
        }
  }


As you can see, the code repetition is happening. Can anyone suggest what could be the best way to optimise and generalise the design to avoid such a situation?
I don't know Akka-Http API well enough, but it looks like the if condition is misplaced. I think that it can be substituted with a matcher under case stories:

optionalHeaderValueByName("Accept") { contentType =&gt;
  complete {
    NewsService.getNewsList().map {
      case stories: List[Story] =&gt; {
        contentType match {
          case Some(protoEncode) =&gt; 
            HttpResponse(entity = HttpEntity(ContentType(protoEncoding), StoryList(stories).toProto().build().toByteArray))
          case None =&gt; StoryList(stories)
        }
      }
    }
  }
}


It seems that it is a bit too deeply nested, but at least there is no repetition on NewsService.getNewsList().map.