-
Notifications
You must be signed in to change notification settings - Fork 0
v2 endpoint for admin related metadata #83
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.hello.suripu.admin.models; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.hello.suripu.core.models.Account; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public class AccountAdminView { | ||
|
||
@JsonProperty("account") | ||
public final Account account; | ||
|
||
private AccountAdminView(final Account account) { | ||
this.account = account; | ||
} | ||
|
||
@JsonProperty("disabled") | ||
public final Boolean disabled() { | ||
return account.email.startsWith("ADMIN-DISABLED"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you make it a static constant |
||
} | ||
|
||
public static AccountAdminView fromAccount(@NotNull final Account account) { | ||
return new AccountAdminView(account); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.hello.suripu.admin.resources.v1; | ||
|
||
import com.codahale.metrics.annotation.Timed; | ||
import com.google.common.base.Optional; | ||
import com.hello.suripu.core.db.AccountDAO; | ||
import com.hello.suripu.core.models.Account; | ||
import com.hello.suripu.core.notifications.HelloPushMessage; | ||
import com.hello.suripu.core.notifications.MobilePushNotificationProcessor; | ||
import com.hello.suripu.core.oauth.AccessToken; | ||
import com.hello.suripu.core.oauth.OAuthScope; | ||
import com.hello.suripu.coredw8.oauth.Auth; | ||
import com.hello.suripu.coredw8.oauth.ScopesAllowed; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.validation.Valid; | ||
import javax.ws.rs.Consumes; | ||
import javax.ws.rs.POST; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.PathParam; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.Response; | ||
|
||
public class NotificationResources { | ||
|
||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(NotificationResources.class); | ||
|
||
private final AccountDAO accountDAO; | ||
private final MobilePushNotificationProcessor pushNotificationProcessor; | ||
|
||
public NotificationResources(final AccountDAO accountDAO, MobilePushNotificationProcessor pushNotificationProcessor) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing final on 2nd arg |
||
this.accountDAO = accountDAO; | ||
this.pushNotificationProcessor = pushNotificationProcessor; | ||
} | ||
|
||
|
||
@ScopesAllowed({OAuthScope.ADMINISTRATION_READ}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you meant ADMINSTRATION_WRITE |
||
@POST | ||
@Path("/send/{email}") | ||
@Timed | ||
@Consumes(MediaType.APPLICATION_JSON) | ||
public Response send( | ||
@Auth final AccessToken accessToken, | ||
@PathParam("email") final String email, | ||
@Valid final HelloPushMessage message) { | ||
|
||
LOGGER.debug("email: {}", email); | ||
LOGGER.debug("push target: {}", message.target); | ||
LOGGER.debug("push details: {}", message.details); | ||
LOGGER.debug("push body: {}", message.body); | ||
|
||
final Optional<Account> accountOptional = accountDAO.getByEmail(email); | ||
if(!accountOptional.isPresent()) { | ||
return Response.status(Response.Status.NOT_FOUND).build(); | ||
} | ||
|
||
pushNotificationProcessor.push(accountOptional.get().id.get(), message); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any exception to be caught here? |
||
return Response.ok().build(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.hello.suripu.admin.resources.v2; | ||
|
||
|
||
import com.google.common.base.Optional; | ||
import com.hello.suripu.admin.models.AccountAdminView; | ||
import com.hello.suripu.core.db.AccountDAO; | ||
import com.hello.suripu.core.models.Account; | ||
import com.hello.suripu.core.oauth.OAuthScope; | ||
import com.hello.suripu.coredw8.oauth.AccessToken; | ||
import com.hello.suripu.coredw8.oauth.Auth; | ||
import com.hello.suripu.coredw8.oauth.ScopesAllowed; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.QueryParam; | ||
import javax.ws.rs.WebApplicationException; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.Response; | ||
|
||
@Path("/v2/account") | ||
public class AccountAdminResources { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(AccountAdminResources.class); | ||
|
||
public final AccountDAO accountDAO; | ||
|
||
public AccountAdminResources(final AccountDAO accountDAO) { | ||
this.accountDAO = accountDAO; | ||
} | ||
|
||
@ScopesAllowed({OAuthScope.ADMINISTRATION_READ}) | ||
@GET | ||
@Produces(MediaType.APPLICATION_JSON) | ||
public AccountAdminView retrieveAccountByEmailOrId(@Auth final AccessToken accessToken, | ||
@QueryParam("email") final String email, | ||
@QueryParam("id") final Long id) { | ||
if (email == null && id == null) { | ||
throw new WebApplicationException(Response.status(Response.Status.NOT_FOUND) | ||
.entity("Missing query params, please specify email or id").build()); | ||
} | ||
|
||
final Optional<Account> account = (email !=null) | ||
? accountDAO.getByEmail(email.toLowerCase()) | ||
: accountDAO.getById(id); | ||
|
||
if (!account.isPresent()) { | ||
LOGGER.warn("email={} id={} result=missing", email, id); | ||
throw new WebApplicationException(Response.status(Response.Status.NOT_FOUND).entity("Account not found").build()); | ||
} | ||
|
||
return AccountAdminView.fromAccount(account.get()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit pick : do you think isDisabled a better name?