示例#1
0
        async Task <ApiResponse <TResponse> > PostJson <TResponse>(string relativeUrl, object req)
        {
            var jsonReq = _converter.ToJson(req);

            _logger.Debug(this, $"Request ({req.GetType().Name}) to '{relativeUrl}': '{jsonReq}'");
            var netResponse = await _network.PostJson(relativeUrl, jsonReq);

            ApiResponse <TResponse> result;

            if (netResponse.Success)
            {
                var response = _converter.FromJson <TResponse>(netResponse.Text);
                result = response.AsResult();
            }
            else
            {
                var error = ConvertToError(netResponse);
                _errorHandle.OnError(error);
                result = error.AsError <TResponse>();
            }
            _logger.Debug(this, $"Response ({typeof(TResponse).Name}) from '{relativeUrl}': {result.Success}, '{result.Result}', {result.Error}");
            if (!result.Success)
            {
                OnError(result.Error);
            }
            return(result);
        }
示例#2
0
        public bool TryAdd(UserState user)
        {
            var doc = BsonSerializer.Deserialize <BsonDocument>(_convert.ToJson(user));

            _users.InsertOne(new UserModel {
                Login = user.Login, User = doc
            });
            return(true);
        }
示例#3
0
        public IActionResult Wrap <T>(ApiResponse <T> response)
        {
            switch (response.Error)
            {
            case null: return(new ObjectResult(_convert.ToJson(response.Result)));

            case ClientError e: return(new BadRequestObjectResult(e.Message));

            case ConflictError e: return(new ConflictObjectResult(e.Message));

            case ServerError e: return(new ObjectResult(e.Message)
                {
                    StatusCode = 500
                });

            default: throw new InvalidOperationException("Unexpected response type");
            }
        }
        BsonDocument ToBson(GameState state)
        {
            var json = _convert.ToJson(state);

            return(BsonSerializer.Deserialize <BsonDocument>(json));
        }
示例#5
0
        public void Save()
        {
            var contents = _convert.ToJson(State);

            File.WriteAllText(_path, contents);
        }