Example #1
0
        private void ProcessRequest <TResult>(UnityWebRequest request, Request req, ITimedFuture <Response <TResult> > result)
        {
            Verbose(5, $"Requesting {typeof(TResult).Name}, {req}");

            IEnumerator Coro(IGenerator self)
            {
                void SetError(long code, string text = "")
                {
                    Warn($"Error: {code}: {text}, for {req}.");
                    result.Value = new Response <TResult>(new RequestResult(req, ConvertToError(code), text));
                }

                using (request)
                {
                    request.SetRequestHeader("Content-Type", "application/json");
                    request.SetRequestHeader("Accept", "application/json");
                    if (!string.IsNullOrEmpty(Token))
                    {
                        request.SetRequestHeader("Authorization", $"Bearer {Token}");
                    }

                    request.SendWebRequest();
                    result.TimedOut += tr =>
                    {
                        SetError(408L, "Timed out");
                        request.Abort();
                        self.Complete();
                    };

                    yield return(self.ResumeAfter(() => request.isDone));

                    if (request.isNetworkError || request.isHttpError)
                    {
                        SetError(request.responseCode, request.error);
                        yield break;
                    }

                    try
                    {
                        var settings = new JsonSerializerSettings {
                            DateFormatString = "yy-MM-ddTHH-mm-ss"
                        };
                        var res = JsonConvert.DeserializeObject <TResult>(request.downloadHandler.text, settings);
                        result.Value = new Response <TResult>(req, res);
                        Verbose(10, $"Request succeeded, for {req}");
                    }
                    catch (Exception e)
                    {
                        Error($"Caught error when deserialising:\nMessage: {e.Message}\nInner: {e.InnerException?.Message}");
                        SetError(500L, e.Message);
                    }
                }
            }

            Run(Coro);
        }
Example #2
0
        private bool MetadataInCache(Guid id, out ITimedFuture <Response <ResourceMetadata> > futureMetadata)
        {
            if (Model.TryGetMetadata(id, out var metadata))
            {
                metadata.LastAccessTime = DateTime.UtcNow;
                futureMetadata          = New.TimedFuture(_DefaultTimeOut, new Response <ResourceMetadata>(new Request(), metadata));
                return(true);
            }

            futureMetadata = null;

            return(false);
        }