Exemplo n.º 1
0
 public async Task <TResp> HttpGet <TResp>(string uri)
 {
     try
     {
         if (_stopWatch != null)
         {
             _stopWatch.Restart();
         }
         HttpClient client = GetHttpClient();
         using (HttpResponseMessage httpresp = await client.GetAsync(uri, _cts.Token))
         {
             LastStatus = httpresp.StatusCode;
             if (httpresp.IsSuccessStatusCode)
             {
                 using (Stream strm = await httpresp.Content.ReadAsStreamAsync())
                 {
                     TResp resp = _json.Deserialize <TResp>(strm);
                     if (_stopWatch != null)
                     {
                         _stopWatch.Stop();
                     }
                     return(resp);
                 }
             }
         }
     }
     catch (Exception ex)
     {
         Dispose();
         LastStatus = HttpStatusCode.NotAcceptable;          // UnMarshall error
         _logger.LogWarning($"RestClient | HttpGet : {uri} : { ex.Message}");
     }
     return(default(TResp));
 }
Exemplo n.º 2
0
        public static T ReadAndDeserialize <T>(this string file, IJson json)
        {
            file.VerifyNotEmpty(nameof(file));
            json.VerifyNotNull(nameof(json));

            return(json.Deserialize <T>(File.ReadAllText(file)));
        }
Exemplo n.º 3
0
        public async Task Import(CancellationToken token)
        {
            IReadOnlyList <string> files = GetFiles(_option.File !);

            foreach (var file in files)
            {
                _logger.LogInformation($"Importing configuration {file}");

                string json = File.ReadAllText(file);
                switch (_json.Deserialize <RecordBase>(json))
                {
                case RecordBase v:
                    await WriteRecord(v.RecordType, json, token);

                    break;

                default:
                    throw new ArgumentException($"Bad format");
                }
            }
        }
Exemplo n.º 4
0
        public async Task GivenMlHost_WhenPing_ShouldResponed()
        {
            TestWebsiteHost host = await TestApplication.GetHost();

            IJson jsonSerializer = host.Resolve <IJson>();

            var response = await host.Client.GetAsync("api/ping");

            response.EnsureSuccessStatusCode();

            var responseString = await response.Content.ReadAsStringAsync();

            PingResponse pingResponse = jsonSerializer.Deserialize <PingResponse>(responseString);

            pingResponse.Should().NotBeNull();
            pingResponse.Status.Should().NotBeNullOrEmpty();
            pingResponse.Status.Func(x => _validResponses.Contains(x !)).Should().BeTrue();
        }
Exemplo n.º 5
0
        public void Load(TextReader stream)
        {
            if (stream == default)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            TerrainFile?terrainConfig = _json.Deserialize <TerrainFile>(stream.ReadToEnd());

            if (terrainConfig == null)
            {
                throw new InvalidOperationException("Unable to parse terrain file.");
            }

            foreach (TerrainConfig entry in terrainConfig.Terrain)
            {
                PhaseConfig phase   = terrainConfig.Phase.Where(p => string.Equals(p.Name, entry.PhaseName, StringComparison.OrdinalIgnoreCase)).First();
                var         terrain = new Terrain(entry.Id, entry.IdName, phase.Phases.Select(p => {
                    return(transition: p.Transition, attribute: p as ITerrainAttributes);
                }));

                _terrain[terrain.Id] = terrain;
            }
        }