get method

  1. @override
Future<AtContact?> get(
  1. String atSign, {
  2. AtKey? getAtKey,
})

returns the AtContact.has to pass the 'atSign' Throws FormatException on invalid json Throws class extending AtException on invalid atsign.

Implementation

@override
Future<AtContact?> get(String atSign, {AtKey? getAtKey}) async {
  AtContact? contact;
  var atKey = getAtKey ?? _formKey(KeyType.contact, key: atSign, isGet: true);
  if (_regexType == RegexType.all) {
    List<AtKey>? scanList;
    try {
      scanList = await atClient!.getAtKeys(regex: atKey.key);
    } on KeyNotFoundException {
      logger.info('${atKey.key} on not found in the keystore');
    } on AtClientException {
      logger.info('${atKey.key} on not found in the keystore');
    }
    atKey = (scanList != null && scanList.isNotEmpty)
        ? _formAtKeyFromScanKeys(scanList[0])
        : atKey;
  }
  AtValue? atValue;
  atValue = await _get(atKey);

  //check for old key if new key data is not present.
  if (atValue?.value == null) {
    atKey = _formKey(KeyType.contact, key: atSign, isOld: true);
    atValue = await _get(atKey);
  }
  //migrate key to new keyformat if atKey is old.
  if (atValue?.value != null && _isOldKey(atKey)) {
    var newAtKey = _formKey(KeyType.contact, key: atSign);
    await atClient!.put(newAtKey, atValue?.value);
    AtValue? getValue;
    getValue = await _get(newAtKey);
    if (getValue?.value != null) await atClient!.delete(atKey);
  }
  if (atValue?.value != null) {
    var value = atValue?.value;
    value = value?.replaceAll('data:', '');
    if (value != null && value != 'null') {
      var json;
      try {
        json = jsonDecode(value);
      } on FormatException catch (e) {
        logger.severe('Invalid JSON. ${e.message} found in JSON : $value');
        throw InvalidSyntaxException('Invalid JSON found');
      }
      if (json != null) {
        contact = AtContact.fromJson(json);
      }
    }
  }
  return contact;
}