예제 #1
0
        private TReturn Query <TReturn>(string apiEndPoint, RestParameterSet parameter, int maxTries)
        {
            var para = parameter.CreateParamString(secret);


            string url = serverbasepath + "/" + apiEndPoint + ".php" + para.Item1;

#if DEBUG
            if (DebugSettings.Get("DebugNetwork"))
            {
                SAMLog.Debug($"QueryAsync('{apiEndPoint}', '{url}', {maxTries})");
            }
#endif

            for (;;)
            {
                try
                {
                    HttpResponseMessage response;
                    if (para.Item2 != null)
                    {
                        response = http.PostAsync(url, para.Item2).Result;
                    }
                    else
                    {
                        response = http.GetAsync(url).Result;
                    }

                    response.EnsureSuccessStatusCode();
                    var content = response.Content.ReadAsStringAsync().Result;
#if DEBUG
                    if (DebugSettings.Get("DebugNetwork"))
                    {
                        var json   = CompactJsonFormatter.CompressJson(content, 1);
                        var jlines = json.Replace("\r\n", "\n").Split('\n');
                        if (jlines.Length > 7)
                        {
                            json = string.Join("\n", jlines.Take(3).Concat(new[] { "..." }).Concat(jlines.Reverse().Take(3).Reverse()));
                        }
                        SAMLog.Debug($"Query '{apiEndPoint}' returned \r\n" + json);
                    }
#endif
                    return(JsonConvert.DeserializeObject <TReturn>(content));
                }
                catch (Exception e)
                {
                    if (maxTries > 0)
                    {
                        maxTries--;
                        SAMLog.Info("QueryAsync", $"Retry query '{url}'. {maxTries}remaining", e.Message);
                        MonoSAMGame.CurrentInst.Bridge.Sleep(RETRY_SLEEP_TIME);
                        continue;
                    }
                    else
                    {
                        throw new RestConnectionException(e);                         // return to sender
                    }
                }
            }
        }
예제 #2
0
 protected Task <TReturn> QueryAsync <TReturn>(string apiEndPoint, RestParameterSet parameter, int maxTries)
 {
     return(Task.Run(() => Query <TReturn>(apiEndPoint, parameter, maxTries)));
 }