...
 
Commits (2)
......@@ -2,4 +2,5 @@ api {
login-endpoint = "user"
section-endpoint = "sections"
subnet-endpoint = "subnets"
address-endpoint = "addresses"
}
\ No newline at end of file
......@@ -3,6 +3,7 @@ package api
import errors.APIConfigError
import requests.RequestAuth
import utils.config.{APIConfig, UserConfig}
import errors.APIConfigError
/**
* A simple requests wrapper to send requests to phpipam server.
......@@ -10,13 +11,18 @@ import utils.config.{APIConfig, UserConfig}
object Client {
/* Base URL */
private val baseURL: String = UserConfig.getApiUrl.get
private val appName: String = UserConfig.getAppName.get
private val baseURL: String = UserConfig.getApiUrl.getOrElse(
throw new APIConfigError("Server base URL is missing")
)
private val appName: String = UserConfig.getAppName.getOrElse(
throw new APIConfigError("App name is missing")
)
/* Endpoints */
private val loginEndpoint: String = APIConfig.getLoginEndpoint
private val sectionEndpoint: String = APIConfig.getSectionEndpoint
private val subnetEndpoint: String = APIConfig.getSubnetEndpoint
private val addressEndpoint: String = APIConfig.getAddressEndpoint
/* Various options */
private val verifySsl: Boolean = UserConfig.getVerifySsl
......@@ -137,13 +143,38 @@ object Client {
}
/**
* Send a request to the api to retrieve a specific subnet with unqieu `id`.
* Send a request to the api to retrieve a specific subnet with unique `id`.
*
* @param id The unique ID of the subnet
* @return A tuple containing the status code and the returned json
* @return A tuple containing the status code and the returned json
*/
def getSubnet(id: String): (Int, String) = {
val endpoint = f"$baseURL/$appName/$subnetEndpoint/$id"
sendRequest(endpoint, method="GET")
}
/**** ADDRESSES ****/
/**
* Send a request to the api to retrieve every address with unique of a
* specific subnet with unique`id`.
*
* @param id The unique ID of the subnet
* @return A tuple containing the status code and the returned json
*/
def getAddressesFromSubnet(id: String): (Int, String) = {
val endpoint = f"$baseURL/$appName/$subnetEndpoint/$id/$addressEndpoint"
sendRequest(endpoint, method="GET")
}
/**
* Send a request to the api to retrieve a specific address with unique `id`.
*
* @param id The unique ID of the address
* @return A tuple containing the status code and the returned json
*/
def getAddress(id: String): (Int, String) = {
val endpoint = f"$baseURL/$appName/$addressEndpoint/$id"
sendRequest(endpoint, method="GET")
}
}
package api.objects
import api.Client
import play.api.libs.json.{Json, Reads}
case class Address(
id: String,
subnetId: String,
ip: String,
is_gateway: Option[String],
description: Option[String],
hostname: Option[String],
mac: Option[String],
deviceId: Option[String],
customer_id: Option[String]
){
// The API returns a string (0/1) OR null.
val is_gateway_bool: Boolean = is_gateway match {
case Some("1") => true
case _ => false
}
}
/**
* The companion object exposing methods to operate with addresses and the
* API.
*/
object Address extends APIObject[Address] {
override protected implicit val decoder: Reads[Address] = Json.reads[Address]
/**
* Retrieve the address with unique `id`.
*
* @param id The unique ID of the address
* @return An `Address` object
*/
def getAddress(id: Int): Address = getAddress(id.toString)
def getAddress(id: String): Address = {
val (_, json) = Client.getAddress(id)
extractFromJson(json)
}
}
......@@ -23,7 +23,8 @@ case class AuthToken(token: String, expires: String) {
* API.
*/
object AuthToken extends APIObject[AuthToken] {
override val decoder: Reads[AuthToken] = Json.reads[AuthToken]
override protected implicit val decoder: Reads[AuthToken] = Json
.reads[AuthToken]
/**
* Retrieve a user token from the api.
......
......@@ -25,7 +25,7 @@ case class Section(
* API.
*/
object Section extends APIObject[Section] {
override implicit val decoder: Reads[Section] = Json.reads[Section]
override protected implicit val decoder: Reads[Section] = Json.reads[Section]
/**
* Get the section with unique `id`.
......
......@@ -27,7 +27,7 @@ case class Subnet(
/**
* The companion object exposing methods to operate with auth tokens and the
* The companion object exposing methods to operate with subnets and the
* API.
*/
object Subnet extends APIObject[Subnet] {
......
package api.objects.collections
import api.Client
import api.objects.Address
import play.api.libs.json.{Json, Reads}
/**
* An object to retrieve a list of addresses.
*/
object Addresses extends APIObjectCollection[Address]{
override protected implicit val decoder: Reads[Address] = Json.reads[Address]
/**
* Get all addresses of a subnet from the API.
*
* @return A list of `Address`
*/
def getSubnetAddresses(id: Int): List[Address] = getSubnetAddresses(id.toString)
def getSubnetAddresses(id: String): List[Address] = {
val (_, json) = Client.getAddressesFromSubnet(id)
extractFromJson(json)
}
}
......@@ -5,16 +5,16 @@ import api.objects.Section
import play.api.libs.json.{Json, Reads}
/**
* An object to retrieve a list of section
* An object to retrieve a list of section.
*/
object Sections extends APIObjectCollection[Section] {
protected implicit val decoder: Reads[Section] = Json.reads[Section]
override protected implicit val decoder: Reads[Section] = Json.reads[Section]
/**
* Get all sections from the API.
*
* @return A list of `Section`.
* @return A list of `Section`
*/
def getSections: List[Section] = {
val (_, json) = Client.getSections
......
......@@ -9,15 +9,15 @@ import play.api.libs.json.{Json, Reads}
*/
object Subnets extends APIObjectCollection[Subnet] {
protected implicit val decoder: Reads[Subnet] = Json.reads[Subnet]
override protected implicit val decoder: Reads[Subnet] = Json.reads[Subnet]
/**
* Get all subnets of a section from the API.
*
* @return A list of `Section`.
*/
def getSubnetsFromSection(id: Int): List[Subnet] = getSubnetsFromSection(id.toString)
def getSubnetsFromSection(id: String): List[Subnet] = {
def getSectionSubnets(id: Int): List[Subnet] = getSectionSubnets(id.toString)
def getSectionSubnets(id: String): List[Subnet] = {
val (_, json) = Client.getSubnetsFromSection(id)
extractFromJson(json)
}
......
......@@ -10,9 +10,10 @@ import pureconfig.generic.auto._
object APIConfig {
case class API(
loginEndpoint: String,
sectionEndpoint: String,
subnetEndpoint: String
loginEndpoint: String,
sectionEndpoint: String,
subnetEndpoint: String,
addressEndpoint: String
)
case class Config(api: API)
......@@ -25,4 +26,5 @@ object APIConfig {
def getLoginEndpoint: String = config.api.loginEndpoint
def getSectionEndpoint: String = config.api.sectionEndpoint
def getSubnetEndpoint: String = config.api.subnetEndpoint
def getAddressEndpoint: String = config.api.addressEndpoint
}