示例#1
0
        static void Main(string[] args)
        {
            Console.WriteLine("NField API survey data downloader commandline tool v.0.9 By DDI (@piachu), Copyright 2020\r\n");
            Parser.Default.ParseArguments <Options>(args)
            .WithParsed <Options>(options =>
            {
                try
                {
                    if (File.Exists(options.FileName))
                    {
                        throw new IOException($"File already exists: {options.FileName}");
                    }

                    INfieldConnection connection = NfieldConnectionFactory.Create(new Uri("https://api.nfieldmr.com/v1/"));
                    connection.SignInAsync(options.Domain, options.User, options.Password).Wait();

                    INfieldSurveyDataService surveyDataService = connection.GetService <INfieldSurveyDataService>();

                    SurveyDownloadDataRequest myRequest = new SurveyDownloadDataRequest
                    {
                        DownloadSuccessfulLiveInterviewData    = options.SuccessfulLiveInterviewData,
                        DownloadNotSuccessfulLiveInterviewData = options.NotSuccessfulLiveInterviewData,
                        DownloadOpenAnswerData             = options.OpenAnswerData,
                        DownloadClosedAnswerData           = options.ClosedAnswerData,
                        DownloadSuspendedLiveInterviewData = options.SuspendedLiveInterviewData,
                        DownloadCapturedMedia     = options.CapturedMedia,
                        DownloadParaData          = options.ParaData,
                        DownloadTestInterviewData = options.TestInterviewDataData,
                        DownloadFileName          = Path.GetFileNameWithoutExtension(options.FileName),
                        // UTC time start of today
                        //StartDate = DateTime.ParseExact("2019-09-01T00:00:00Z", "yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture),
                        //EndDate = DateTime.ParseExact("2019-12-31T23:59:59Z", "yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture),
                        //SurveyId = "da96674b-dd83-4656-8305-11fc28f0b206"
                        StartDate = options.StartDate,
                        EndDate   = options.EndDate,
                        SurveyId  = options.SurveyId
                    };

                    var task = surveyDataService.PostAsync(myRequest).Result;

                    // request the background tasks service
                    var backgroundTasksService = connection.GetService <INfieldBackgroundTasksService>();

                    // Example of performing operations on background tasks.
                    var backgroundTaskQuery = backgroundTasksService.QueryAsync().Result.Where(s => s.Id == task.Id);
                    var mybackgroundTask    = backgroundTaskQuery.FirstOrDefault();

                    var status = mybackgroundTask.Status;
                    while ((status == BackgroundTaskStatus.Running) || (status == BackgroundTaskStatus.Created))
                    {
                        Console.WriteLine($"Status: {status.ToString()}");
                        Console.WriteLine("Waiting 5s...");
                        Task.Delay(5000).Wait();
                        backgroundTaskQuery = backgroundTasksService.QueryAsync().Result.Where(s => s.Id == task.Id);
                        mybackgroundTask    = backgroundTaskQuery.FirstOrDefault();

                        status = mybackgroundTask.Status;
                    }
                    if (mybackgroundTask.Status == BackgroundTaskStatus.SuccessfullyCompleted)
                    {
                        Console.WriteLine($"Status: {status.ToString()}");
                        Console.WriteLine($"Downloading file: {mybackgroundTask.ResultUrl}");
                        Console.WriteLine($"Downloading to: {myRequest.DownloadFileName}.zip");

                        DownloadDataFileAsync(mybackgroundTask.ResultUrl, $"{options.FileName}").Wait();
                    }
                    else
                    {
                        // Canceled = 2 or Faulted = 3
                        if (mybackgroundTask.Status == BackgroundTaskStatus.Faulted)
                        {
                            Console.WriteLine($"Faiure! Status: {status.ToString()}");
                            Environment.Exit(-254);
                        }
                        else
                        {
                            Console.WriteLine($"Download cancelled. Status: {status.ToString()}");
                            Environment.Exit(-1);
                        }
                    }
                    Console.WriteLine("Finished succesfuly");
                }
                catch (Exception e)
                {
                    Console.WriteLine("Exception occured");
                    Console.WriteLine(e.Message);
                }
            });
        }
示例#2
0
        static void Main()
        {
            // Example of using the Nfield SDK with a user defined IoC container.
            // In most cases the IoC container is created and managed through the application.
            using (IKernel kernel = new StandardKernel())
            {
                InitializeNfield(kernel);

                const string serverUrl = "http://*****:*****@"*QUESTION 1 *CODES 61L1
                               Do you watch TV?
                                1:Yes
                                2:No
                                *END"
                };
                var updatedScript = surveyScriptService.PostAsync("surveyWithOdinScriptId", myScript).Result;
                // Upload survey script with file path
                var scriptFilePath  = @"C:\SimpleQ.odin";
                var myUpdatedScript =
                    surveyScriptService.PostAsync("surveyWithOdinScriptId", scriptFilePath).Result;

                // Create survey
                var newSurvey = surveysService.AddAsync(new Survey(SurveyType.Basic)
                {
                    ClientName  = "clientName",
                    Description = "description",
                    SurveyName  = "abc"
                }).Result;

                surveyScriptService.PostAsync(newSurvey.SurveyId, myScript).Wait();

                var surveyFieldworkService = connection.GetService <INfieldSurveyFieldworkService>();
                //Get survey fieldwork status
                var surveyFieldworkStatus = surveyFieldworkService.GetStatusAsync(newSurvey.SurveyId).Result; //Should be under construction

                // Start the fieldwork for the survey
                surveyFieldworkService.StartFieldworkAsync(newSurvey.SurveyId).Wait();
                surveyFieldworkStatus = surveyFieldworkService.GetStatusAsync(newSurvey.SurveyId).Result; //Should be started

                // Example of a download data request: filtering testdata collected today
                var surveyDataService = connection.GetService <INfieldSurveyDataService>();

                var myRequest = new SurveyDownloadDataRequest
                {
                    DownloadSuccessfulLiveInterviewData    = false,
                    DownloadNotSuccessfulLiveInterviewData = false,
                    DownloadOpenAnswerData             = true,
                    DownloadClosedAnswerData           = true,
                    DownloadSuspendedLiveInterviewData = false,
                    DownloadCapturedMedia     = false,
                    DownloadParaData          = false,
                    DownloadTestInterviewData = true,
                    DownloadFileName          = "MyFileName",
                    StartDate = DateTime.Today.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture),            // UTC time start of today
                    EndDate   = DateTime.Today.AddDays(1).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture), // UTC time end of today
                    SurveyId  = "SomeSurveyId"
                };

                var task = surveyDataService.PostAsync(myRequest).Result;

                // request the background tasks service
                var backgroundTasksService = connection.GetService <INfieldBackgroundTasksService>();

                // Example of performing operations on background tasks.
                var backgroundTaskQuery = backgroundTasksService.QueryAsync().Result.Where(s => s.Id == task.Id);
                var mybackgroundTask    = backgroundTaskQuery.FirstOrDefault();

                if (mybackgroundTask != null)
                {
                    var status = mybackgroundTask.Status;
                }

                // Example of creating a new translation
                const string surveyName      = "Language";
                const string languageName    = "Dutch";
                const string translationName = "ButtonNext";
                const string translationText = "Volgende";
                // First create the survey if it does not exist
                var languageSurvey = surveysService.QueryAsync().Result
                                     .SingleOrDefault(s => s.SurveyName == surveyName);
                if (languageSurvey == null)
                {
                    languageSurvey = surveysService.AddAsync(
                        new Survey(SurveyType.Basic)
                    {
                        SurveyName = surveyName
                    }).Result;
                }
                // Then find the language we wish to change a translation for
                var languageService = connection.GetService <INfieldLanguagesService>();
                var language        = languageService.QueryAsync(languageSurvey.SurveyId)
                                      .Result.SingleOrDefault(l => l.Name == languageName);
                if (language == null)
                {
                    language = languageService.AddAsync(languageSurvey.SurveyId,
                                                        new Language {
                        Name = languageName
                    }).Result;
                }
                // Now add or update a translation
                var translationsService = connection.GetService <INfieldTranslationsService>();
                var translation         = translationsService.QueryAsync(languageSurvey.SurveyId,
                                                                         language.Id).Result.SingleOrDefault(t => t.Name == translationName);
                if (translation == null)
                {
                    translationsService.AddAsync(languageSurvey.SurveyId,
                                                 language.Id, new Translation
                    {
                        Name = translationName,
                        Text = translationText
                    }).Wait();
                }
                else
                {
                    translation.Text = translationText;
                    translationsService.UpdateAsync(languageSurvey.SurveyId,
                                                    language.Id, translation).Wait();
                }
            }
        }