Exemplo n.º 1
0
        /*--------------------------------------------------------------------------------------------*/
        protected virtual FabricResponse <T> GetFabricResponse(IClientContext pContext)
        {
            string fullPath = pContext.Config.ApiPath + Path + (Query != null ? "?" + Query : "");

            pContext.LogInfo("Request initiated...");

            ////

            try {
                pContext.LogInfo("Request Path: " + Method + " " + Path);
                pContext.LogInfo("Request URL: " + fullPath);

                IFabricHttpResponse wr = GetHttpWebResponse(fullPath);

                string data = StreamToString(wr.GetResponseStream());
                pContext.LogDebug("Request Response: " + data);
                return(new FabricResponse <T>(JsonSerializer.DeserializeFromString <T>(data)));
            }
            catch (WebException we) {
                if (we.Response == null)
                {
                    throw new Exception("No Fabric response from " + Method + " " + fullPath);
                }

                string data       = StreamToString(we.Response.GetResponseStream());
                bool   isOauthErr = (data.Length > 9 && data.Substring(0, 9) == "{\"error\":");
                bool   isRespErr  = typeof(FabResponse).IsAssignableFrom(typeof(T));

                pContext.LogDebug("Request Error: " + data +
                                  " (IsError=" + isRespErr + ", IsOauthError=" + isOauthErr + ")");

                if (isRespErr)
                {
                    FabResponse frErr = JsonSerializer.DeserializeFromString <FabResponse>(data);
                    return(new FabricResponse <T>(frErr, we));
                }

                if (isOauthErr)
                {
                    FabOauthError oerr = JsonSerializer.DeserializeFromString <FabOauthError>(data);
                    return(new FabricResponse <T>(oerr, we));
                }

                throw;
            }
        }
Exemplo n.º 2
0
        ////////////////////////////////////////////////////////////////////////////////////////////////
        /*--------------------------------------------------------------------------------------------*/
        public T Send(IClientContext pContext, SessionType pSessionType)
        {
            switch (pSessionType)
            {
            case SessionType.Default:
                vSess = pContext.ActiveSess;
                break;

            case SessionType.App:
                vSess = pContext.AppSess;
                break;

            case SessionType.Person:
                vSess = pContext.PersonSess;
                break;
            }

            pContext.LogDebug("SEND: " + pSessionType + " / " + vSess + " / " + pContext.AppSess);
            FabricResponse <T> resp = GetFabricResponse(pContext);

            if (resp.RespError != null)
            {
                pContext.LogError("Request Exception: " + resp.RespError.Error.Code + " / " +
                                  resp.RespError.Error.Name + " / " + resp.RespError.Error.Message);
                throw new FabricErrorException(resp.RespError);
            }

            if (resp.OauthError != null)
            {
                pContext.LogError("Request OAuth Exception: " +
                                  resp.OauthError.error + " / " + resp.OauthError.error_description);
                throw new FabricErrorException(resp.OauthError);
            }

            return(resp.Data);
        }