Beispiel #1
0
        /// <summary>
        /// Select a controller. The <paramref name="request"/> parameter is guaranteed not to be <c>null</c>.
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        protected virtual HttpControllerDescriptor OnSelectController(HttpRequestMessage request)
        {
            ControllerIdentification controllerName = this.GetControllerIdentificationFromRequest(request);

            if (String.IsNullOrEmpty(controllerName.Name))
            {
                throw new HttpResponseException(request.CreateResponse(HttpStatusCode.NotFound));
            }

            HttpControllerDescriptor controllerDescriptor;

            if (this._controllerInfoCache.Value.TryGetValue(controllerName, out controllerDescriptor))
            {
                return(controllerDescriptor);
            }

            ICollection <Type> matchingTypes = this._controllerTypeCache.GetControllerTypes(controllerName);

            // ControllerInfoCache is already initialized.
            Contract.Assert(matchingTypes.Count != 1);

            if (matchingTypes.Count == 0)
            {
                // no matching types
                throw ApiControllerNotFoundException.Create(controllerName);
            }

            // multiple matching types
            throw AmbigiousApiRequestException.Create(controllerName, request.GetRouteData().Route, matchingTypes);
        }
Beispiel #2
0
        private async Task <PostResponse> Synchronize(SQLiteConnection connTran, object synchronizableModel)
        {
            SynchronizableModelBase syncModel = synchronizableModel as SynchronizableModelBase;

            if (syncModel == null)
            {
                throw new NullReferenceException("Unable to cast passed model into a syncable model.");
            }

            ApiBase apiBaseInstance = new SyncRegistry().GetApiBaseInstance(syncModel.TableName);

            if (apiBaseInstance == null)
            {
                Exception ex = new ApiControllerNotFoundException(syncModel.TableName);
                this.Logger.Error(ex);
                throw ex;
            }

            SyncRecord syncRec = await this.GetSyncRecordAsync(syncModel.RequestId);

            if (syncModel.DontSync)
            {
                if (syncRec == null)
                {
                    syncRec = new SyncRecord
                    {
                        ModelId   = syncModel.Id,
                        ModelType = syncModel.TableName,
                        RequestId = syncModel.RequestId,
                        Status    = syncModel.Channel == DataChannel.Fallback ? RecordStatus.FallbackSent : RecordStatus.Synced
                    };

                    this.Logger.Debug("Sync Status " + syncRec.Status);
                    await this.SaveAsync(connTran, syncRec);
                }

                return(new PostResponse
                {
                    RequestId = syncModel.RequestId,
                    Successful = true,
                    ResponseText = string.Empty
                });
            }

            PostResponse postResponse = await this.SyncUpAsync(synchronizableModel, apiBaseInstance);

            if (postResponse != null && postResponse.Successful)
            {
                this.Logger.Debug("Synced successfully");
                return(postResponse);
            }

            if (postResponse != null)
            {
                this.Logger.Debug("Synced successfully but the server response had an error");
                postResponse = await apiBaseInstance.AfterSyncUpProcessing(synchronizableModel, postResponse);

                if (postResponse.Successful)
                {
                    return(postResponse);
                }
            }

            if (connTran == null)
            {
                return(postResponse);
            }

            this.Logger.Debug("Didn't sync so we try and queue");
            syncRec = new SyncRecord
            {
                ModelType = syncModel.TableName,
                RequestId = syncModel.RequestId,
                ModelId   = syncModel.Id,
                Status    = RecordStatus.Pending
            };

            bool queued = this.QueueRecordForSyncing(connTran, syncRec);

            if (queued)
            {
                if (postResponse != null)
                {
                    return(postResponse);
                }

                Guid requestId = syncModel.RequestId;

                return(new PostResponse
                {
                    RequestId = requestId,
                    Successful = false,
                    ResponseText = string.Empty
                });
            }

            throw new NotQueuedException(syncModel.TableName);
        }