예제 #1
0
        static string GetToken(string email, string password)
        {
            string apiUrl = config["apiUrl"];
            var    req    = new WebRequestOptions {
                UriString = $"{apiUrl}{Urls.login}"
            };
            var resp = req.Post(new HttpPostPayload
            {
                ContentType = "application/json",
                Data        = new Authorize
                {
                    Email    = email,
                    Password = password
                }.ToJson()
            }).FromJson <Credential>();

            return(resp.Token);
        }
예제 #2
0
파일: Program.cs 프로젝트: tmcmil/cset
        static void Import(string token, string importdir, List <KeyValuePair <string, byte[]> > files)
        {
            string apiUrl = config["apiUrl"];

            if (Directory.Exists(importdir))
            {
                files.Clear();
                foreach (var filepath in Directory.GetFiles(importdir, "*.csetw", SearchOption.AllDirectories))
                {
                    var buffer = File.ReadAllBytes(filepath);
                    files.Add(new KeyValuePair <string, byte[]>(Path.GetFileName(filepath), buffer));
                }
            }

            var req = new WebRequestOptions
            {
                UriString = $"{apiUrl}{Urls.import}",
                Headers   = new Dictionary <string, string> {
                    { "Authorization", token }
                }
            };

            foreach (var file in files)
            {
                var filename = file.Key;
                var buffer   = file.Value;
                using (var bs = new BlobStream(filename, "application/octet-stream"))
                {
                    bs.Write(buffer, 0, buffer.Length);
                    var content = new Dictionary <string, object> {
                        { "file", bs }
                    };
                    using (var data = MimeMultipartStream.FromContent(content))
                    {
                        try
                        {
                            var resp = req.Post(new HttpPostPayload
                            {
                                ContentType = data.ContentType,
                                Data        = data.Buffer
                            });
                            var log = $"{filename} | {resp}";
                            Console.WriteLine(log);
                            Log.Information(log);
                        }
                        catch (WebException ex)
                        {
                            using (var stream = ex.Response.GetResponseStream())
                            {
                                using (var reader = new StreamReader(stream))
                                {
                                    var json = reader.ReadToEnd();
                                    var err  = JObject.Parse(json);
                                    var log  = $"{filename} | \"Failed to import assessment\"";
                                    Console.WriteLine(log);
                                    Log.Error(ex, log);
                                    Log.Error("Server-Message: {0}", err.Value <string>("Message"));
                                    Log.Error("Server-ExceptionType: {0}", err.Value <string>("ExceptionType"));
                                    Log.Error("Server-ExceptionMessage: {0}", err.Value <string>("ExceptionMessage"));
                                    Log.Error("Server-StackTrace: {0}", err.Value <string>("StackTrace"));
                                }
                            }
                        }
                    }
                }
            }
        }