public static void UploadApiReferences(this ICakeContext context, RestApiCredentials credentials, JavaOptions options)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var request = new JavaRequest(credentials, options);

            context.Log.Write(Verbosity.Normal, LogLevel.Information, "Uploading API Refs for {0}-{1} at {2}",
                              request.ProductKey, request.SectionKey, credentials.Uri);

            var multipartContent = new MultipartFormDataContent();
            var json             = JsonConvert.SerializeObject(request,
                                                               new JsonSerializerSettings {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });

            multipartContent.Add(new StringContent(json, Encoding.UTF8, "application/json"), request.ContentName);

            var content = new ByteArrayContent(request.JarFile.FileData);

            content.Headers.ContentType = new MediaTypeHeaderValue(request.JarFile.FileMimeType);
            multipartContent.Add(content, "jarFile", request.JarFile.FileName);

            PostContent(context, credentials, multipartContent);
        }
 internal DotNetHtmlRequest(RestApiCredentials credentials, DotNetHtmlOptions options) : base(credentials, options)
 {
     ZipFile = new ApiFileInfo
     {
         FileName     = Path.GetFileName(options.ZipFilePath),
         FileData     = File.ReadAllBytes(options.ZipFilePath),
         FileMimeType = MimeMapping.MimeUtility.GetMimeMapping(options.ZipFilePath)
     };
 }
        internal BaseRequest(RestApiCredentials credentials, BaseOptions options)
        {
            Validate(credentials);

            Validate(options);

            UserName   = credentials.UserName;
            Password   = credentials.Password;
            ProductKey = options.ProductKey;
        }
        internal JavaRequest(RestApiCredentials credentials, JavaOptions options) : base(credentials, options)
        {
            Version = options.Version;

            JarFile = new ApiFileInfo
            {
                FileName     = Path.GetFileName(options.JarFilePath),
                FileData     = File.ReadAllBytes(options.JarFilePath),
                FileMimeType = MimeMapping.MimeUtility.GetMimeMapping(options.JarFilePath)
            };
        }
Ejemplo n.º 5
0
        internal DotNetRequest(RestApiCredentials credentials, DotNetOptions options) : base(credentials, options)
        {
            DllFile = new ApiFileInfo
            {
                FileName     = Path.GetFileName(options.DllFilePath),
                FileData     = File.ReadAllBytes(options.DllFilePath),
                FileMimeType = MimeMapping.GetMimeMapping(options.DllFilePath)
            };

            XmlFile = new ApiFileInfo
            {
                FileName     = Path.GetFileName(options.XmlFilePath),
                FileData     = File.ReadAllBytes(options.XmlFilePath),
                FileMimeType = MimeMapping.GetMimeMapping(options.XmlFilePath)
            };
        }
        private void Validate(RestApiCredentials credentials)
        {
            if (string.IsNullOrEmpty(credentials.UserName))
            {
                throw new ArgumentNullException(nameof(credentials.UserName));
            }

            if (string.IsNullOrEmpty(credentials.Password))
            {
                throw new ArgumentNullException(nameof(credentials.Password));
            }

            if (string.IsNullOrEmpty(credentials.Uri))
            {
                throw new ArgumentNullException(nameof(credentials.Uri));
            }
        }
        private static void PostContent(ICakeContext context, RestApiCredentials credentials,
                                        MultipartFormDataContent multipartContent)
        {
            var client = new HttpClient {
                Timeout = TimeSpan.FromSeconds(2400)
            };

            HttpResponseMessage response = client.PostAsync(credentials.Uri, multipartContent).Result;

            if (response.StatusCode != HttpStatusCode.OK)
            {
                string errorMsg = response.Content.ReadAsStringAsync().Result;
                context.Log.Write(Verbosity.Normal, LogLevel.Error,
                                  "Uploading API references completed with error: '{0}'",
                                  errorMsg);

                throw new Exception(errorMsg);
            }

            context.Log.Write(Verbosity.Normal, LogLevel.Information, "SUCCESS! API references has been uploaded.");
        }