updateUserProfilePicture method

  1. @override
Future<Either<Failure, String?>> updateUserProfilePicture(
  1. AuthenticationData authData,
  2. String imagePath
)
override

Implementation

@override
Future<Either<Failure, String?>> updateUserProfilePicture(
    AuthenticationData authData, String imagePath) async {
  return wrapAndHandleHttpBaseRequest<String?>(
    () async {
      if (config.updateUserProfilePictureApiEndpoint == null) {
        throw Exception(
            "'updateUserProfilePictureApiEndpoint' property is not defined in provided AccountBasicConfig");
      }

      final url = config.updateUserProfilePictureApiEndpoint!(authData);

      logger.d("UpdateUserProfilePicture code at: $url ");

      final multiPartRequest = http.MultipartRequest(
        config.updateUserProfilePictureApiMethod ?? 'PATCH',
        url,
      );

      // if the user has defined a mapper most likely they will handle the
      // file addition, so no need to attach it on our own
      if (config.updateUserProfilePictureCustomRequestMapper != null) {
        return config.updateUserProfilePictureCustomRequestMapper!.call(
          imagePath,
          multiPartRequest,
          authData,
        );
      }

      final file = await http.MultipartFile.fromPath(
        'profile_picture',
        imagePath,
      );

      multiPartRequest.files.add(file);

      return multiPartRequest;
    },
    onResponse: (response, left, right) {
      if (config.updateUserProfilePictureCustomResponseParser != null) {
        return right(
          config.updateUserProfilePictureCustomResponseParser!.call(response),
        );
      }
      return right(null);
    },
  );
}