-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor code and set message header to send to expensify.
- Loading branch information
1 parent
25aafee
commit 430d991
Showing
8 changed files
with
226 additions
and
164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import com.google.api.client.auth.oauth2.Credential; | ||
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp; | ||
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver; | ||
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; | ||
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets; | ||
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; | ||
import com.google.api.client.http.HttpTransport; | ||
import com.google.api.client.json.JsonFactory; | ||
import com.google.api.client.json.jackson2.JacksonFactory; | ||
import com.google.api.client.util.store.FileDataStoreFactory; | ||
import com.google.api.services.gmail.GmailScopes; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.security.GeneralSecurityException; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
class GmailServiceConfigurer { | ||
private String applicationName; | ||
private HttpTransport httpTransport; | ||
private JsonFactory jsonFactory; | ||
private List<String> scopes; | ||
private FileDataStoreFactory dataStoreFactory; | ||
|
||
GmailServiceConfigurer() { | ||
} | ||
|
||
GmailServiceConfigurer(String applicationName, HttpTransport httpTransport, JsonFactory jsonFactory, List<String> scopes, FileDataStoreFactory dataStoreFactory) { | ||
this.applicationName = applicationName; | ||
this.httpTransport = httpTransport; | ||
this.jsonFactory = jsonFactory; | ||
this.scopes = scopes; | ||
this.dataStoreFactory = dataStoreFactory; | ||
} | ||
|
||
GmailServiceConfigurer loadDefaultConfiguration() { | ||
final java.io.File DATA_STORE_DIR = new java.io.File( | ||
System.getProperty("user.home"), ".credentials/gmail-java-quickstart"); | ||
this.applicationName = "Gmail API Java GrabReceiptsForwarder"; | ||
try { | ||
this.httpTransport = GoogleNetHttpTransport.newTrustedTransport(); | ||
this.dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR); | ||
} catch (GeneralSecurityException | IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
this.jsonFactory = JacksonFactory.getDefaultInstance(); | ||
this.scopes = Collections.singletonList(GmailScopes.GMAIL_MODIFY); | ||
|
||
return this; | ||
} | ||
|
||
String getApplicationName() { | ||
return applicationName; | ||
} | ||
|
||
HttpTransport getHttpTransport() { | ||
return httpTransport; | ||
} | ||
|
||
JsonFactory getJsonFactory() { | ||
return jsonFactory; | ||
} | ||
|
||
Credential authorize() throws IOException { | ||
GoogleClientSecrets clientSecrets = getClientSecrets(); | ||
|
||
GoogleAuthorizationCodeFlow flow = buildAuthorizationCodeFlow(clientSecrets); | ||
|
||
return new AuthorizationCodeInstalledApp( | ||
flow, new LocalServerReceiver()).authorize("user"); | ||
} | ||
|
||
private GoogleAuthorizationCodeFlow buildAuthorizationCodeFlow(GoogleClientSecrets clientSecrets) throws IOException { | ||
return new GoogleAuthorizationCodeFlow.Builder( | ||
httpTransport, jsonFactory, clientSecrets, scopes) | ||
.setDataStoreFactory(dataStoreFactory) | ||
.setAccessType("offline") | ||
.build(); | ||
} | ||
|
||
private GoogleClientSecrets getClientSecrets() throws IOException { | ||
InputStream in = | ||
GrabReceiptsForwarder.class.getResourceAsStream("/client_secret.json"); | ||
return GoogleClientSecrets.load(jsonFactory, new InputStreamReader(in)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import com.google.api.client.auth.oauth2.Credential; | ||
import com.google.api.services.gmail.Gmail; | ||
|
||
import java.io.IOException; | ||
|
||
class GmailServiceProvider { | ||
private GmailServiceConfigurer configurer; | ||
|
||
GmailServiceProvider(GmailServiceConfigurer configurer) { | ||
this.configurer = configurer; | ||
} | ||
|
||
Gmail getGmailService() { | ||
Credential credential = null; | ||
try { | ||
credential = configurer.authorize(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
return new Gmail.Builder(configurer.getHttpTransport(), configurer.getJsonFactory(), credential) | ||
.setApplicationName(configurer.getApplicationName()) | ||
.build(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import com.google.api.services.gmail.Gmail; | ||
import com.google.api.services.gmail.model.ListMessagesResponse; | ||
|
||
import javax.mail.MessagingException; | ||
import java.io.IOException; | ||
import java.security.GeneralSecurityException; | ||
|
||
public class GrabReceiptsForwarder { | ||
|
||
public static void main(String[] args) throws IOException, MessagingException, GeneralSecurityException { | ||
Gmail service = new GmailServiceProvider(new GmailServiceConfigurer().loadDefaultConfiguration()).getGmailService(); | ||
String user = "me"; | ||
|
||
MessageHandler messageHandler = new MessageHandler(service, user); | ||
ListMessagesResponse response = messageHandler.getListMessageResponseBySearchQuery(new DateHandler().formatSearchQuery()); | ||
messageHandler.sendListMessages(response, "[email protected]"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import com.google.api.client.util.Base64; | ||
import com.google.api.services.gmail.Gmail; | ||
import com.google.api.services.gmail.model.ListMessagesResponse; | ||
import com.google.api.services.gmail.model.Message; | ||
|
||
import javax.mail.MessagingException; | ||
import javax.mail.Session; | ||
import javax.mail.internet.MimeMessage; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.Properties; | ||
|
||
class MessageHandler { | ||
private final Gmail service; | ||
private final String user; | ||
|
||
MessageHandler(Gmail service, String user) { | ||
this.service = service; | ||
this.user = user; | ||
} | ||
|
||
ListMessagesResponse getListMessageResponseBySearchQuery(String query) throws IOException { | ||
return service.users() | ||
.messages().list(user) | ||
.setQ(query) | ||
.execute(); | ||
} | ||
|
||
void sendListMessages(ListMessagesResponse messagesResponse, String to) throws IOException, MessagingException { | ||
List<Message> messages = messagesResponse.getMessages(); | ||
for (Message message: messages) { | ||
message = service.users().messages().get(user, message.getId()).execute(); | ||
MimeMessage mimeMessage = getMimeMessage(message.getId()); | ||
mimeMessage.setHeader("To", to); | ||
sendMessage(mimeMessage); | ||
} | ||
} | ||
|
||
|
||
private MimeMessage getMimeMessage(String messageId) | ||
throws IOException, MessagingException { | ||
Message message = service.users().messages().get(user, messageId).setFormat("raw").execute(); | ||
|
||
byte[] emailBytes = Base64.decodeBase64(message.getRaw()); | ||
|
||
Properties props = new Properties(); | ||
Session session = Session.getDefaultInstance(props, null); | ||
|
||
return new MimeMessage(session, new ByteArrayInputStream(emailBytes)); | ||
} | ||
|
||
private void sendMessage(MimeMessage email) | ||
throws MessagingException, IOException { | ||
Message message = createMessageWithEmail(email); | ||
message = service.users().messages().send(user, message).execute(); | ||
|
||
System.out.println("Message id: " + message.getId()); | ||
System.out.println(message.toPrettyString()); | ||
} | ||
|
||
private static Message createMessageWithEmail(MimeMessage email) | ||
throws MessagingException, IOException { | ||
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
email.writeTo(baos); | ||
String encodedEmail = Base64.encodeBase64URLSafeString(baos.toByteArray()); | ||
Message message = new Message(); | ||
message.setRaw(encodedEmail); | ||
return message; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,17 +7,6 @@ | |
import static org.junit.Assert.assertEquals; | ||
|
||
public class DateHandlerTest { | ||
@Test | ||
public void testFindThisMonday() throws Exception { | ||
LocalDateTime now = LocalDateTime.now(); | ||
assertEquals(LocalDate.of(2017, 5, 29).getDayOfYear(), now.minusDays(now.getDayOfWeek().getValue() - 1).getDayOfYear()); | ||
} | ||
|
||
@Test | ||
public void testDateTimeFormat() throws Exception { | ||
assertEquals("2017-05-31", LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE)); | ||
} | ||
|
||
@Test | ||
public void testFormatSearchQuery() throws Exception { | ||
assertEquals("from:([email protected]) business receipt after:2017-05-28 before:2017-06-03", new DateHandler().formatSearchQuery()); | ||
|
Oops, something went wrong.