pull method

Future<OneDriveResponse> pull(
  1. String remotePath, {
  2. bool isAppFolder = false,
})

Implementation

Future<OneDriveResponse> pull(String remotePath, {bool isAppFolder = false}) async {
  final accessToken = await _tokenManager.getAccessToken();
  if (accessToken == null) {
    return OneDriveResponse(message: "Null access token", bodyBytes: Uint8List(0));
  }

  /// We need to call this method to create app folder and make sure it exists.
  /// Otherwise, we will get "Access Denied - 403".
  /// https://learn.microsoft.com/en-us/onedrive/developer/rest-api/concepts/special-folders-appfolder?view=odsp-graph-online
  if (isAppFolder) {
    await getMetadata(remotePath, isAppFolder: isAppFolder);
  }

  final url = Uri.parse("${apiEndpoint}me/drive/${_getRootFolder(isAppFolder)}:$remotePath:/content");

  try {
    final resp = await http.get(
      url,
      headers: {"Authorization": "Bearer $accessToken"},
    );

    debugPrint("# OneDrive -> pull: ${resp.statusCode}\n# Body: ${resp.body}");

    if (resp.statusCode == 200 || resp.statusCode == 201) {
      return OneDriveResponse(
          statusCode: resp.statusCode,
          body: resp.body,
          message: "Download successfully.",
          bodyBytes: resp.bodyBytes,
          isSuccess: true);
    } else if (resp.statusCode == 404) {
      return OneDriveResponse(
          statusCode: resp.statusCode,
          body: resp.body,
          message: "File not found.",
          bodyBytes: Uint8List(0));
    } else {
      return OneDriveResponse(
          statusCode: resp.statusCode,
          body: resp.body,
          message: "Error while downloading file.",
          bodyBytes: Uint8List(0));
    }
  } catch (err) {
    debugPrint("# OneDrive -> pull: $err");
    return OneDriveResponse(message: "Unexpected exception: $err");
  }
}