Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

APIS-7318 - Update heading links and move API Docs to use the V2 Page… #525

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ class ApiDocumentationController @Inject() (
loggedInUserService: LoggedInUserService,
errorHandler: ErrorHandler,
mcc: MessagesControllerComponents,
apiIndexView: ApiIndexView,
retiredVersionJumpView: RetiredVersionJumpView,
apisFilteredView: ApisFilteredView,
xmlDocumentationView: XmlDocumentationView,
parentPage: ParentPageOuter,
xmlServicesService: XmlServicesService,
Expand All @@ -61,47 +59,15 @@ class ApiDocumentationController @Inject() (
) extends FrontendController(mcc) with HeaderNavigation with PageAttributesHelper with HomeCrumb with DocumentationCrumb with ApplicationLogger {

private lazy val cacheControlHeaders = "cache-control" -> "no-cache,no-store,max-age=0"
private lazy val apiDocCrumb = Crumb("API Documentation", routes.ApiDocumentationController.apiIndexPage(None, None, None).url)

private lazy val v2ApiDocCrumb = Crumb(
private lazy val apiDocCrumb = Crumb(
"API Documentation",
uk.gov.hmrc.apidocumentation.v2.controllers.routes.FilteredDocumentationIndexController.apiListIndexPage(List.empty, List.empty).url
routes.FilteredDocumentationIndexController.apiListIndexPage(List.empty, List.empty).url
)

def apiIndexPage(service: Option[ServiceName], version: Option[ApiVersionNbr], filter: Option[String]): Action[AnyContent] = headerNavigation { implicit request => navLinks =>
def pageAttributes(title: String = "API Documentation") =
PageAttributes(title, breadcrumbs = Breadcrumbs(documentationCrumb, homeCrumb), headerLinks = navLinks, sidebarLinks = navigationService.sidebarNavigation())

val params = for (a <- service; b <- version) yield (a, b)

params match {
case Some((service, version)) =>
val url = routes.ApiDocumentationController.renderApiDocumentation(service, version, None).url
Future.successful(Redirect(url))
case None =>
(for {
userId <- extractDeveloperIdentifier(loggedInUserService.fetchLoggedInUser())
apis <- apiDefinitionService.fetchAllDefinitions(userId)
xmlApis <- xmlServicesService.fetchAllXmlApis()
} yield {
val apisByCategory = Documentation.groupedByCategory(apis, xmlApis, ServiceGuide.serviceGuides, RoadMap.roadMaps)

filter match {
case Some(f) => Ok(apisFilteredView(pageAttributes("Filtered API Documentation"), apisByCategory, DocumentationCategory.fromFilter(f)))
case _ => Ok(apiIndexView(pageAttributes(), apisByCategory))
}

}) recoverWith {
case e: Throwable =>
logger.error("Could not load API Documentation service", e)
errorHandler.internalServerErrorTemplate.map(InternalServerError(_))
}
}
}

def redirectToApiDocumentation(service: ServiceName, version: Option[ApiVersionNbr]): Action[AnyContent] = version match {
case Some(version) => Action.async {
Future.successful(Redirect(routes.ApiDocumentationController.renderApiDocumentation(service, version, None)))
Future.successful(Redirect(routes.ApiDocumentationController.renderApiDocumentation(service, version)))
}
case _ => redirectToCurrentApiDocumentation(service)
}
Expand All @@ -112,7 +78,7 @@ class ApiDocumentationController @Inject() (
extendedDefn <- apiDefinitionService.fetchExtendedDefinition(service, userId)
} yield {
extendedDefn.flatMap(_.userAccessibleApiDefinition.defaultVersion).fold(errorHandler.notFoundTemplate.map(NotFound(_))) { version =>
successful(Redirect(routes.ApiDocumentationController.renderApiDocumentation(service, version.version, None)))
successful(Redirect(routes.ApiDocumentationController.renderApiDocumentation(service, version.version)))
}
}).flatten recoverWith {
case _: NotFoundException => errorHandler.notFoundTemplate.map(NotFound(_))
Expand All @@ -122,12 +88,12 @@ class ApiDocumentationController @Inject() (
}
}

def renderApiDocumentation(service: ServiceName, version: ApiVersionNbr, useV2: Option[Boolean]): Action[AnyContent] =
def renderApiDocumentation(service: ServiceName, version: ApiVersionNbr): Action[AnyContent] =
headerNavigation { implicit request => navLinks =>
(for {
userId <- extractDeveloperIdentifier(loggedInUserService.fetchLoggedInUser())
api <- apiDefinitionService.fetchExtendedDefinition(service, userId)
apiDocumentation <- doRenderApiDocumentation(service, version, api, navLinks, userId, useV2)
apiDocumentation <- doRenderApiDocumentation(service, version, api, navLinks, userId)
} yield apiDocumentation) recoverWith {
case e: NotFoundException =>
logger.info(s"Upstream request not found: ${e.getMessage}")
Expand All @@ -144,18 +110,13 @@ class ApiDocumentationController @Inject() (
version: ApiVersionNbr,
apiOption: Option[ExtendedApiDefinition],
navLinks: Seq[NavLink],
developerId: Option[DeveloperIdentifier],
useV2IndexPage: Option[Boolean]
developerId: Option[DeveloperIdentifier]
)(implicit request: Request[AnyContent],
messagesProvider: MessagesProvider
): Future[Result] = {
def makePageAttributes(apiDefinition: ExtendedApiDefinition, sidebarLinks: Seq[SidebarLink]) = {

val breadcrumbs = if (useV2IndexPage.getOrElse(false)) Breadcrumbs(
v2ApiDocCrumb,
homeCrumb
)
else Breadcrumbs(
val breadcrumbs = Breadcrumbs(
apiDocCrumb,
homeCrumb
)
Expand All @@ -166,27 +127,25 @@ class ApiDocumentationController @Inject() (
def renderNotFoundPage = errorHandler.notFoundTemplate.map(NotFound(_))

def redirectToLoginPage = {
logger.info(s"redirectToLogin - access_uri ${routes.ApiDocumentationController.renderApiDocumentation(service, version, None).url}")
logger.info(s"redirectToLogin - access_uri ${routes.ApiDocumentationController.renderApiDocumentation(service, version).url}")
Future.successful(Redirect("/developer/login").withSession(
"access_uri" -> routes.ApiDocumentationController.renderApiDocumentation(service, version, None).url,
"access_uri" -> routes.ApiDocumentationController.renderApiDocumentation(service, version).url,
"ts" -> Instant.now(Clock.systemUTC).toEpochMilli.toString
))
}

def renderRetiredVersionJumpPage(api: ExtendedApiDefinition, useV2: Option[Boolean])(implicit request: Request[AnyContent], messagesProvider: MessagesProvider) = {
def renderRetiredVersionJumpPage(api: ExtendedApiDefinition)(implicit request: Request[AnyContent], messagesProvider: MessagesProvider) = {
val apiDefinition = api.userAccessibleApiDefinition

Future.successful(Ok(retiredVersionJumpView(
makePageAttributes(apiDefinition, navigationService.sidebarNavigation()),
apiDefinition,
useV2
apiDefinition
)))
}

def renderDocumentationPage(
api: ExtendedApiDefinition,
selectedVersion: ExtendedApiVersion,
useV2: Option[Boolean]
selectedVersion: ExtendedApiVersion
)(implicit request: Request[AnyContent],
messagesProvider: MessagesProvider
): Future[Result] = {
Expand Down Expand Up @@ -220,7 +179,7 @@ class ApiDocumentationController @Inject() (
markdownBlocks = List(overview, errors, testing) ++ (if (requiredFraudPrevention) List(fraudPrevention) else List()) ++ List(versioning)
attrs = makePageAttributes(api, navigationService.openApiSidebarNavigation(markdownBlocks))

} yield Ok(parentPage(attrs, markdownBlocks, api.name, api, selectedVersion, developerId.isDefined, useV2 = useV2)).withHeaders(cacheControlHeaders)
} yield Ok(parentPage(attrs, markdownBlocks, api.name, api, selectedVersion, developerId.isDefined)).withHeaders(cacheControlHeaders)
}

val categories = APICategoryFilters.categoryMap.getOrElse(api.name, Seq.empty) ++ api.categories
Expand All @@ -235,28 +194,22 @@ class ApiDocumentationController @Inject() (
} yield (api, apiVersion, visibility)

findVersion(apiOption) match {
case Some((api, selectedVersion, VersionVisibility(_, _, true, _))) if selectedVersion.status == ApiStatus.RETIRED => renderRetiredVersionJumpPage(api, useV2IndexPage)
case Some((api, selectedVersion, VersionVisibility(_, _, true, _))) => renderDocumentationPage(api, selectedVersion, useV2IndexPage)
case Some((api, selectedVersion, VersionVisibility(ApiAccessType.PRIVATE, _, false, true))) => renderDocumentationPage(api, selectedVersion, useV2IndexPage)
case Some((api, selectedVersion, VersionVisibility(_, _, true, _))) if selectedVersion.status == ApiStatus.RETIRED => renderRetiredVersionJumpPage(api)
case Some((api, selectedVersion, VersionVisibility(_, _, true, _))) => renderDocumentationPage(api, selectedVersion)
case Some((api, selectedVersion, VersionVisibility(ApiAccessType.PRIVATE, _, false, true))) => renderDocumentationPage(api, selectedVersion)
case Some((_, _, VersionVisibility(ApiAccessType.PRIVATE, false, _, _))) => redirectToLoginPage
case _ => renderNotFoundPage
}
}
// scalastyle:on method.length

def renderXmlApiDocumentation(name: String, useV2: Option[Boolean]): Action[AnyContent] = headerNavigation { implicit request => navLinks =>
def renderXmlApiDocumentation(name: String): Action[AnyContent] = headerNavigation { implicit request => navLinks =>
def makePageAttributes(apiDefinition: Documentation): PageAttributes = {
val xmlCrumb = Crumb(
apiDefinition.name,
routes.ApiDocumentationController.renderXmlApiDocumentation(apiDefinition.context, useV2).url
routes.ApiDocumentationController.renderXmlApiDocumentation(apiDefinition.context).url
)
val breadcrumbs = if (useV2.getOrElse(false)) {
Breadcrumbs(
xmlCrumb,
v2ApiDocCrumb,
homeCrumb
)
} else Breadcrumbs(
val breadcrumbs = Breadcrumbs(
xmlCrumb,
apiDocCrumb,
homeCrumb
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,8 @@ class DocumentationController @Inject() (
partialsService: PartialsService,
mcc: MessagesControllerComponents,
indexView: IndexView,
retiredVersionJumpView: RetiredVersionJumpView,
tutorialsView: TutorialsView,
credentialsView: CredentialsView,
developmentPracticesView: DevelopmentPracticesView,
mtdIntroductionView: MtdIntroductionView,
namingGuidelinesView: NamingGuidelinesView,
referenceView: ReferenceView,
termsOfUseView: TermsOfUseView,
Expand Down Expand Up @@ -131,7 +128,7 @@ class DocumentationController @Inject() (
_ => navLinks =>
Future.successful(
Redirect(
routes.ApiDocumentationController.apiIndexPage(None, None, None).url
routes.FilteredDocumentationIndexController.apiListIndexPage(List.empty, List.empty).url
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ class DownloadController @Inject() (
)(implicit val ec: ExecutionContext
) extends FrontendController(cc) with ApplicationLogger {

def downloadResource(service: ServiceName, version: ApiVersionNbr, resource: String, useV2: Option[Boolean]) = Action.async { implicit request =>
def downloadResource(service: ServiceName, version: ApiVersionNbr, resource: String) = Action.async { implicit request =>
(for {
userId <- extractDeveloperIdentifier(loggedInUserService.fetchLoggedInUser())
api <- apiDefinitionService.fetchExtendedDefinition(service, userId)
validResource = validateResource(resource)
result <- fetchResourceForApi(api, version, validResource, useV2)
result <- fetchResourceForApi(api, version, validResource)
} yield {
result
}) recoverWith {
Expand All @@ -61,8 +61,7 @@ class DownloadController @Inject() (
}
}

private def fetchResourceForApi(apiOption: Option[ExtendedApiDefinition], version: ApiVersionNbr, validResource: String, useV2: Option[Boolean])(implicit request: Request[_])
: Future[Result] = {
private def fetchResourceForApi(apiOption: Option[ExtendedApiDefinition], version: ApiVersionNbr, validResource: String)(implicit request: Request[_]): Future[Result] = {
def findVersion(apiOption: Option[ExtendedApiDefinition]) =
for {
api <- apiOption
Expand All @@ -75,7 +74,7 @@ class DownloadController @Inject() (

def redirectToLoginPage(service: ServiceName) =
Future.successful(Redirect("/developer/login").withSession(
"access_uri" -> routes.ApiDocumentationController.renderApiDocumentation(service, version, useV2).url
"access_uri" -> routes.ApiDocumentationController.renderApiDocumentation(service, version).url
))

findVersion(apiOption) match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package uk.gov.hmrc.apidocumentation.v2.controllers
package uk.gov.hmrc.apidocumentation.controllers

import javax.inject.{Inject, Singleton}
import scala.concurrent.{ExecutionContext, Future}
Expand All @@ -26,12 +26,10 @@ import uk.gov.hmrc.play.bootstrap.frontend.controller.FrontendController

import uk.gov.hmrc.apidocumentation.ErrorHandler
import uk.gov.hmrc.apidocumentation.config.ApplicationConfig
import uk.gov.hmrc.apidocumentation.controllers.{DocumentationCrumb, HeaderNavigation, HomeCrumb, routes}
import uk.gov.hmrc.apidocumentation.models._
import uk.gov.hmrc.apidocumentation.services.{ApiDefinitionService, LoggedInUserService, NavigationService, XmlServicesService}
import uk.gov.hmrc.apidocumentation.util.ApplicationLogger
import uk.gov.hmrc.apidocumentation.v2.models._
import uk.gov.hmrc.apidocumentation.v2.views.html.FilteredIndexView
import uk.gov.hmrc.apidocumentation.views.html.documentationList.FilteredIndexView

@Singleton
class FilteredDocumentationIndexController @Inject() (
Expand All @@ -47,11 +45,9 @@ class FilteredDocumentationIndexController @Inject() (
) extends FrontendController(mcc)
with HeaderNavigation with ApplicationLogger with DocumentationCrumb with HomeCrumb {

// lazy val v2HomeCrumb =
// Crumb("Home", uk.gov.hmrc.apidocumentation.v2.controllers.routes.FilteredDocumentationIndexController.apiListIndexPage(List.empty, List.empty).url)
private lazy val apiDocCrumb = Crumb(
"API Documentation",
uk.gov.hmrc.apidocumentation.v2.controllers.routes.FilteredDocumentationIndexController.apiListIndexPage(List.empty, List.empty).url
uk.gov.hmrc.apidocumentation.controllers.routes.FilteredDocumentationIndexController.apiListIndexPage(List.empty, List.empty).url
)

private lazy val restApiDescriptionOverrides: Seq[RestApiDescriptionOverride] = RestApiDescriptionOverride.descriptionOverrides
Expand Down Expand Up @@ -109,7 +105,7 @@ class FilteredDocumentationIndexController @Inject() (

private def xmlApiToXmlDocumentation(api: XmlApiDocumentation) = {
val identifier = DocumentIdentifier(api.name)
val url = routes.ApiDocumentationController.renderXmlApiDocumentation(identifier.value, useV2 = Some(true)).url
val url = routes.ApiDocumentationController.renderXmlApiDocumentation(identifier.value).url
XmlDocumentation.fromXmlDocumentation(identifier, api, url)
}

Expand All @@ -118,7 +114,7 @@ class FilteredDocumentationIndexController @Inject() (
.versionsAsList
.sorted(WrappedApiDefinition.statusVersionOrdering)
.head.versionNbr
val url: String = routes.ApiDocumentationController.renderApiDocumentation(api.serviceName, defaultVersionNbr, useV2 = Some(true)).url
val url: String = routes.ApiDocumentationController.renderApiDocumentation(api.serviceName, defaultVersionNbr).url
RestDocumentation.fromApiDefinition(api, url, defaultVersionNbr, getRestApiDescriptionOverride(api.serviceName.value))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ class OpenApiDocumentationController @Inject() (
} yield (api, apiVersion, visibility)

findVersion(apiOption) match {
case Some((api, selectedVersion, VersionVisibility(_, _, true, _))) if selectedVersion.status == ApiStatus.RETIRED => badRequestPage
case Some((api, selectedVersion, VersionVisibility(_, _, true, _))) => renderDocumentationPage(api.name)
case Some((api, selectedVersion, VersionVisibility(ApiAccessType.PRIVATE, _, false, true))) => renderDocumentationPage(api.name) // TODO - makes no sense for oas/page
case Some((_, _, VersionVisibility(ApiAccessType.PRIVATE, false, _, _))) => badRequestPage
case _ => renderNotFoundPage
case Some((_, selectedVersion, VersionVisibility(_, _, true, _))) if selectedVersion.status == ApiStatus.RETIRED => badRequestPage
case Some((api, _, VersionVisibility(_, _, true, _))) => renderDocumentationPage(api.name)
case Some((api, _, VersionVisibility(ApiAccessType.PRIVATE, _, false, true))) => renderDocumentationPage(api.name) // TODO - makes no sense for oas/page
case Some((_, _, VersionVisibility(ApiAccessType.PRIVATE, false, _, _))) => badRequestPage
case _ => renderNotFoundPage
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,18 @@ import uk.gov.hmrc.play.bootstrap.frontend.controller.FrontendController
class RedirectController @Inject() (cc: MessagesControllerComponents)
extends FrontendController(cc) {

def `redirectToDocumentationIndexPage`(useV2: Option[Boolean]): Action[AnyContent] = {
def `redirectToDocumentationIndexPage`(): Action[AnyContent] = {

val redirectTo = if (useV2.getOrElse(false)) {
uk.gov.hmrc.apidocumentation.v2.controllers.routes.FilteredDocumentationIndexController.apiListIndexPage(List.empty, List.empty).url
} else routes.ApiDocumentationController.apiIndexPage(None, None, None).url
val redirectTo = routes.FilteredDocumentationIndexController.apiListIndexPage(List.empty, List.empty).url

Action.async { _ =>
Future.successful(MovedPermanently(redirectTo))
}
}

def redirectToApiDocumentationPage(service: ServiceName, version: ApiVersionNbr, endpoint: String, useV2: Option[Boolean]): Action[AnyContent] = {
def redirectToApiDocumentationPage(service: ServiceName, version: ApiVersionNbr, endpoint: String): Action[AnyContent] = {
val redirectTo = routes.ApiDocumentationController
.renderApiDocumentation(service, version, useV2)
.renderApiDocumentation(service, version)
.url
Action.async { _ =>
Future.successful(MovedPermanently(redirectTo))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import scala.util.{Failure, Success, Try}
import play.api.mvc.{PathBindable, QueryStringBindable}
import uk.gov.hmrc.apiplatform.modules.apis.domain.models.{ApiCategory, ServiceName}

import uk.gov.hmrc.apidocumentation.v2.models.DocumentationTypeFilter
import uk.gov.hmrc.apidocumentation.models.DocumentationTypeFilter

package object binders {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package uk.gov.hmrc.apidocumentation.v2.models
package uk.gov.hmrc.apidocumentation.models

import scala.io.Source

Expand All @@ -23,8 +23,6 @@ import uk.gov.hmrc.apiplatform.modules.apis.domain.models.{ApiCategory, ApiDefin
import uk.gov.hmrc.apiplatform.modules.common.domain.models.ApiVersionNbr
import uk.gov.hmrc.play.json.Union

import uk.gov.hmrc.apidocumentation.models.{DocumentationLabel, XmlApiDocumentation}

case class DocumentIdentifier(value: String) extends AnyVal

object DocumentIdentifier {
Expand Down
Loading