Search in sources :

Example 1 with GraphPagination

use of org.dataportabilityproject.transfer.microsoft.types.GraphPagination in project data-transfer-project by google.

the class MicrosoftContactsExporter method doExport.

@SuppressWarnings("unchecked")
private ExportResult<ContactsModelWrapper> doExport(TokenAuthData authData, String url) {
    Request.Builder graphReqBuilder = new Request.Builder().url(url);
    graphReqBuilder.header("Authorization", "Bearer " + authData.getToken());
    try (Response graphResponse = client.newCall(graphReqBuilder.build()).execute()) {
        ResponseBody body = graphResponse.body();
        if (body == null) {
            return new ExportResult<>(ExportResult.ResultType.ERROR, "Error retrieving contacts: response body was null");
        }
        String graphBody = new String(body.bytes());
        Map graphMap = objectMapper.reader().forType(Map.class).readValue(graphBody);
        String nextLink = (String) graphMap.get(ODATA_NEXT);
        ContinuationData continuationData = nextLink == null ? null : new ContinuationData(new GraphPagination(nextLink));
        List<Map<String, Object>> rawContacts = (List<Map<String, Object>>) graphMap.get("value");
        if (rawContacts == null) {
            return new ExportResult<>(ExportResult.ResultType.END);
        }
        ContactsModelWrapper wrapper = transform(rawContacts);
        return new ExportResult<>(ExportResult.ResultType.CONTINUE, wrapper, continuationData);
    } catch (IOException e) {
        // FIXME log error
        e.printStackTrace();
        return new ExportResult<>(ExportResult.ResultType.ERROR, "Error retrieving contacts: " + e.getMessage());
    }
}
Also used : GraphPagination(org.dataportabilityproject.transfer.microsoft.types.GraphPagination) Request(okhttp3.Request) ContinuationData(org.dataportabilityproject.spi.transfer.types.ContinuationData) ContactsModelWrapper(org.dataportabilityproject.types.transfer.models.contacts.ContactsModelWrapper) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody) Response(okhttp3.Response) List(java.util.List) Map(java.util.Map) ExportResult(org.dataportabilityproject.spi.transfer.provider.ExportResult)

Aggregations

IOException (java.io.IOException)1 List (java.util.List)1 Map (java.util.Map)1 Request (okhttp3.Request)1 Response (okhttp3.Response)1 ResponseBody (okhttp3.ResponseBody)1 ExportResult (org.dataportabilityproject.spi.transfer.provider.ExportResult)1 ContinuationData (org.dataportabilityproject.spi.transfer.types.ContinuationData)1 GraphPagination (org.dataportabilityproject.transfer.microsoft.types.GraphPagination)1 ContactsModelWrapper (org.dataportabilityproject.types.transfer.models.contacts.ContactsModelWrapper)1