// // Copyright 2014-2015 Amazon.com, // Inc. or its affiliates. All Rights Reserved. // // SPDX-License-Identifier: Apache-2.0 // using System; using System.IO; using Amazon.Runtime; using Amazon.CognitoIdentity; using Amazon.CognitoSync.Model; namespace Amazon.CognitoSync.SyncManager.Internal { /// /// Remote data storage using Cognito Sync service on which we can invoke /// actions like creating a dataset or record. /// public partial class CognitoSyncStorage : IDisposable { private readonly string identityPoolId; #if UNITY private readonly AmazonCognitoSyncClient client; #else private readonly IAmazonCognitoSync client; #endif private readonly CognitoAWSCredentials cognitoCredentials; #region Dispose /// /// Dispose this Object /// public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } /// /// Dispose this Object /// /// protected virtual void Dispose(bool disposing) { if (disposing) { if (client != null) client.Dispose(); } } #endregion #region Constructor /// /// Creates an insance of CognitoSyncStorage. /// /// /// public CognitoSyncStorage(CognitoAWSCredentials cognitoCredentials, AmazonCognitoSyncConfig config) { if (cognitoCredentials == null) { throw new ArgumentNullException("cognitoCredentials"); } this.identityPoolId = cognitoCredentials.IdentityPoolId; this.cognitoCredentials = cognitoCredentials; this.client = new AmazonCognitoSyncClient(cognitoCredentials, config); } #endregion #region Private Methods private string GetCurrentIdentityId() { return cognitoCredentials.GetIdentityId(); } private static RecordPatch RecordToPatch(Record record) { RecordPatch patch = new RecordPatch(); patch.DeviceLastModifiedDate = record.DeviceLastModifiedDate.Value; patch.Key = record.Key; patch.Value = record.Value; patch.SyncCount = record.SyncCount; patch.Op = (record.Value == null ? Operation.Remove : Operation.Replace); return patch; } private static DatasetMetadata ModelToDatasetMetadata(Amazon.CognitoSync.Model.Dataset model) { return new DatasetMetadata( model.DatasetName, model.CreationDate, model.LastModifiedDate, model.LastModifiedBy, model.DataStorage, model.NumRecords ); } private static Record ModelToRecord(Amazon.CognitoSync.Model.Record model) { return new Record( model.Key, model.Value, model.SyncCount, model.LastModifiedDate, model.LastModifiedBy, model.DeviceLastModifiedDate, false); } private static SyncManagerException HandleException(Exception e, string message) { var ase = e as AmazonServiceException; if (ase == null) ase = new AmazonServiceException(e); if (ase.GetType() == typeof(ResourceNotFoundException)) { return new DatasetNotFoundException(message); } else if (ase.GetType() == typeof(ResourceConflictException) || ase.StatusCode == System.Net.HttpStatusCode.Conflict) { return new DataConflictException(message); } else if (ase.GetType() == typeof(LimitExceededException)) { return new DataLimitExceededException(message); } else if (IsNetworkException(ase)) { return new NetworkException(message); } else if (ase.GetType() == typeof(AmazonCognitoSyncException) && ase.Message != null && ase.Message.StartsWith("Current SyncCount for:", StringComparison.Ordinal)) { return new DataConflictException(message); } else { return new DataStorageException(message, ase); } } private static bool IsNetworkException(AmazonServiceException ase) { return ase.InnerException != null && ase.InnerException.GetType() == typeof(IOException); } #endregion } }