Skip to content

Commit

Permalink
Add deleteUser support
Browse files Browse the repository at this point in the history
  • Loading branch information
luismfonseca committed Feb 7, 2019
1 parent ba523c4 commit c128bee
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

### Added
- Support for [Authenticated Users](https://docs.pusher.com/beams/concepts/authenticated-users):
`generateToken` and `publishToUsers` functions were added
`generateToken`, `publishToUsers` and `deleteUser` functions were added

### Changed
- Renamed `publish` method to `publishToInterest`
Expand Down
42 changes: 39 additions & 3 deletions src/main/kotlin/com/pusher/pushnotifications/PushNotifications.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package com.pusher.pushnotifications
import com.google.gson.Gson
import io.jsonwebtoken.Jwts
import io.jsonwebtoken.SignatureAlgorithm
import org.apache.http.client.methods.HttpDelete
import java.io.IOException
import java.net.URISyntaxException
import org.apache.http.client.methods.HttpPost
import org.apache.http.entity.StringEntity
import org.apache.http.impl.client.HttpClients
import org.apache.http.util.EntityUtils
import java.net.URLEncoder
import java.time.LocalDateTime
import java.time.ZoneOffset
import java.util.*
Expand Down Expand Up @@ -38,7 +40,7 @@ class PushNotifications(private val instanceId: String, private val secretKey: S
private val maxRequestInterestsAllowed = 100
private val userIdMaxLength = 164
private val maxRequestUsersAllowed = 1000
private val baseURL = "https://$instanceId.pushnotifications.pusher.com/publish_api/v1"
private val baseURL = "https://$instanceId.pushnotifications.pusher.com"

init {
if (instanceId.isEmpty()) {
Expand Down Expand Up @@ -114,7 +116,7 @@ class PushNotifications(private val instanceId: String, private val secretKey: S
publishRequestWithInterests.put("interests", interests)

val client = HttpClients.createDefault()
val httpPost = HttpPost("$baseURL/instances/$instanceId/publishes/interests")
val httpPost = HttpPost("$baseURL/publish_api/v1/instances/$instanceId/publishes/interests")
httpPost.setEntity(StringEntity(gson.toJson(publishRequestWithInterests)))
httpPost.setHeader("Accept", "application/json")
httpPost.setHeader("Content-Type", "application/json")
Expand Down Expand Up @@ -160,7 +162,7 @@ class PushNotifications(private val instanceId: String, private val secretKey: S
publishRequestWithUsers.put("users", users)

val client = HttpClients.createDefault()
val httpPost = HttpPost("$baseURL/instances/$instanceId/publishes/users")
val httpPost = HttpPost("$baseURL/publish_api/v1/instances/$instanceId/publishes/users")
httpPost.setEntity(StringEntity(gson.toJson(publishRequestWithUsers)))
httpPost.setHeader("Accept", "application/json")
httpPost.setHeader("Content-Type", "application/json")
Expand All @@ -181,6 +183,40 @@ class PushNotifications(private val instanceId: String, private val secretKey: S
}
}

/**
* Remove the user with the given ID (and all of their devices) from the Pusher Beams database.
* The user will no longer receive any notifications. This action cannot be undone.
*
* @param userId id of the user to be deleted
*/
fun deleteUser(userId: String) {
if (userId.length > userIdMaxLength) {
throw IllegalArgumentException(
"User id ($userId) is too long (expected less than ${userIdMaxLength + 1}, got ${userId.length})")
}

val userIdURLEncoded = URLEncoder.encode(userId, "UTF-8")

val httpDelete = HttpDelete("$baseURL/customer_api/v1/instances/$instanceId/users/$userIdURLEncoded")
httpDelete.setHeader("Authorization", String.format("Bearer %s", this.secretKey))

val client = HttpClients.createDefault()
val response = client.execute(httpDelete)
val responseBody = EntityUtils.toString(response.entity, "UTF-8")
val statusCode = response.statusLine.statusCode

when (statusCode) {
401 -> throw PusherAuthError(extractErrorDescription(responseBody))
404 -> throw PusherMissingInstanceError(extractErrorDescription(responseBody))
429 -> throw PusherTooManyRequestsError(extractErrorDescription(responseBody))
in 400..499 -> throw PusherValidationError(extractErrorDescription(responseBody))
in 500..599 -> throw PusherServerError(extractErrorDescription(responseBody))
else -> {
return // great
}
}
}

private fun extractErrorDescription(responseBody: String): String =
gson.fromJson(responseBody, PushNotificationErrorResponse::class.java).description
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,18 @@ class PushNotificationsTest {

assertThat(publishId, `is`(notNullValue()))
}

// deleteUser

@Test(expected = IllegalArgumentException::class)
fun `deleteUser should throw an exception if the user id is too long`() {
val longUserId = String(ByteArray(200))
beams.deleteUser(longUserId)
}

@Test
fun `deleteUser should not return errors if the user id is valid`() {
beams.deleteUser("java-user")
// no exceptions --> great
}
}

0 comments on commit c128bee

Please sign in to comment.