Ejemplo n.º 1
0
        public async Task <OutpT> RunHttpRequest <InpT, OutpT>(
            string interfaceName, string methodName, Func <string, OutpT> deserialize, InpT inp)
        {
            var inputAsJson = JSON.Stringify(inp);
            var requestId   = Guid.NewGuid().ToString();

            var url = string.Format("/{0}/{1}", interfaceName, methodName);

            Logger.Debug(typeof(BridgeHttpRequester), "Request id={0} to={1} starting", requestId, url);
            var result = await Task.FromPromise <ResultHolder <XMLHttpRequest> >(
                new XMLHttpRequestImplementingIPromise("POST", url, inputAsJson),
                (Func <ResultHolder <XMLHttpRequest>, ResultHolder <XMLHttpRequest> >)(x => x));

            if (result.Success)
            {
                Logger.Debug(typeof(BridgeHttpRequester), "Request id={0} Success now will deserialize", requestId);
                var bsd = deserialize(BridgeObjectUtil.NoOpCast <string>(result.Result.ResponseText));
                Logger.Debug(typeof(BridgeHttpRequester), "Success ok deserialized");

                return(bsd);
            }

            var answer = result.Result.ResponseText;

            Logger.Error(
                typeof(BridgeHttpRequester),
                $"Failed request id={requestId} while calling server. Got status={result.Result.Status} Response={answer}");
            var errMsg = result.Result.Status == 400 ? answer.TillFirstNewLineOrEverything() : answer;

            throw new Exception(errMsg);
        }
Ejemplo n.º 2
0
        public TValue Get(TKey key)
        {
            var result = _impl.Get(key);

            if (result == null)
            {
                // friendlier exception instead of default Bridge.InvalidCastException
                throw new KeyNotFoundException("No value for key present in WeakMap");
            }

            return(BridgeObjectUtil.NoOpCast <TValue>(result));
        }
Ejemplo n.º 3
0
        public async Task <RemoteFileId[]> Upload(
            IEnumerable <Bridge.Html5.File> uploaded, FileUploadOperation operation, RemoteFileId toReplaceOrRemoveId)
        {
            var input = _input?.Invoke();
            var res   = await XMLHttpRequestUtils.Upload(
                _interfaceName, _setterMethod, uploaded, operation, toReplaceOrRemoveId, input);

            if (!res.Success)
            {
                throw new Exception(res.Result.ResponseText);
            }

            var output = res.Result.Response;

            Logger.Debug(GetType(), "upload success file id json is {0}", output);
            return(JsonConvert.DeserializeObject <RemoteFileId[]>(
                       BridgeObjectUtil.NoOpCast <string>(output)));
        }