Skip to content

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<dropwizard.version>0.8.2</dropwizard.version>
<metrics.version>3.1.0</metrics.version>
<java.version>1.7</java.version>
<suripu.version>0.6.2</suripu.version>
<suripu.version>0.6.12</suripu.version>
<jodatime.version>20150319</jodatime.version>
</properties>

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/hello/suripu/admin/SuripuAdmin.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import com.hello.suripu.admin.resources.v1.TimelineResources;
import com.hello.suripu.admin.resources.v1.TokenResources;
import com.hello.suripu.admin.resources.v1.WifiResources;
import com.hello.suripu.admin.resources.v2.AccountAdminResources;
import com.hello.suripu.core.configuration.DynamoDBTableName;
import com.hello.suripu.core.configuration.QueueName;
import com.hello.suripu.core.db.AccountDAO;
Expand Down Expand Up @@ -457,6 +458,7 @@ public void run(SuripuAdminConfiguration configuration, Environment environment)
environment.jersey().register(new WifiResources(wifiInfoDAO));
environment.jersey().register(new KeyStoreResources(senseKeyStore, pillKeyStore));
environment.jersey().register(new DBResource(sensorsTableDAO));
environment.jersey().register(new AccountAdminResources(accountDAO));
environment.jersey().register(new FeedbackResources(feedbackDAO, accountDAO));

}
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/com/hello/suripu/admin/models/AccountAdminView.java
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() {
Copy link
Contributor

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?

return account.email.startsWith("ADMIN-DISABLED");
Copy link
Contributor

Choose a reason for hiding this comment

The 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) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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})
Copy link
Contributor

Choose a reason for hiding this comment

The 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);
Copy link
Contributor

Choose a reason for hiding this comment

The 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());
}
}