added class for remote urls to avoid Source.io

This commit is contained in:
Tony Tam 2012-10-11 10:49:39 -07:00
parent 735f5c61b3
commit cb534047ee
4 changed files with 30 additions and 6 deletions

View File

@ -54,6 +54,7 @@ class BasicObjcGenerator extends BasicGenerator {
"long" -> "NSNumber",
"double" -> "NSNumber",
"Array" -> "NSArray",
"List" -> "NSArray",
"object" -> "NSObject")
override def importMapping = Map(

View File

@ -18,10 +18,13 @@ package com.wordnik.swagger.codegen.util
import com.wordnik.swagger.model._
import java.net.URL
import java.io.InputStream
import scala.io._
import scala.collection.mutable.{ ListBuffer, HashMap, HashSet }
object ApiExtractor {
object ApiExtractor extends RemoteUrl {
def json = ScalaJsonUtil.getJsonMapper
def fetchApiListings(basePath: String, apis: List[ApiListingReference], apiKey: Option[String] = None): List[ApiListing] = {
@ -29,7 +32,7 @@ object ApiExtractor {
val str = basePath.startsWith("http") match {
case true => {
println("calling: " + ((basePath + api.path + apiKey.getOrElse("")).replaceAll(".\\{format\\}", ".json")))
Source.fromURL((basePath + api.path + apiKey.getOrElse("")).replaceAll(".\\{format\\}", ".json")).mkString
urlToString((basePath + api.path + apiKey.getOrElse("")).replaceAll(".\\{format\\}", ".json"))
}
case false => Source.fromFile((basePath + api.path).replaceAll(".\\{format\\}", ".json")).mkString
}
@ -42,7 +45,7 @@ object ApiExtractor {
val str = basePath.startsWith("http") match {
case true => {
println("calling: " + ((basePath + api.path + apiKey.getOrElse("")).replaceAll(".\\{format\\}", ".json")))
Source.fromURL((basePath + api.path + apiKey.getOrElse("")).replaceAll(".\\{format\\}", ".json")).mkString
urlToString((basePath + api.path + apiKey.getOrElse("")).replaceAll(".\\{format\\}", ".json"))
}
case false => Source.fromFile((basePath + api.path).replaceAll(".\\{format\\}", ".json")).mkString
}

View File

@ -0,0 +1,20 @@
package com.wordnik.swagger.codegen.util
import java.net.URL
import java.io.InputStream
import scala.io.Source
trait RemoteUrl {
def urlToString(url: String): String = {
var is: InputStream = null
try{
val conn = new URL(url).openConnection()
is = conn.getInputStream()
Source.fromInputStream(is).mkString
}
finally {
if(is != null) is.close()
}
}
}

View File

@ -18,14 +18,14 @@ package com.wordnik.swagger.codegen.util
import com.wordnik.swagger.model._
import scala.io.Source
import scala.io._
object ResourceExtractor {
object ResourceExtractor extends RemoteUrl {
def json = ScalaJsonUtil.getJsonMapper
def fetchListing(path: String, apiKey: Option[String] = None): ResourceListing = {
val str = path.startsWith("http") match {
case true => Source.fromURL(path + apiKey.getOrElse("")).mkString
case true => urlToString(path + apiKey.getOrElse(""))
case false => Source.fromFile(path).mkString
}
json.readValue(str, classOf[ResourceListing])