Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add methods to fetch degrees by their acronyms.
Update dependencies.
- Loading branch information
Showing
12 changed files
with
73 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
sbt.version=1.3.10 | ||
sbt.version=1.4.6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,58 @@ | ||
package org.fenixedu.sdk.services | ||
|
||
import cats.effect.Sync | ||
import cats.effect.Concurrent | ||
import cats.implicits._ | ||
import org.fenixedu.sdk.models.{CourseRef, Degree} | ||
import org.http4s.Uri | ||
import org.http4s.client.Client | ||
|
||
final class Degrees[F[_]: Sync](baseUri: Uri)(implicit client: Client[F]) { | ||
final class Degrees[F[_]: Concurrent](baseUri: Uri)(implicit client: Client[F]) { | ||
val uri: Uri = baseUri / "degrees" | ||
|
||
/** @return the information for all degrees. If no academicTerm is defined it returns the degree information for the currentAcademicTerm. */ | ||
def list(academicTerm: Option[String] = None): F[List[Degree]] = client.expect(uri.+??("academicTerm", academicTerm)) | ||
def list(academicTerm: Option[String] = None): F[List[Degree]] = | ||
client.expect(uri.withOptionQueryParam("academicTerm", academicTerm)) | ||
|
||
/** @return the information for the `id` degree. If no academicTerm is defined it returns the degree information for the currentAcademicTerm. */ | ||
def get(id: String, academicTerm: Option[String] = None): F[Degree] = client.expect((uri / id).+??("academicTerm", academicTerm)) | ||
def get(id: String, academicTerm: Option[String] = None): F[Degree] = | ||
client.expect((uri / id).withOptionQueryParam("academicTerm", academicTerm)) | ||
|
||
/** @return the information for the degree with `acronym` on the specified `academicTerm`. | ||
* If no academicTerm is defined it returns the degree information for the currentAcademicTerm. */ | ||
def getByAcronym(acronym: String, academicTerm: Option[String] = None): F[Option[Degree]] = { | ||
// The API is not very expressive, so we need to do it using list. | ||
list(academicTerm).map(_.find(_.acronym == acronym)) | ||
} | ||
/** @return the information for the degree with `acronym` on the specified `academicTerm`, assuming the Degree exists. | ||
* If no academicTerm is defined it returns the degree information for the currentAcademicTerm. */ | ||
def applyByAcronym(acronym: String, academicTerm: Option[String] = None): F[Degree] = | ||
getByAcronym(acronym, academicTerm).flatMap { | ||
case Some(degree) => Concurrent[F].pure(degree) | ||
case None => Concurrent[F].raiseError(new NoSuchElementException(s"""Could not find Degree with acronym "$acronym".""")) | ||
} | ||
|
||
|
||
/** @return the information for a degree’s courses. If no academicTerm is defined it returns the degree information for the currentAcademicTerm. */ | ||
def courses(id: String, academicTerm: Option[String] = None): F[List[CourseRef]] = client.expect((uri / id / "courses").+??("academicTerm", academicTerm)) | ||
def courses(id: String, academicTerm: Option[String] = None): F[List[CourseRef]] = | ||
client.expect((uri / id / "courses").withOptionQueryParam("academicTerm", academicTerm)) | ||
|
||
/** @return the information for the degree with `acronym` on the specified `academicTerm`. | ||
* If no academicTerm is defined it returns the degree information for the currentAcademicTerm. */ | ||
def getCoursesByAcronym(acronym: String, academicTerm: Option[String] = None): F[Option[List[CourseRef]]] = | ||
for { | ||
courseOpt <- getByAcronym(acronym, academicTerm) | ||
courses <- courseOpt match { | ||
case Some(degree) => courses(degree.id, academicTerm).map(Option.apply) | ||
case None => Concurrent[F].pure(Option.empty) | ||
} | ||
} yield courses | ||
|
||
/** @return the information for the degree with `acronym` on the specified `academicTerm`, assuming the Degree exists. | ||
* If no academicTerm is defined it returns the degree information for the currentAcademicTerm. */ | ||
def applyCoursesByAcronym(acronym: String, academicTerm: Option[String] = None): F[List[CourseRef]] = | ||
getCoursesByAcronym(acronym, academicTerm).flatMap { | ||
case Some(degree) => Concurrent[F].pure(degree) | ||
case None => Concurrent[F].raiseError(new NoSuchElementException(s"""Could not find Degree with acronym "$acronym".""")) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
package org.fenixedu.sdk | ||
|
||
import cats.Applicative | ||
import cats.effect.Sync | ||
import cats.effect.Concurrent | ||
import io.circe.{Decoder, Encoder, Printer} | ||
import org.http4s.{EntityDecoder, EntityEncoder, circe} | ||
|
||
package object services { | ||
val jsonPrinter: Printer = Printer.noSpaces.copy(dropNullValues = true) | ||
implicit def jsonEncoder[F[_]: Applicative, A: Encoder]: EntityEncoder[F, A] = circe.jsonEncoderWithPrinterOf[F, A](jsonPrinter) | ||
implicit def jsonDecoder[F[_]: Sync, A: Decoder]: EntityDecoder[F, A] = circe.accumulatingJsonOf[F, A] | ||
implicit def jsonDecoder[F[_]: Concurrent, A: Decoder]: EntityDecoder[F, A] = circe.accumulatingJsonOf[F, A] | ||
|
||
// Without this decoding to Unit wont work. This makes the EntityDecoder[F, Unit] defined in EntityDecoder companion object | ||
// have a higher priority than the jsonDecoder defined above. https://github.com/http4s/http4s/issues/2806 | ||
implicit def void[F[_]: Sync]: EntityDecoder[F, Unit] = EntityDecoder.void | ||
implicit def void[F[_]: Concurrent]: EntityDecoder[F, Unit] = EntityDecoder.void | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
version := "0.1.0" | ||
version := "0.2.0-SNAPSHOT" |