GetDutchie / brick

An intuitive way to work with persistent data in Dart

Home Page:https://getdutchie.github.io/brick/#/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Changing the defaultHeaders cancels pending changes

hortigado opened this issue · comments

Hello, when I close the user's session because their cookie expired, the pending uploads are canceled when the session is opened again. I place the cookie in the restprovider, in defaultHeaders. How can I prevent pending changes from being cancelled? Greetings

image

I started checking the package and it seems that it saves the header in each transaction. Now I am creating a function to change the header of all pending transactions. If there is already a function that does it, help me by letting me know. Thank you

Future<bool> unprocessedRequestsHeaderSet({
    required Map<String, dynamic> header,
  }) async {
    final db = await getDb();
    final List<Map<String, dynamic>> send = await unprocessedRequests();
    final List<Map<String, dynamic>> docs = [];
    for (var element in send.toList()) {
      final Map<String, dynamic> document = Map.of(element);
      document['headers'] = formatMap(header);
      docs.add(document);
    }
    try {
      for (var data in docs) {
        await RequestSqliteCache.updateRequest(
          db: db,
          data: data,
          lockedColumn: lockedColumn,
          primaryKeyColumn: primaryKeyColumn,
          tableName: tableName,
        );
      }
      return true;
    } catch (e) {
      return false;
    }
  }`
String formatMap(Map<String, dynamic> map) {
  final StringBuffer buffer = StringBuffer('{');
  final keys = map.keys.toList();
  for (int i = 0; i < keys.length; i++) {
    final key = keys[i];
    final value = map[key];
    buffer.write('"$key": "$value"');
    if (i != keys.length - 1) {
      buffer.write(', ');
    }
  }
  buffer.write('}');
  return buffer.toString();
}

request_sqlite_cache_manager.dart

static Future<int> updateRequest({
    required DatabaseExecutor db,
    required Map<String, dynamic> data,
    required String lockedColumn,
    required String primaryKeyColumn,
    required String tableName,
  }) async =>
      await _updateRequest(false, data, db, tableName, lockedColumn, primaryKeyColumn);
  static Future<int> _updateRequest(
    bool shouldLock,
    Map<String, dynamic> data,
    DatabaseExecutor db,
    String tableName,
    String lockedColumn,
    String primaryKeyColumn,
  ) async {
    return await db.update(
      tableName,
      data,
      where: '$primaryKeyColumn = ?',
      whereArgs: [data[primaryKeyColumn]],
    );
  }

request_sqlite_cache.dart

I create it function for solved it problem.

Also, if the endpoint is modified, it also resolves it.

Hey @hortigado so this is a curious problem. One we encountered in GraphQL but didn't entirely backport to REST. GraphQL only applies headers from the provider invoking the request. However, REST applies them which is likely where you're seeing that override occur from your default headers.

Instead of manipulating your headers on the request, I would recommend subclassing the ReatRequestSqliteCache, overriding just one method and removing the line where cached headers are reapplied.

For formatMap, I also recommend using Dart's built in json encoder.

That said, if it works it works. Subclassing has its own pitfalls of drifting from upstream.

@tshedor Hi thanks for the suggestions, if I have free time I will work on it.
It is true my solution is something simple but it was the easiest thing to do to quickly solve the problem. As for the formatMap, I tried with json encoder but for some reason it did not show me the correct format, I would have to check it again.
For now I consider the problem resolved but it would be a feature to continue working on because it is necessary.