예제 #1
0
        public async Task <GetRecordResponse> Handle(PrivateSession session, GetRecordRequest request, Func <PrivateConnection> connectionFactory)
        {
            var requestModel = new Json.GetRecordRequest {
                Resource = (int)request.Resource, Fields = request.Fields.ToString(), Records = request.Records.ToList()
            };

            using (var connection = connectionFactory().ApiName(ApiName).Method(PrivateConnectionMethod.Read))
            {
                try
                {
                    using (var response = await connection.SendAsync(requestModel))
                    {
                        var data = await response.ReadAsync <Json.GetRecordResponse>();

                        return(new GetRecordResponse((from item in data.Items
                                                      let idField = ResourceType.Standard.ToIdField(request.Resource)
                                                                    select ReadDataCollection(idField, item.Fields, data.Versions)).ToList()));
                    }
                }
                catch (ConnectionResponseException e)
                {
                    // FIXME parse response object stream inside connection
                    using (var s = new MemoryStream())
                    {
                        await e.Response.CopyToAsync(s);

                        s.Position = 0;
                        using (var r = new StreamReader(s))
                        {
                            throw new GetRecordException(string.Format("Could not read records: {0}", r.ReadToEnd()), request, e);
                        }
                    }
                }
            }
        }
예제 #2
0
        public async Task <GetRecordResponse> Handle(PrivateSession session, GetRecordRequest request, Func <PrivateConnection> connectionFactory)
        {
            var requestModel = new Json.GetRecordRequest {
                Resource = (int)request.Resource, Fields = request.Fields.ToString(), Records = request.Records.ToList()
            };

            using (var connection = connectionFactory().ApiName(ApiName).Method(PrivateConnectionMethod.Read))
            {
                try
                {
                    using (var response = await connection.SendAsync(requestModel))
                    {
                        var data = await response.ReadAsync <Json.GetRecordResponse>();

                        return(new GetRecordResponse((from item in data.Items
                                                      let idField = ResourceType.Standard.ToIdField(request.Resource)
                                                                    select ReadDataCollection(idField, item.Fields, data.Versions)).ToList()));
                    }
                }
                catch (ConnectionResponseException e)
                {
                    throw e.ToRequestException("Could not read records", request);
                }
            }
        }