public async Task <Application> AddApplicationAsync(ClientContext context, string teamId, NewApplication newApplication, Platform platform = Platform.Ios)
        {
            await Configure.AwaitFalse();

            var prefixes = await this.GetApplicationPrefixesAsync(context.AsCacheContext(), teamId, platform);

            newApplication.Prefix = prefixes.First();

            var uriBuilder = new AppleDeveloperRequestUriBuilder(
                new RestUri(this.UrlProvider.AddApplicationUrl, new { platform }));

            uriBuilder.AddQueryValues(new Dictionary <string, string> {
                { "teamId", teamId }
            });
            var request  = RestRequest.Post(uriBuilder.ToUri(), RestContentType.FormUrlEncoded, newApplication);
            var response = await this.SendAsync <Result <Application> >(context, request);

            this.CheckResultForErrors(response.Content);

            context.DeleteValue <List <Application> >(teamId, platform);

            var application = response.Content.Data;

            application.TeamId = teamId;

            return(application);
        }
        public async Task <ApplicationDetails> GetApplicationDetails(ClientContext context, Application application)
        {
            await Configure.AwaitFalse();

            if (context.IsForceFromBackend ||
                !context.TryGetValue(out ApplicationDetails applicationDetails, application.TeamId, application.Id, application.Platform))
            {
                var uriBuilder = new AppleDeveloperRequestUriBuilder(
                    new RestUri(this.UrlProvider.GetApplicationDetailsUrl, new {
                    platform = application.Platform
                }));
                uriBuilder.AddQueryValues(new Dictionary <string, string> {
                    { "teamId", application.TeamId }
                });
                var request = RestRequest.Post(uriBuilder.ToUri(), RestContentType.FormUrlEncoded, new {
                    appIdId = application.Id
                });
                var response = await this.SendAsync <Result <ApplicationDetails> >(context, request);

                this.CheckResultForErrors(response.Content);

                applicationDetails        = response.Content.Data;
                applicationDetails.TeamId = application.TeamId;

                context.AddValue(applicationDetails, application.TeamId, application.Id, application.Platform);
            }
            return(applicationDetails);
        }
        private async Task <List <TPageResult> > SendPageRequestAsync <TPageResult>(
            ClientContext context,
            RestRequest request,
            Dictionary <string, string> queryValues = null,
            string sortColumn = "name",
            int pageSize      = 500)
            where TPageResult : PageResult
        {
            await Configure.AwaitFalse();

            var uriBuilder = new AppleDeveloperRequestUriBuilder(request.Uri);

            uriBuilder.AddQueryValues(queryValues);

            var pages = new List <TPageResult>();

            var pageNumber = 1;
            var nd         = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
            var total      = 0;
            var sort       = sortColumn + "=asc";
            var count      = 0;

            do
            {
                request.Uri     = uriBuilder.ToUri();
                request.Content = new
                {
                    nd,
                    pageNumber,
                    pageSize,
                    sort
                };
                request.ContentType = RestContentType.FormUrlEncoded;

                var response = await this.SendAsync <TPageResult>(context, request);

                var page = response.Content;
                this.CheckResultForErrors(page);

                var recordsCount = page.RecordsCount;
                if (recordsCount <= 0)
                {
                    break;
                }
                count += recordsCount;

                pages.Add(page);

                total = Math.Max(total, page.TotalRecords);

                pageNumber++;
            }while (count < total);

            return(pages);
        }
        public async Task <ApplicationDetails> UpdateApplicationFeatureAsync <TFeatureValue>(
            ClientContext context, ApplicationDetails applicationDetails,
            Expression <Func <ApplicationFeatures, TFeatureValue> > feature, TFeatureValue value)
        {
            await Configure.AwaitFalse();

            var member = feature.Body as MemberExpression;

            if (member == null)
            {
                throw new ArgumentException($"Expression '{feature}' does not refer to a property.", nameof(feature));
            }

            var property = member.Member as PropertyInfo;

            if (property == null)
            {
                throw new ArgumentException($"Expression '{feature}' does not refer to a property.", nameof(feature));
            }

            var originalValue = property.GetValue(applicationDetails.Features);

            if (value.Equals(originalValue))
            {
                return(applicationDetails);
            }

            var jsonProperty = property.GetCustomAttribute <JsonPropertyAttribute>();
            var featureType  = jsonProperty?.PropertyName ?? _resolver.GetResolvedPropertyName(property.Name);
            var featureValue = typeof(TFeatureValue) == typeof(bool)
                ? ((bool)(object)value ? "yes" : "no")
                : value.ToString();

            var uriBuilder = new AppleDeveloperRequestUriBuilder(
                new RestUri(this.UrlProvider.UpdateApplicationUrl, new { platform = applicationDetails.Platform }));

            uriBuilder.AddQueryValues(new Dictionary <string, string> {
                { "teamId", applicationDetails.TeamId },
                { "displayId", applicationDetails.Id },
                { "featureType", featureType },
                { "featureValue", featureValue }
            });
            var request  = RestRequest.Post(uriBuilder.ToUri());
            var response = await this.SendAsync <Result <ApplicationDetails> >(context, request);

            this.CheckResultForErrors(response.Content);

            property.SetValue(applicationDetails.Features, value);

            return(applicationDetails);
        }
        public async Task <CertificateRequest> SubmitCertificateSigningRequestAsync(ClientContext context, string teamId, CertificateSigningRequest certSigningRequest, Platform platform = Platform.Ios)
        {
            await Configure.AwaitFalse();

            var uriBuilder = new AppleDeveloperRequestUriBuilder(new RestUri(this.UrlProvider.SubmitCertificateSigningRequestUrl, new { platform }));

            uriBuilder.AddQueryValues(new Dictionary <string, string> {
                { "teamId", teamId }
            });

            var request  = RestRequest.Post(uriBuilder.ToUri(), RestContentType.FormUrlEncoded, certSigningRequest);
            var response = await this.SendAsync <Result <CertificateRequest> >(context, request);

            this.CheckResultForErrors(response.Content);

            return(response.Content.Data);
        }
        private async Task <Result <List <Device> > > AddDevicesAsync(ClientContext context, string teamId, NewDevices newDevices, Platform platform)
        {
            await Configure.AwaitFalse();

            var uriBuilder = new AppleDeveloperRequestUriBuilder(new RestUri(this.UrlProvider.AddDevicesUrl, new { platform }));

            uriBuilder.AddQueryValues(new Dictionary <string, string> {
                { "teamId", teamId }
            });
            var request  = RestRequest.Post(uriBuilder.ToUri(), RestContentType.FormUrlEncoded, newDevices);
            var response = await this.SendAsync <Result <List <Device> > >(context, request);

            this.CheckResultForErrors(response.Content);

            context.DeleteValue <List <Device> >(teamId, platform);

            return(response.Content);
        }
        public async Task <bool> DisableDeviceAsync(ClientContext context, Device device)
        {
            await Configure.AwaitFalse();

            var uriBuilder = new AppleDeveloperRequestUriBuilder(new RestUri(this.UrlProvider.DeleteDeviceUrl, new {
                platform = device.Platform
            }));

            uriBuilder.AddQueryValues(new Dictionary <string, string> {
                { "teamId", device.TeamId },
                { "deviceId", device.DeviceId }
            });
            var request  = RestRequest.Post(uriBuilder.ToUri());
            var response = await this.SendAsync <Result <Device> >(context, request);

            this.CheckResultForErrors(response.Content);

            context.DeleteValue <List <Device> >(device.TeamId, device.Platform);

            return(true);
        }
        public async Task <CertificateRequest> RevokeCertificateRequestAsync(ClientContext context, CertificateRequest certificateRequest)
        {
            await Configure.AwaitFalse();

            var uriBuilder = new AppleDeveloperRequestUriBuilder(new RestUri(this.UrlProvider.DownloadCertificateUrl, new {
                platform = certificateRequest.Platform
            }));

            uriBuilder.AddQueryValues(new Dictionary <string, string> {
                { "teamId", certificateRequest.TeamId },
                { "certificateId", certificateRequest.CertificateId },
                { "type", certificateRequest.CertificateTypeDisplayId.ToStringValue() }
            });

            var request  = RestRequest.Post(uriBuilder.ToUri());
            var response = await this.SendAsync <Result <CertificateRequest> >(context, request);

            this.CheckResultForErrors(response.Content);

            return(response.Content.Data);
        }
        public async Task <byte[]> DownloadRawCertificateAsync(ClientContext context, CertificateRequest certificateRequest)
        {
            await Configure.AwaitFalse();

            var uriBuilder = new AppleDeveloperRequestUriBuilder(new RestUri(this.UrlProvider.DownloadCertificateUrl, new
            {
                platform = certificateRequest.Platform
            }));

            uriBuilder.AddQueryValues(new Dictionary <string, string> {
                { "teamId", certificateRequest.TeamId },
                { "certificateId", certificateRequest.CertificateId },
                { "type", certificateRequest.CertificateTypeDisplayId.ToStringValue() }
            });

            var request  = RestRequest.Get(uriBuilder.ToUri());
            var response = await this.SendAsync(context, request);

            var binaryContent = response.RawContent as RawBinaryContent;

            return(binaryContent?.Value);
        }
        public async Task <Device> ChangeDeviceNameAsync(ClientContext context, Device device, string newName)
        {
            await Configure.AwaitFalse();

            var uriBuilder = new AppleDeveloperRequestUriBuilder(new RestUri(this.UrlProvider.UpdateDeviceUrl, new {
                platform = device.Platform
            }));

            uriBuilder.AddQueryValues(new Dictionary <string, string> {
                { "teamId", device.TeamId },
            });
            var request = RestRequest.Post(uriBuilder.ToUri(), RestContentType.FormUrlEncoded, new UpdateDevice {
                DeviceId     = device.DeviceId,
                DeviceNumber = device.DeviceNumber,
                Name         = newName
            });
            var response = await this.SendAsync <Result <Device> >(context, request);

            this.CheckResultForErrors(response.Content);

            context.DeleteValue <List <Device> >(device.TeamId, device.Platform);

            return(response.Content.Data);
        }