Commit ff57a8a0 authored by Ignamarte's avatar Ignamarte

Add Section object

parent 313f3a65
......@@ -94,7 +94,13 @@ object Client {
}
def getSections: (Int, String) = {
sendRequest(f"$baseURL$appName$sectionEndpoint", method = "GET")
val endpoint = f"$baseURL$appName$sectionEndpoint"
sendRequest(endpoint, method = "GET")
}
def getSection(id: String): (Int, String) =
{
val endpoint = f"$baseURL$appName$sectionEndpoint$id/"
sendRequest(endpoint, method= "GET")
}
}
......@@ -32,9 +32,7 @@ trait APIObject[A] {
Json.fromJson[Wrapper](json) match {
case JsSuccess(r, _) => r.data
case e: JsError => throw APIError(
422, f"Errors: ${JsError toJson e}"
)
case e: JsError => throw APIError(422, f"Errors: ${JsError toJson e}")
}
}
}
......@@ -25,6 +25,13 @@ case class AuthToken(token: String, expires: String) {
object AuthToken extends APIObject[AuthToken] {
override val decoder: Reads[AuthToken] = Json.reads[AuthToken]
/**
* Retrieve a user token from the api.
*
* @param user An existing username
* @param pass The password matching the username
* @return An `AuthToken` object
*/
def getAuthToken(user: String, pass: String): AuthToken = {
val (_, json) = api.Client.requestAuthToken(user, pass)
extractFromJson(json)
......
package api.objects
import play.api.libs.json.{Json, Reads}
/**
* A single php ipam section.
*
* @param id The unique ID of the section
* @param name The name of the section
* @param description The description of the section
* @param masterSection The parent section
* @param DNS Main DNS for this section
*/
case class Section(
id: String,
name: String,
description: Option[String],
masterSection: String,
DNS: Option[String]
)
/**
* The companion object exposing methods to operate with auth tokens and the
* API.
*/
object Section extends APIObject[Section] {
override val decoder: Reads[Section] = Json.reads[Section]
/**
* Get the section with unique `id`.
*
* @param id The unique ID of the section
* @return A `Section` object
*/
def getSection(id: Int): Section = getSection(id.toString)
def getSection(id: String): Section = {
val (_, json) = api.Client.getSection(id)
extractFromJson(json)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment