BasicGenerator refactoring

This commit is contained in:
Bartosz Kowalik 2014-01-30 10:35:30 +01:00 committed by Bartosz Kowalik
parent 7414a9d56a
commit 48a07d031e

View File

@ -53,21 +53,8 @@ abstract class BasicGenerator extends CodegenConfig with PathUtil {
throw new RuntimeException("Need url to resource listing as argument. You can also specify VM Argument -DfileMap=/path/to/folder/containing.resources.json/") throw new RuntimeException("Need url to resource listing as argument. You can also specify VM Argument -DfileMap=/path/to/folder/containing.resources.json/")
} }
val host = args(0) val host = args(0)
val authorization = { val apiKey = if(args.length > 1) Some(args(1)) else None
Option(System.getProperty("header")) match { val authorization = authenticate(apiKey)
case Some(e) => {
// this is ugly and will be replaced with proper arg parsing like in ScalaAsyncClientGenerator soon
val authInfo = e.split(":")
Some(ApiKeyValue(authInfo(0), "header", authInfo(1)))
}
case _ => {
if (args.length > 1) {
Some(ApiKeyValue("api_key", "query", args(1)))
}
else None
}
}
}
val doc = { val doc = {
try { try {
ResourceExtractor.fetchListing(getResourcePath(host), authorization) ResourceExtractor.fetchListing(getResourcePath(host), authorization)
@ -76,13 +63,7 @@ abstract class BasicGenerator extends CodegenConfig with PathUtil {
} }
} }
implicit val basePath = getBasePath(host, doc.basePath) val apis: List[ApiListing] = getApis(host, doc, authorization)
println("base path is " + basePath)
val apiReferences = doc.apis
if (apiReferences == null)
throw new Exception("No APIs specified by resource")
val apis = ApiExtractor.fetchApiListings(doc.swaggerVersion, basePath, apiReferences, authorization)
SwaggerSerializers.validationMessages.filter(_.level == ValidationMessage.ERROR).size match { SwaggerSerializers.validationMessages.filter(_.level == ValidationMessage.ERROR).size match {
case i: Int if i > 0 => { case i: Int if i > 0 => {
@ -136,6 +117,32 @@ abstract class BasicGenerator extends CodegenConfig with PathUtil {
codegen.writeSupportingClasses(operationMap, allModels.toMap) codegen.writeSupportingClasses(operationMap, allModels.toMap)
} }
def getApis(host: String, doc: ResourceListing, authorization: Option[ApiKeyValue]): List[ApiListing] = {
implicit val basePath = getBasePath(host, doc.basePath)
println("base path is " + basePath)
val apiReferences = doc.apis
if (apiReferences == null)
throw new Exception("No APIs specified by resource")
ApiExtractor.fetchApiListings(doc.swaggerVersion, basePath, apiReferences, authorization)
}
def authenticate(apiKey: Option[String]): Option[ApiKeyValue] = {
Option(System.getProperty("header")) match {
case Some(e) => {
// this is ugly and will be replaced with proper arg parsing like in ScalaAsyncClientGenerator soon
val authInfo = e.split(":")
Some(ApiKeyValue(authInfo(0), "header", authInfo(1)))
}
case _ => {
apiKey.map{ key =>
Some(ApiKeyValue("api_key", "query", key))
}.getOrElse(None)
}
}
}
def extractApiOperations(apiListings: List[ApiListing], allModels: HashMap[String, Model] )(implicit basePath:String) = { def extractApiOperations(apiListings: List[ApiListing], allModels: HashMap[String, Model] )(implicit basePath:String) = {
val output = new ListBuffer[(String, String, Operation)] val output = new ListBuffer[(String, String, Operation)]
apiListings.foreach(apiDescription => { apiListings.foreach(apiDescription => {