Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ Scrava is currently built for Scala 2.11 and 2.12. To use scrava in an sbt proje
val client = new ScravaClient("[accessToken]")
val athlete = client.retrieveAthlete()

###Build Client without token
val (client, athlete) = clientFactory.instance("[APP_ID]", "[APP_SECRET]", "[CODE]")

**Note:** Most functions have optional ID parameters (i.e, `retrieveAthlete()`). If no ID is provided, the function will operate based on the currently authenticated user. Therefore, `retrieveAthlete()` will return the athlete profile of the currently authenticated user (based on the access token provided), and `listAthleteFriends()` will return the list of friends of the currently authenticated athlete.

To retrieve perform these functions for a particular athlete/activity/etc, simply pass in the respective ID: `retrieveAthlete(Some([athleteID]))` or `listAthleteFriends(Some([athleteID]))`.
Expand Down
33 changes: 33 additions & 0 deletions src/main/scala/kiambogo/scrava/ClientFactory.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,42 @@
package kiambogo.scrava


import kiambogo.scrava.models._
import net.liftweb.json.parse

import scala.util.{Failure, Success, Try}
import scalaj.http.Http

trait ClientFactory {
def instance(token: String): Client

def instance(client_id: String, client_secret: String, code: String): (Client, AthleteSummary)
}

class ClientFactoryImpl extends ClientFactory {

override def instance(token: String): Client = new ScravaClient(token)

/**
* Return a StravaClient from http://strava.github.io/api/v3/oauth/#post-token
*
* @param client_id application’s ID, obtained during registration
* @param client_secret application’s secret, obtained during registration
* @param code authorization code (from callback URL)
* @return a ScravaClient and the AthleteSummary returned by the API.
*/
override def instance(client_id: String, client_secret: String, code: String): (Client, AthleteSummary) = {
val request = Http(s"https://www.strava.com/oauth/token").method("post")
.postForm(Seq(("client_id", client_id), ("client_secret", client_secret), ("code", code)))
implicit val formats = net.liftweb.json.DefaultFormats

Try {
parse(request.asString.body).extract[TokenExchange]
} match {
case Success(TokenExchange(access_token, token_type, athlete)) =>
(new ScravaClient(access_token), athlete)
case Failure(error) =>
throw new RuntimeException(s"Could not get Token Exchange Athlete: $error")
}
}
}
3 changes: 3 additions & 0 deletions src/main/scala/kiambogo/scrava/models/TokenExchange.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package kiambogo.scrava.models

case class TokenExchange(access_token:String, token_type:String, athlete: AthleteSummary)
19 changes: 19 additions & 0 deletions src/test/scala/kiambogo/scrava/IntegrationTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,23 @@ class IntegrationTest extends FlatSpec with Matchers {
stream(0).asInstanceOf[LatLng].resolution should equal(Some("high"))
stream(1).asInstanceOf[Distance].original_size should equal(Some(114))
}

/**
* Need to provide a valid APP_ID, Secret and Code
* The code can be generate with
* https://www.strava.com/oauth/authorize?client_id=APP_ID&response_type=code&redirect_uri=http://127.0.0.1/token_exchange&scope=write&state=mystate&approval_prompt=force
*/
ignore should "Make a Token Exchange" in {

val clientFactory = new ClientFactoryImpl
val clientId = "APP_ID"
val client_secret = "SEEEECRET"
val code = "CODE"
val (client, athlete) = clientFactory.instance(clientId, client_secret, code)

athlete.firstname should equal("Luis Miguel")
client.listAthleteClubs

}

}