Ejemplo n.º 1
0
        public async Task <ActionResult> Connect(Guid uniqueId, int externalApiId)
        {
            var externalApi     = ExternalApi.ChartMogul.GetById(externalApiId);
            var externalApiAuth = new ExternalApiAuth {
                ExternalApiId = externalApiId
            };

            // Replace with requested object
            if (uniqueId != Guid.Empty)
            {
                externalApiAuth = _externalApiAuthManager.GetExternalApiAuth(CompanyId, uniqueId);
            }

            switch (externalApi.ApiAuthorizationType)
            {
            case ExternalApiAuthorizationType.OAuth20:
                // Hard coded to Google for now
                var company = _companyManager.Get(CompanyId);
                Session["CompanyGuid"] = company.UniqueId.ToString();
                var authResult = await _googleAuthorizer.Authorize(company.UniqueId, externalApiAuth, Url.Action("SetGoogleAnalyticsSiteId"), CancellationToken.None);

                if (authResult.Credential == null)
                {
                    return(View("_startOAuth", new OAuthStartAuthorisation {
                        ExternalApi = externalApi, StartUrl = authResult.RedirectUri
                    }));
                }

                try
                {
                    var accountSummaries = await _googleAuthorizer.GetAccountSummaries(authResult);

                    if (string.IsNullOrWhiteSpace(externalApiAuth.ConfigData))
                    {
                        // Default to first profile
                        externalApiAuth.ConfigData = accountSummaries.items[0].webProperties[0].profiles[0].id;
                        _externalApiAuthManager.Update(externalApiAuth);
                    }
                    return(View("_manageOAuth", new GoogleOAuthConfigView {
                        ExternalApi = externalApi, ExternalApiAuth = externalApiAuth, Accounts = accountSummaries
                    }));
                }
                catch
                {
                    _externalApiAuthManager.Delete(externalApiAuth.Id);
                    return(Content("We couldn't retrieve your Google Analytics site list.\nTry refreshing the page and re-connecting to Google Analytics"));
                }

            default:
                return(View("_connect", externalApiAuth));
            }
        }
Ejemplo n.º 2
0
        public Task StoreAsync <T>(string key, T value)
        {
            var companyGuid = ExtractCompanyGuid(key);
            var serialized  = NewtonsoftJsonSerializer.Instance.Serialize(value);

            var typeParameterType = typeof(T);

            Debug.WriteLine("GETASYNC");
            Debug.WriteLine("Key is:" + key);
            Debug.WriteLine("T is:" + typeParameterType.FullName);
            Debug.WriteLine("value is:" + value);

            // Check the serialized data can be converted back,
            // if not then it wasn't a valid value and shouldn't be saved
            try
            {
                var check = NewtonsoftJsonSerializer.Instance.Deserialize <T>(serialized);
            }
            catch (Exception ex)
            {
                return(Task.Delay(0));
            }

            var externalApiAuth = _externalApiAuthManager.GetByCompanyGuid(companyGuid, ExternalApi.GoogleAnalytics.Id);

            if (externalApiAuth == null)
            {
                // New User we insert it into the database
                externalApiAuth = new ExternalApiAuth {
                    ExternalApiId = ExternalApi.GoogleAnalytics.Id
                };
                externalApiAuth = StoreValue(key, serialized, externalApiAuth);
                _externalApiAuthManager.SaveWithCompanyGuid(companyGuid, externalApiAuth);
            }
            else
            {
                // Existing User We update it
                externalApiAuth = StoreValue(key, serialized, externalApiAuth);
                _externalApiAuthManager.Update(externalApiAuth);
            }

            return(Task.Delay(0));
        }