Skip to content

Instantly share code, notes, and snippets.

@DhavalDalal
Last active April 15, 2019 03:30
Show Gist options
  • Save DhavalDalal/1fa3862b4272b67d464a296579c414ac to your computer and use it in GitHub Desktop.
Save DhavalDalal/1fa3862b4272b67d464a296579c414ac to your computer and use it in GitHub Desktop.
Free 50 (Scala)

Free 50 (Scala)

  • Incoming Requests to various URLs of a web-service are modelled as:
    • Visits - A tuple containing Client IP and number of visits made from that IP.
    import java.net.InetAddress
    type Visits = (InetAddress, Int)
    
    • Request - An ADT containing the web-service URL with a list of clients that made the visits.
    import java.net.URL
    case class Request(val url: URL, val from: List[Visits])
    
  • Clients of Geographic Services can make 50 free calls in a day, more than that result in blocking that IP.
  • Refer to FreeFifty.scala for web-request data and implement the bodies marked with ???
import java.net.{InetAddress, URL}
type Visits = (InetAddress, Int)
case class Request(val url: URL, val from: List[Visits])
val hits: List[Request] = List(
Request(new URL("https://geographic-services.com/api/weather"),
List((InetAddress.getByName("56.22.11.1"), 3),
(InetAddress.getByName("22.11.1.123"), 4))),
Request(new URL("https://geographic-services.com/api/places/nearby/London"),
List((InetAddress.getByName("182.124.0.2"), 56),
(InetAddress.getByName("87.12.191.12"), 35))),
Request(new URL("https://geographic-services.com/api/places"), Nil),
Request(new URL("https://geographic-services.com/api/weather/IN/Mumbai"),
List((InetAddress.getByName("56.22.11.1"), 8),
(InetAddress.getByName("87.12.191.12"), 25))),
)
// Using Either implement the following:
val interactions: List[Either[URL, List[Visits]]] = ???
// Using interactions implement the following:
val clientIpsToBeBlocked: List[Visits] = ???
println("Block these IPs => " + clientIpsToBeBlocked) // List((/87.12.191.12,60), (/182.124.0.2,56))
val unusedUrls: List[URL] = ???
println("unused URLs => " + unusedUrls) // List(https://geographic-services.com/api/places)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment