Search in sources :

Example 1 with GetPeopleResponse

use of com.google.api.services.people.v1.model.GetPeopleResponse in project data-transfer-project by google.

the class GoogleContactsExporter method exportContacts.

private ExportResult<ContactsModelWrapper> exportContacts(TokensAndUrlAuthData authData, Optional<PaginationData> pageData) {
    try {
        // Set up connection
        Connections.List connectionsListRequest = getOrCreatePeopleService(authData).people().connections().list(SELF_RESOURCE);
        // Get next page, if we have a page token
        if (pageData.isPresent()) {
            StringPaginationToken paginationToken = (StringPaginationToken) pageData.get();
            connectionsListRequest.setPageToken(paginationToken.getToken());
        }
        // Get list of connections (nb: not a list containing full info of each Person)
        ListConnectionsResponse response = connectionsListRequest.setPersonFields(PERSON_FIELDS).execute();
        List<Person> peopleList = response.getConnections();
        // Get list of resource names, then get list of Persons
        List<String> resourceNames = peopleList.stream().map(Person::getResourceName).collect(Collectors.toList());
        GetPeopleResponse batchResponse = getOrCreatePeopleService(authData).people().getBatchGet().setResourceNames(resourceNames).setPersonFields(PERSON_FIELDS).execute();
        List<PersonResponse> personResponseList = batchResponse.getResponses();
        // Convert Persons to VCards
        List<VCard> vCards = personResponseList.stream().map(a -> convert(a.getPerson())).collect(Collectors.toList());
        // Determine if there's a next page
        StringPaginationToken nextPageData = null;
        if (response.getNextPageToken() != null) {
            nextPageData = new StringPaginationToken(response.getNextPageToken());
        }
        ContinuationData continuationData = new ContinuationData(nextPageData);
        ContactsModelWrapper wrapper = new ContactsModelWrapper(makeVCardString(vCards));
        // Get result type
        ResultType resultType = ResultType.CONTINUE;
        if (nextPageData == null) {
            resultType = ResultType.END;
        }
        return new ExportResult<ContactsModelWrapper>(resultType, wrapper, continuationData);
    } catch (IOException e) {
        return new ExportResult<ContactsModelWrapper>(ResultType.ERROR, e.getMessage());
    }
}
Also used : Connections(com.google.api.services.people.v1.PeopleService.People.Connections) EmailAddress(com.google.api.services.people.v1.model.EmailAddress) Connections(com.google.api.services.people.v1.PeopleService.People.Connections) ExportResult(org.dataportabilityproject.spi.transfer.provider.ExportResult) LoggerFactory(org.slf4j.LoggerFactory) PhoneNumber(com.google.api.services.people.v1.model.PhoneNumber) StructuredName(ezvcard.property.StructuredName) TokensAndUrlAuthData(org.dataportabilityproject.types.transfer.auth.TokensAndUrlAuthData) Person(com.google.api.services.people.v1.model.Person) ContinuationData(org.dataportabilityproject.spi.transfer.types.ContinuationData) Credential(com.google.api.client.auth.oauth2.Credential) VCARD_PRIMARY_PREF(org.dataportabilityproject.datatransfer.google.common.GoogleStaticObjects.VCARD_PRIMARY_PREF) LinkedList(java.util.LinkedList) GetPeopleResponse(com.google.api.services.people.v1.model.GetPeopleResponse) PaginationData(org.dataportabilityproject.spi.transfer.types.PaginationData) SELF_RESOURCE(org.dataportabilityproject.datatransfer.google.common.GoogleStaticObjects.SELF_RESOURCE) ListConnectionsResponse(com.google.api.services.people.v1.model.ListConnectionsResponse) VCard(ezvcard.VCard) PeopleService(com.google.api.services.people.v1.PeopleService) ExportInformation(org.dataportabilityproject.spi.transfer.types.ExportInformation) Exporter(org.dataportabilityproject.spi.transfer.provider.Exporter) StringPaginationToken(org.dataportabilityproject.spi.transfer.types.StringPaginationToken) Logger(org.slf4j.Logger) BearerToken(com.google.api.client.auth.oauth2.BearerToken) StringWriter(java.io.StringWriter) Telephone(ezvcard.property.Telephone) ResultType(org.dataportabilityproject.spi.transfer.provider.ExportResult.ResultType) IOException(java.io.IOException) UUID(java.util.UUID) JCardWriter(ezvcard.io.json.JCardWriter) Collectors(java.util.stream.Collectors) SOURCE_PARAM_NAME_TYPE(org.dataportabilityproject.datatransfer.google.common.GoogleStaticObjects.SOURCE_PARAM_NAME_TYPE) List(java.util.List) PERSON_FIELDS(org.dataportabilityproject.datatransfer.google.common.GoogleStaticObjects.PERSON_FIELDS) FieldMetadata(com.google.api.services.people.v1.model.FieldMetadata) PersonResponse(com.google.api.services.people.v1.model.PersonResponse) Email(ezvcard.property.Email) ContactsModelWrapper(org.dataportabilityproject.types.transfer.models.contacts.ContactsModelWrapper) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Name(com.google.api.services.people.v1.model.Name) GoogleStaticObjects(org.dataportabilityproject.datatransfer.google.common.GoogleStaticObjects) ListConnectionsResponse(com.google.api.services.people.v1.model.ListConnectionsResponse) ContinuationData(org.dataportabilityproject.spi.transfer.types.ContinuationData) ContactsModelWrapper(org.dataportabilityproject.types.transfer.models.contacts.ContactsModelWrapper) ResultType(org.dataportabilityproject.spi.transfer.provider.ExportResult.ResultType) IOException(java.io.IOException) GetPeopleResponse(com.google.api.services.people.v1.model.GetPeopleResponse) Person(com.google.api.services.people.v1.model.Person) VCard(ezvcard.VCard) StringPaginationToken(org.dataportabilityproject.spi.transfer.types.StringPaginationToken) PersonResponse(com.google.api.services.people.v1.model.PersonResponse) ExportResult(org.dataportabilityproject.spi.transfer.provider.ExportResult)

Example 2 with GetPeopleResponse

use of com.google.api.services.people.v1.model.GetPeopleResponse in project data-transfer-project by google.

the class GoogleContactsServiceTest method setUpSinglePersonResponse.

private void setUpSinglePersonResponse() throws IOException {
    connectionsList = Collections.singletonList(PERSON);
    listConnectionsResponse = new ListConnectionsResponse();
    listConnectionsResponse.setConnections(connectionsList);
    PersonResponse personResponse = new PersonResponse().setPerson(PERSON);
    GetPeopleResponse batchResponse = new GetPeopleResponse().setResponses(Collections.singletonList(personResponse));
    // This can't go in setup()
    when(listConnectionsRequest.setPersonFields(PERSON_FIELDS)).thenReturn(listConnectionsRequest);
    when(listConnectionsRequest.execute()).thenReturn(listConnectionsResponse);
    // This is specific to returning a single Person
    when(connections.list(SELF_RESOURCE)).thenReturn(listConnectionsRequest);
    when(getBatchGet.setResourceNames(Collections.singletonList(RESOURCE_NAME))).thenReturn(getBatchGet);
    when(getBatchGet.execute()).thenReturn(batchResponse);
}
Also used : GetPeopleResponse(com.google.api.services.people.v1.model.GetPeopleResponse) ListConnectionsResponse(com.google.api.services.people.v1.model.ListConnectionsResponse) PersonResponse(com.google.api.services.people.v1.model.PersonResponse)

Example 3 with GetPeopleResponse

use of com.google.api.services.people.v1.model.GetPeopleResponse in project data-transfer-project by google.

the class GoogleContactsService method export.

public ContactsModelWrapper export(ExportInformation continuationInformation) throws IOException {
    // Set up connection
    Connections.List connectionsList = peopleService.people().connections().list(GoogleContactsConstants.SELF_RESOURCE);
    // Get next page, if we have a page token
    if (continuationInformation.getPaginationInformation().isPresent()) {
        String pageToken = ((StringPaginationToken) continuationInformation.getPaginationInformation().get()).getId();
        connectionsList.setPageToken(pageToken);
    }
    // Get list of connections (nb: not a list containing full info of each Person)
    ListConnectionsResponse response = connectionsList.setPersonFields(GoogleContactsConstants.PERSON_FIELDS).execute();
    List<Person> peopleList = response.getConnections();
    // Get list of resource names, then get list of Persons
    List<String> resourceNames = peopleList.stream().map(Person::getResourceName).collect(Collectors.toList());
    GetPeopleResponse batchResponse = peopleService.people().getBatchGet().setResourceNames(resourceNames).setPersonFields(GoogleContactsConstants.PERSON_FIELDS).execute();
    List<PersonResponse> personResponseList = batchResponse.getResponses();
    // Convert Persons to VCards
    List<VCard> vCards = personResponseList.stream().map(a -> GoogleContactToVCardConverter.convert(a.getPerson())).collect(Collectors.toList());
    // Determine if there's a next page
    StringPaginationToken newPage = null;
    if (response.getNextPageToken() != null) {
        newPage = new StringPaginationToken(response.getNextPageToken());
    }
    ContinuationInformation newContinuationInformation = null;
    if (newPage != null) {
        newContinuationInformation = new ContinuationInformation(null, newPage);
    }
    return new ContactsModelWrapper(vCards, newContinuationInformation);
}
Also used : Connections(com.google.api.services.people.v1.PeopleService.People.Connections) VCard(ezvcard.VCard) PeopleService(com.google.api.services.people.v1.PeopleService) Importer(org.dataportabilityproject.dataModels.Importer) Logger(org.slf4j.Logger) Connections(com.google.api.services.people.v1.PeopleService.People.Connections) ExportInformation(org.dataportabilityproject.dataModels.ExportInformation) Collection(java.util.Collection) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Exporter(org.dataportabilityproject.dataModels.Exporter) ContactsModelWrapper(org.dataportabilityproject.dataModels.contacts.ContactsModelWrapper) Person(com.google.api.services.people.v1.model.Person) List(java.util.List) PersonResponse(com.google.api.services.people.v1.model.PersonResponse) JobDataCache(org.dataportabilityproject.cloud.interfaces.JobDataCache) ContinuationInformation(org.dataportabilityproject.dataModels.ContinuationInformation) Credential(com.google.api.client.auth.oauth2.Credential) VisibleForTesting(com.google.common.annotations.VisibleForTesting) GoogleStaticObjects(org.dataportabilityproject.serviceProviders.google.GoogleStaticObjects) GetPeopleResponse(com.google.api.services.people.v1.model.GetPeopleResponse) ListConnectionsResponse(com.google.api.services.people.v1.model.ListConnectionsResponse) StringPaginationToken(org.dataportabilityproject.shared.StringPaginationToken) ListConnectionsResponse(com.google.api.services.people.v1.model.ListConnectionsResponse) ContactsModelWrapper(org.dataportabilityproject.dataModels.contacts.ContactsModelWrapper) GetPeopleResponse(com.google.api.services.people.v1.model.GetPeopleResponse) ContinuationInformation(org.dataportabilityproject.dataModels.ContinuationInformation) Person(com.google.api.services.people.v1.model.Person) VCard(ezvcard.VCard) StringPaginationToken(org.dataportabilityproject.shared.StringPaginationToken) PersonResponse(com.google.api.services.people.v1.model.PersonResponse)

Example 4 with GetPeopleResponse

use of com.google.api.services.people.v1.model.GetPeopleResponse in project data-transfer-project by google.

the class GoogleContactsExporterTest method setUpSinglePersonResponse.

private void setUpSinglePersonResponse() throws IOException {
    connectionsList = Collections.singletonList(PERSON);
    listConnectionsResponse = new ListConnectionsResponse();
    listConnectionsResponse.setConnections(connectionsList);
    PersonResponse personResponse = new PersonResponse().setPerson(PERSON);
    GetPeopleResponse batchResponse = new GetPeopleResponse().setResponses(Collections.singletonList(personResponse));
    // This can't go in setup()
    when(listConnectionsRequest.setPersonFields(PERSON_FIELDS)).thenReturn(listConnectionsRequest);
    when(listConnectionsRequest.execute()).thenReturn(listConnectionsResponse);
    // This is specific to returning a single Person
    when(connections.list(SELF_RESOURCE)).thenReturn(listConnectionsRequest);
    when(getBatchGet.setResourceNames(Collections.singletonList(RESOURCE_NAME))).thenReturn(getBatchGet);
    when(getBatchGet.execute()).thenReturn(batchResponse);
}
Also used : GetPeopleResponse(com.google.api.services.people.v1.model.GetPeopleResponse) ListConnectionsResponse(com.google.api.services.people.v1.model.ListConnectionsResponse) PersonResponse(com.google.api.services.people.v1.model.PersonResponse)

Aggregations

GetPeopleResponse (com.google.api.services.people.v1.model.GetPeopleResponse)4 ListConnectionsResponse (com.google.api.services.people.v1.model.ListConnectionsResponse)4 PersonResponse (com.google.api.services.people.v1.model.PersonResponse)4 Credential (com.google.api.client.auth.oauth2.Credential)2 PeopleService (com.google.api.services.people.v1.PeopleService)2 Connections (com.google.api.services.people.v1.PeopleService.People.Connections)2 Person (com.google.api.services.people.v1.model.Person)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 VCard (ezvcard.VCard)2 IOException (java.io.IOException)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 BearerToken (com.google.api.client.auth.oauth2.BearerToken)1 EmailAddress (com.google.api.services.people.v1.model.EmailAddress)1 FieldMetadata (com.google.api.services.people.v1.model.FieldMetadata)1 Name (com.google.api.services.people.v1.model.Name)1 PhoneNumber (com.google.api.services.people.v1.model.PhoneNumber)1 Preconditions (com.google.common.base.Preconditions)1 JCardWriter (ezvcard.io.json.JCardWriter)1 Email (ezvcard.property.Email)1