예제 #1
0
 /// <summary>
 /// Creates the HTTP json request.
 /// </summary>
 /// <param name="self">The self.</param>
 /// <param name="url">The URL.</param>
 /// <param name="method">The method.</param>
 /// <param name="metadata">The metadata.</param>
 /// <param name="credentials">The credentials.</param>
 /// <returns></returns>
 public static HttpJsonRequest CreateHttpJsonRequest(object self, Uri url, RequestMethod method, JObject metadata,
                                                     ICredentials credentials)
 {
     var request = new HttpJsonRequest(url, method, metadata, credentials);
     ConfigureRequest(self, new WebRequestEventArgs {Request = request.webRequest});
     return request;
 }
예제 #2
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="requestUri"></param>
        /// <param name="method"></param>
        /// <param name="request"></param>
        protected void CreateContext(Uri requestUri, RequestMethod method, out HttpJsonRequest request)
        {
            Guard.Assert(() => requestUri != null);

            var metadata = new JObject();

            request = HttpJsonRequest.CreateHttpJsonRequest(this, new Uri(requestUri.AbsoluteUri + Guid.NewGuid()),
                                                            method, metadata, Credentials);

            ContextStorage.Add(request, SynchronizationContext.Current);
        }
예제 #3
0
        private void AttachmentDelete_Callback(string key, HttpJsonRequest request, IAsyncResult result,
                                               CallbackFunction.SaveMany<string> callback)
        {
            SynchronizationContext context = GetContext(request);
            DeleteContext(request);

            HttpStatusCode statusCode;
            Exception exception;
            NameValueCollection headers;
            string json = GetResponseStream(request.HttpWebRequest, result, out statusCode, out exception, out headers);

            var deleteResponse = new DeleteResponse<string>
                                     {
                                         Data = key,
                                         StatusCode = statusCode,
                                         Exception = exception
                                     };

            context.Post(
                delegate { callback.Invoke(new List<Response<string>> { deleteResponse }); },
                null);
        }
예제 #4
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        protected SynchronizationContext GetContext(HttpJsonRequest request)
        {
            Guard.Assert(() => ContextStorage.ContainsKey(request));

            return ContextStorage[request];
        }
예제 #5
0
        private void AttachmentGetAll_Callback(HttpJsonRequest request, IAsyncResult result, CallbackFunction.Load<IList<KeyValuePair<string, Attachment>>> callback)
        {
            SynchronizationContext context = GetContext(request);
            DeleteContext(request);

            HttpStatusCode statusCode;
            Exception exception;
            NameValueCollection headers;

            string json = GetResponseStream(request.HttpWebRequest, result, out statusCode, out exception, out headers);

            JArray array = JArray.Parse(json);

            var attachments = new List<KeyValuePair<string, Attachment>>();
            string key;

            foreach (JToken attachment in array)
            {
                attachments.Add(AttachmentMapper.Map(attachment));
            }

            var loadResponse = new LoadResponse<IList<KeyValuePair<string, Attachment>>>
                                   {
                                       Data = attachments,
                                       StatusCode = statusCode,
                                       Exception = exception
                                   };

            context.Post(
                delegate { callback.Invoke(loadResponse); },
                null);
        }
예제 #6
0
        private void AttachmentGet_Callback(string key, HttpJsonRequest request, IAsyncResult result,
                                            CallbackFunction.Load<KeyValuePair<string, Attachment>> callback)
        {
            SynchronizationContext context = GetContext(request);
            DeleteContext(request);

            HttpStatusCode statusCode;
            Exception exception;

            Attachment attachment = AttachmentMapper.Map(key, request.HttpWebRequest, result, out statusCode,
                                                         out exception);

            var loadResponse = new LoadResponse<KeyValuePair<string, Attachment>>
                                   {
                                       Data = new KeyValuePair<string, Attachment>(key, attachment),
                                       StatusCode = statusCode,
                                       Exception = exception
                                   };

            context.Post(
                delegate { callback.Invoke(loadResponse); },
                null);
        }
예제 #7
0
        private void IndexSave_Callback(string name, IndexDefinition entity, HttpJsonRequest request,
                                        IAsyncResult result,
                                        CallbackFunction.SaveOne<KeyValuePair<string, IndexDefinition>> callback)
        {
            SynchronizationContext context = GetContext(request);
            DeleteContext(request);

            HttpStatusCode statusCode;
            Exception exception;
            NameValueCollection headers;
            string json = GetResponseStream(request.HttpWebRequest, result, out statusCode, out exception, out headers);

            var saveResponse = new SaveResponse<KeyValuePair<string, IndexDefinition>>
                                   {
                                       Data = new KeyValuePair<string, IndexDefinition>(name, entity),
                                       StatusCode = statusCode,
                                       Exception = exception
                                   };

            context.Post(
                delegate { callback.Invoke(saveResponse); },
                null);
        }
예제 #8
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        protected void DeleteContext(HttpJsonRequest request)
        {
            Guard.Assert(() => request != null);

            ContextStorage.Remove(request);
        }
예제 #9
0
        private void IndexGet_Callback(string name, HttpJsonRequest request, IAsyncResult result,
                                       CallbackFunction.Load<KeyValuePair<string, IndexDefinition>> callback)
        {
            SynchronizationContext context = GetContext(request);
            DeleteContext(request);

            HttpStatusCode statusCode;
            Exception exception;
            NameValueCollection headers;
            string json = GetResponseStream(request.HttpWebRequest, result, out statusCode, out exception, out headers);

            var index = new KeyValuePair<string, IndexDefinition>();
            if (exception == null)
            {
                index = IndexMapper.Map(json);
            }

            var loadResponse = new LoadResponse<KeyValuePair<string, IndexDefinition>>
                                   {
                                       Data = index,
                                       StatusCode = statusCode,
                                       Exception = exception
                                   };

            context.Post(
                delegate { callback.Invoke(loadResponse); },
                null);
        }
예제 #10
0
        private void DocumentBatch_Callback(IList<ICommandData> batchCommands, HttpJsonRequest request,
                                            IAsyncResult result, CallbackFunction.Batch batchCallback)
        {
            SynchronizationContext context = GetContext(request);
            DeleteContext(request);

            HttpStatusCode statusCode;
            Exception exception;
            NameValueCollection headers;
            string json = GetResponseStream(request.HttpWebRequest, result, out statusCode, out exception, out headers);

            var batchResults = JsonConvert.DeserializeObject<BatchResult[]>(json);

            context.Post(
                delegate { batchCallback.Invoke(batchResults.ToList()); },
                null);
        }
예제 #11
0
        private void DocumentSave_Callback(JsonDocument document, HttpJsonRequest request, IAsyncResult result,
                                           CallbackFunction.SaveMany<JsonDocument> callback)
        {
            SynchronizationContext context = GetContext(request);
            DeleteContext(request);

            HttpStatusCode statusCode;
            Exception exception;
            NameValueCollection headers;
            string json = GetResponseStream(request.HttpWebRequest, result, out statusCode, out exception, out headers);

            if (exception == null)
            {
                JObject responseJson = JObject.Parse(json);

                document.Etag = new Guid(responseJson["ETag"].ToString().Replace("\"", string.Empty));
                document.Key = responseJson["Key"].ToString().Replace("\"", string.Empty);
            }

            var saveResponse = new SaveResponse<JsonDocument>
                                   {
                                       Data = document,
                                       StatusCode = statusCode,
                                       Exception = exception
                                   };

            context.Post(
                delegate { callback.Invoke(new List<Response<JsonDocument>> { saveResponse }); },
                null);
        }
예제 #12
0
        private void DocumentGetAll_Callback(HttpJsonRequest request, IAsyncResult result,
                                             CallbackFunction.Load<IList<JsonDocument>> callback)
        {
            SynchronizationContext context = GetContext(request);
            DeleteContext(request);

            HttpStatusCode statusCode;
            Exception exception;
            NameValueCollection headers;
            string json = GetResponseStream(request.HttpWebRequest, result, out statusCode, out exception, out headers);
            JArray array = JArray.Parse(json);

            var responseResult = new List<JsonDocument>();

            if (exception == null)
            {
                responseResult = array.Select(jsonDocument => DocumentMapper.Map(jsonDocument.ToString())).ToList();
            }

            var loadResponse = new LoadResponse<IList<JsonDocument>>
                                   {
                                       Data = responseResult,
                                       StatusCode = statusCode,
                                       Exception = exception
                                   };

            context.Post(
                delegate { callback.Invoke(loadResponse); },
                null);
        }
예제 #13
0
        private void DocumentGet_Callback(string key, HttpJsonRequest request, IAsyncResult result,
                                          CallbackFunction.Load<JsonDocument> callback)
        {
            SynchronizationContext context = GetContext(request);
            DeleteContext(request);

            HttpStatusCode statusCode;
            Exception exception;
            NameValueCollection headers;
            string json = GetResponseStream(request.HttpWebRequest, result, out statusCode, out exception, out headers);

            JsonDocument document = null;
            if (exception == null)
            {
                document = DocumentMapper.Map(json);
                document.Metadata = headers.FilterHeaders(isServerDocument: false);
                document.Key = key;
            }

            var loadResponse = new LoadResponse<JsonDocument>
                                   {
                                       Data = document,
                                       StatusCode = statusCode,
                                       Exception = exception
                                   };

            context.Post(
                delegate { callback.Invoke(loadResponse); },
                null);
        }
예제 #14
0
 private void LinearQuery_Callback(HttpJsonRequest request, IAsyncResult responseResult, CallbackFunction.Load<IList<JsonDocument>> callback)
 {
     DocumentGetMany_Callback(request, responseResult, callback);
 }
예제 #15
0
        private void AttachmentPut_Callback(string key, Guid? etag, byte[] data, JObject metadata,
                                            HttpJsonRequest request, IAsyncResult result,
                                            CallbackFunction.SaveMany<Attachment> callback)
        {
            SynchronizationContext context = GetContext(request);
            DeleteContext(request);

            HttpStatusCode statusCode;
            Exception exception;
            NameValueCollection headers;
            string json = GetResponseStream(request.HttpWebRequest, result, out statusCode, out exception, out headers);

            var saveResponse = new SaveResponse<Attachment>
                                   {
                                       Data = new Attachment
                                                  {
                                                      Data = data,
                                                      Metadata = metadata
                                                  },
                                       StatusCode = statusCode,
                                       Exception = exception
                                   };

            context.Post(
                delegate { callback.Invoke(new List<Response<Attachment>> { saveResponse }); },
                null);
        }
예제 #16
0
        private void StatisticsGet_Callback(HttpJsonRequest request, IAsyncResult result, CallbackFunction.Load<DatabaseStatistics> callback)
        {
            SynchronizationContext context = GetContext(request);
            DeleteContext(request);

            HttpStatusCode statusCode;
            Exception exception;
            NameValueCollection headers;
            string json = GetResponseStream(request.HttpWebRequest, result, out statusCode, out exception, out headers);

            var statistics = this.StatisticsMapper.Map(json);

            var loadResponse = new LoadResponse<DatabaseStatistics>()
                                   {
                                       Data = statistics,
                                       Exception = exception,
                                       StatusCode = statusCode
                                   };

            context.Post(delegate { callback.Invoke(loadResponse); }, null);
        }
예제 #17
0
        private void IndexGetAll_Callback(IDictionary<string, IndexDefinition> existingEntities, HttpJsonRequest request,
                                          IAsyncResult result,
                                          CallbackFunction.Load<IDictionary<string, IndexDefinition>> callback)
        {
            SynchronizationContext context = GetContext(request);
            DeleteContext(request);

            HttpStatusCode statusCode;
            Exception exception;
            NameValueCollection headers;
            string json = GetResponseStream(request.HttpWebRequest, result, out statusCode, out exception, out headers);
            JArray array = JArray.Parse(json);

            var responseResult = new Dictionary<string, IndexDefinition>();
            if (exception == null)
            {
                var entities = array.Select(index => IndexMapper.Map(index.ToString()));

                responseResult = responseResult.Concat(entities).ToDictionary(x => x.Key, y => y.Value);

                if (existingEntities != null)
                {
                    responseResult.Concat(existingEntities);
                }
            }

            var loadResponse = new LoadResponse<IDictionary<string, IndexDefinition>>
                                   {
                                       Data = responseResult,
                                       StatusCode = statusCode,
                                       Exception = exception
                                   };

            context.Post(
                delegate { callback.Invoke(loadResponse); },
                null);
        }