Skip to content

Add endpoint to refresh the access_token #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 10, 2018
Merged
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
13 changes: 13 additions & 0 deletions src/main/scala/com/codacy/client/bitbucket/v2/AccessToken.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.codacy.client.bitbucket.v2

import play.api.libs.functional.syntax._
import play.api.libs.json.{Reads, __}

case class AccessToken(access_token: String, refresh_token: String)

object AccessToken {
implicit val reader: Reads[AccessToken] = (
(__ \ "access_token").read[String] and
(__ \ "refresh_token").read[String]
)(AccessToken.apply _)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.codacy.client.bitbucket.v2

import play.api.libs.json.{Json, Writes}

object Authorization {

sealed trait RefreshCredentials

case class RefreshToken(refresh_token: String) extends RefreshCredentials {
val grant_type = "refresh_token"
}

object RefreshToken {
implicit val writer: Writes[RefreshToken] = Json.writes[RefreshToken]
}

object RefreshCredentials {
implicit val writer: Writes[RefreshCredentials] =
Writes[RefreshCredentials] {
case c: RefreshToken => Json.toJson(c)(RefreshToken.writer)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.codacy.client.bitbucket.v2.service

import com.codacy.client.bitbucket.client.{BitbucketClient, Request, RequestResponse}
import com.codacy.client.bitbucket.v2.AccessToken
import com.codacy.client.bitbucket.v2.Authorization.RefreshCredentials
import play.api.libs.json._

class AuthorizationServices(client: BitbucketClient) {

/*
* Gets new AccessToken with the RefreshCredentials
*
*/
def refreshAccessToken(
credentials: RefreshCredentials): RequestResponse[AccessToken] = {
val url = s"https://bitbucket.org/site/oauth2/access_token"

val values = Json.toJson[RefreshCredentials](credentials)

client.postJson(Request(url, classOf[AccessToken]), values)
}
}