Search in sources :

Example 1 with GraphPagination

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

the class MicrosoftContactsExporter method doExport.

@SuppressWarnings("unchecked")
private ExportResult<ContactsModelWrapper> doExport(TokensAndUrlAuthData authData, String url) {
    Request.Builder graphReqBuilder = new Request.Builder().url(url);
    graphReqBuilder.header("Authorization", "Bearer " + authData.getAccessToken());
    try (Response graphResponse = client.newCall(graphReqBuilder.build()).execute()) {
        ResponseBody body = graphResponse.body();
        if (body == null) {
            return new ExportResult<>(new Exception("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<>(e);
    }
}
Also used : GraphPagination(org.datatransferproject.transfer.microsoft.types.GraphPagination) Request(okhttp3.Request) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) ContactsModelWrapper(org.datatransferproject.types.common.models.contacts.ContactsModelWrapper) IOException(java.io.IOException) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody) Response(okhttp3.Response) List(java.util.List) Map(java.util.Map) ExportResult(org.datatransferproject.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.datatransferproject.spi.transfer.provider.ExportResult)1 ContinuationData (org.datatransferproject.spi.transfer.types.ContinuationData)1 GraphPagination (org.datatransferproject.transfer.microsoft.types.GraphPagination)1 ContactsModelWrapper (org.datatransferproject.types.common.models.contacts.ContactsModelWrapper)1