Skip to content

Commit

Permalink
Merge pull request #30 from timo-schmid/endpoint-search-streaming
Browse files Browse the repository at this point in the history
Endpoints Search.repositories and Search.users
  • Loading branch information
ChristopherDavenport authored Dec 6, 2019
2 parents 4b2a4aa + 7039e7a commit 62e869a
Show file tree
Hide file tree
Showing 20 changed files with 11,336 additions and 203 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ lazy val commonSettings = Seq(

"io.circe" %% "circe-core" % circeV,
"io.circe" %% "circe-literal" % circeV % Test,
"io.circe" %% "circe-parser" % circeV % Test,
// "io.circe" %% "circe-generic" % circeV,
// "io.circe" %% "circe-parser" % circeV,

// "io.chrisdavenport" %% "log4cats-core" % log4catsV,
//
Expand Down
16 changes: 16 additions & 0 deletions core/src/main/scala/io/chrisdavenport/github/data/Order.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.chrisdavenport.github.data

sealed trait Order

object Order {

case object Ascending extends Order
case object Descending extends Order

def toOptionalParam(order: Order): Option[String] =
order match {
case Ascending => Some("asc")
case Descending => Some("desc")
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,4 @@ object SearchResult {
cursor.downField("items").as[List[A]]
).mapN(SearchResult.apply)

sealed trait Sort

object Sort {

case object Stars extends Sort
case object Forks extends Sort
case object HelpWantedIssues extends Sort
case object Updated extends Sort
case object BestMatch extends Sort

def toOptionalParam(sort: Sort): Option[String] =
sort match {
case Stars => Some("stars")
case Forks => Some("forks")
case HelpWantedIssues => Some("help-wanted-issues")
case Updated => Some("updated")
case BestMatch => None
}

}

sealed trait Order

object Order {

case object Ascending extends Order
case object Descending extends Order

def toOptionalParam(order: Order): Option[String] =
order match {
case Ascending => Some("asc")
case Descending => Some("desc")
}

}

}
31 changes: 31 additions & 0 deletions core/src/main/scala/io/chrisdavenport/github/data/Sort.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.chrisdavenport.github.data

sealed trait Sort

object Sort {

case object BestMatch extends Repository with User
case object Stars extends Repository
case object Forks extends Repository
case object HelpWantedIssues extends Repository
case object Updated extends Repository
case object Followers extends User
case object Repositories extends User
case object Joined extends User

sealed trait Repository extends io.chrisdavenport.github.data.Sort
sealed trait User extends io.chrisdavenport.github.data.Sort

def toOptionalParam(sort: Sort): Option[String] =
sort match {
case BestMatch => None
case Stars => Some("stars")
case Forks => Some("forks")
case HelpWantedIssues => Some("help-wanted-issues")
case Updated => Some("updated")
case Followers => Some("followers")
case Repositories => Some("repositories")
case Joined => Some("joined")
}

}
3 changes: 0 additions & 3 deletions core/src/main/scala/io/chrisdavenport/github/data/Teams.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package io.chrisdavenport.github.data

import cats.implicits._
import org.http4s.Uri
import org.http4s.circe._
import java.time.Instant
import io.circe._
import io.circe.syntax._

Expand Down
20 changes: 10 additions & 10 deletions core/src/main/scala/io/chrisdavenport/github/data/Users.scala
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,15 @@ object Users {
name: Option[String],
email: Option[String],
company: Option[String],
createdAt: Instant,
createdAt: Option[Instant],
blog: Option[String],
location: Option[String],
bio: Option[String],
hireable: Option[Boolean],
publicRepos: Int,
publicGists: Int,
followers: Int,
following: Int,
publicRepos: Option[Int],
publicGists: Option[Int],
followers: Option[Int],
following: Option[Int],
uri: Uri,
htmlUri: Uri,
avatarUri: Uri,
Expand All @@ -112,15 +112,15 @@ object Users {
c.downField("name").as[Option[String]],
c.downField("email").as[Option[String]],
c.downField("company").as[Option[String]],
c.downField("created_at").as[Instant],
c.downField("created_at").as[Option[Instant]],
c.downField("blog").as[Option[String]],
c.downField("location").as[Option[String]],
c.downField("bio").as[Option[String]],
c.downField("hireable").as[Option[Boolean]],
c.downField("public_repos").as[Int],
c.downField("public_gists").as[Int],
c.downField("followers").as[Int],
c.downField("following").as[Int],
c.downField("public_repos").as[Option[Int]],
c.downField("public_gists").as[Option[Int]],
c.downField("followers").as[Option[Int]],
c.downField("following").as[Option[Int]],
c.downField("url").as[Uri],
c.downField("html_url").as[Uri],
c.downField("avatar_url").as[Uri]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package io.chrisdavenport.github.endpoints

import cats.data._
import cats.effect._
import fs2.Stream
import io.chrisdavenport.github.data.Repositories._
import org.http4s._
import org.http4s.implicits._
import org.http4s.client.Client
import io.chrisdavenport.github.Auth
import io.chrisdavenport.github.data.SearchResult
import io.chrisdavenport.github.data.SearchResult.{Order, Sort}
import io.chrisdavenport.github.data.{Order, SearchResult, Sort}
import io.chrisdavenport.github.data.Users.User
import io.chrisdavenport.github.internals.GithubMedia._
import io.chrisdavenport.github.internals.RequestConstructor

Expand All @@ -17,20 +17,47 @@ object Search {
/**
* Repository Search Endpoint
* https://developer.github.com/v3/search/#search-repositories
* @param q The search query string
* @param sort The sorting method
* @param order The sorting order
* @param auth The authentication mechanism
* @tparam F The effect type
*/
def repository[F[_]: Sync](
q: String,
sort: Option[Sort],
sort: Option[Sort.Repository],
order: Option[Order],
auth: Option[Auth]
): Kleisli[F, Client[F], SearchResult[Repo]] =
RequestConstructor.runRequestWithNoBody[F, SearchResult[Repo]](
): Kleisli[Stream[F, *], Client[F], SearchResult[Repo]] =
RequestConstructor.runPaginatedRequest[F, SearchResult[Repo]](
auth,
Method.GET,
(uri"search" / "repositories")
.withQueryParam("q", q)
.withOptionQueryParam("sort", sort.flatMap(Sort.toOptionalParam))
.withOptionQueryParam("order", order.flatMap(Order.toOptionalParam))
)

/**
* User Search Endpoint
* https://developer.github.com/v3/search/#search-users
* @param q The search query string
* @param sort The sorting method
* @param order The sorting order
* @param auth The authentication mechanism
* @tparam F The effect type
*/
def users[F[_]: Sync](
q: String,
sort: Option[Sort.User],
order: Option[Order],
auth: Option[Auth]
): Kleisli[Stream[F, *], Client[F], SearchResult[User]] =
RequestConstructor.runPaginatedRequest[F, SearchResult[User]](
auth,
(uri"search" / "users")
.withQueryParam("q", q)
.withOptionQueryParam("sort", sort.flatMap(Sort.toOptionalParam))
.withOptionQueryParam("order", order.flatMap(Order.toOptionalParam))
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,17 @@ object RequestConstructor {
)
}

final class GithubError private[RequestConstructor] (val status: Status, val body: String) extends Exception(
s"Github Error Occured- Status:$status Body: $body"
)
final class GithubError private[github] (val status: Status, val body: String) extends Exception(
s"Github Error Occured - Status: $status Body: $body"
) {

override def equals(obj: Any): Boolean =
obj match {
case other: GithubError if other.status === status && other.body === body => true
case _ => false
}

}

private val RE_LINK: Regex = "[\\s]*<(.*)>; rel=\"(.*)\"".r

Expand Down
Loading

0 comments on commit 62e869a

Please sign in to comment.