public async Task <string> RequestPartnerPermissions(string email, string myRequestingOrgId)
        {
            User currentUser = await getCurrentUser(true);

            //find my organization
            Organization currentOrg = null;

            if (currentUser.Organizations != null && currentUser.Organizations.Count() > 0)
            {
                currentOrg = currentUser.Organizations.SingleOrDefault(o => o.id == myRequestingOrgId);
            }

            var orgLink     = OAuthWorkFlow.linksFrom(currentOrg)["self"].uri;
            var partnerLink = await PostPartnershipRequest(email, orgLink);

            if (!String.IsNullOrEmpty(partnerLink))
            {
                Resource partnership = await getPartnership(partnerLink);

                //get contact invitation link
                var links             = OAuthWorkFlow.linksFrom(partnership);
                var contactInviteLink = links["contactInvitation"];


                Resource contactInvite = await getPartnership(contactInviteLink.uri);

                partnerLink = contactInviteLink.uri;

                var contactLinks = OAuthWorkFlow.linksFrom(contactInvite);
                await PostPermissionRequest(contactLinks["permissions"].uri);
            }

            return(partnerLink);
        }
        private async Task <bool> createDownloadRequest(DataContracts.File file, long start, long bytesToRead, Stream output)
        {
            var links = OAuthWorkFlow.linksFrom(file);

            var url = links["self"].uri;

            url = url.TrimEnd("?&/".ToCharArray());
            url = url + "?offset=" + start.ToString() + "&size=" + bytesToRead.ToString();

            var response = await SecuredApiFileGetRequest(url);


            Logger.Log("RESOURCE", "createDownloadRequest");

            Logger.Log(response);

            if (response.StatusCode == System.Net.HttpStatusCode.OK)
            {
                using (Stream input = await response.Content.ReadAsStreamAsync())
                {
                    input.CopyTo(output);
                }
                return(true);
            }
            else
            {
                return(false);
            }
        }
        private async Task fillPartnershipTree(Partnership p, bool fetchNewFiles)
        {
            //get fromPartnership
            Dictionary <String, Link> partnershipLinks = OAuthWorkFlow.linksFrom(p);
            Link fromPartnerShipLink = partnershipLinks["fromPartnership"];
            Link toPartnerShipLink   = partnershipLinks["toPartnership"];

            //only try to fetch partner details if not a mailto link
            if (fromPartnerShipLink.uri.ToLower().IndexOf("mailto:") < 0)
            {
                if (!fromPartnerShipLink.uri.ToLower().StartsWith("mailto:"))
                {
                    p.FromOrg = await getOrganization(fromPartnerShipLink, true);
                }

                if (!toPartnerShipLink.uri.ToLower().StartsWith("mailto:"))
                {
                    p.ToOrg = await getOrganization(toPartnerShipLink, true);
                }

                if (partnershipLinks.ContainsKey("contactInvitation"))
                {
                    p.ContactInvite = await getResourceFromCache(partnershipLinks["contactInvitation"].uri);
                }



                if (p.FromOrg != null && !string.IsNullOrEmpty(p.FromOrg.id)) //ADDED MB
                {
                    var orgLinks = OAuthWorkFlow.linksFrom(p.FromOrg);
                    if (orgLinks.ContainsKey("self"))
                    {
                        string filesLink = orgLinks["self"].uri.TrimEnd("/".ToCharArray()) + "/files";

                        if (fetchNewFiles) //only fetch files when we are wanting to download
                        {
                            p.SharedFiles = await getPartnerFileLinks(filesLink, p.FromOrg.id, fetchNewFiles);

                            if (p.SharedFiles != null)
                            {
                                p.TotalFileCount = p.SharedFiles.Count();
                            }
                        }
                        else
                        {
                            p.TotalFileCount = await getCount <DataContracts.File>(filesLink);
                        }
                    }
                }
            }
        }
        public async Task retrieveApiCatalog(string endpoint)
        {
            apiCataloglinks = (Dictionary <String, Link>)CacheManager.GetCacheItem("api_catalog_links");

            if (apiCataloglinks == null)
            {
                var response = await SecuredApiGetRequest(endpoint);

                ApiCatalog apiCatalog = await PartnershipWorkflow.DeserialiseAsync <ApiCatalog>(response.Content);

                apiCataloglinks = OAuthWorkFlow.linksFrom(apiCatalog);
                CacheManager.AddCacheItem("api_catalog_links", apiCataloglinks, 60 * 6);
            }
        }
        public async Task <List <Partnership> > getPartnerships(bool fetchNewFiles, string[] restrictToOrgIds)
        {
            List <Partnership> relavantPartnerships = null;

            User currentUser = await getCurrentUser(true);

            //find my organization
            //Organization currentOrg = null;

            /*if (currentUser.Organizations != null && currentUser.Organizations.Count() > 0)
             * {
             *  currentOrg = currentUser.Organizations[0];
             * }*/

            List <Partnership> partnerships = await getPartnerShipsList(apiCataloglinks["partnerships"].uri);

            relavantPartnerships = new List <Partnership>();
            foreach (var p in partnerships)
            {
                var  partnershipLinks    = OAuthWorkFlow.linksFrom(p);
                Link fromPartnerShipLink = partnershipLinks["fromPartnership"];
                Link toPartnerShipLink   = partnershipLinks["toPartnership"];

                if (fromPartnerShipLink.uri.ToLower() != toPartnerShipLink.uri.ToLower() &&
                    currentUser.Organizations.Count(o => toPartnerShipLink.uri.ToLower().IndexOf(o.id.ToLower()) >= 0) > 0)
                {
                    //if a list of org ids is passed in restrict to then apply filter
                    if (restrictToOrgIds == null || restrictToOrgIds.Count() == 0 || restrictToOrgIds.Any(id => fromPartnerShipLink.uri.IndexOf(id) >= 0))
                    {
                        if (!relavantPartnerships.Any(r => OAuthWorkFlow.linksFrom(r)["fromPartnership"].uri == fromPartnerShipLink.uri))
                        {
                            await fillPartnershipTree(p, fetchNewFiles);

                            if (p.FromOrg != null && !string.IsNullOrEmpty(p.FromOrg.id))
                            {
                                relavantPartnerships.Add(p);
                            }
                        }
                    }
                }
            }
            //CacheManager.AddCacheItem("partnerships", relavantPartnerships, 5);

            return(relavantPartnerships);
        }
        public async Task <User> getCurrentUser(bool fillChildOrgs)
        {
            string cacheKey    = "current_user_childorgs_" + fillChildOrgs.ToString();
            User   currentUser = (User)CacheManager.GetCacheItem(cacheKey);

            if (currentUser == null)
            {
                var response = await SecuredApiGetRequest(apiCataloglinks["currentUser"].uri);

                currentUser = await PartnershipWorkflow.DeserialiseAsync <User>(response.Content);

                if (fillChildOrgs)
                {
                    currentUser.Organizations = await getList <Organization>(OAuthWorkFlow.linksFrom(currentUser)["organizations"].uri);
                }
                CacheManager.AddCacheItem(cacheKey, currentUser, 30);
            }
            return(currentUser);
        }
        public async Task <string> getOrgIdForCompletedPartnership(string resourceLink)
        {
            Logger.Log("RESOURCE", "getOrgIdForCompletePartnership");

            Resource p = await getPartnership(resourceLink);

            var partnershipLinks = OAuthWorkFlow.linksFrom(p);


            string link = this.ExtractTokenFromLink(partnershipLinks["fromPartnership"].uri);

            if (link.ToLower().IndexOf("mailto:") >= 0) //partnership is incomplete
            {
                return(string.Empty);
            }
            else
            {
                return(link);
            }
        }