internal static SvcStatus LaunchJob(int jobId, int unitCount) { SvcStatus status; if (jobId > 0) { using (HttpClient client = new HttpClient()) { CFJobRequest job = new CFJobRequest(); var incExcCountStatus = job.ControlQuality(jobId); if (incExcCountStatus.Result.Response.IsSuccessStatusCode) { var baseAddress = new Uri(string.Format("{0}jobs/{1}/orders.json?key={2}", CrowdflowerBaseUri, jobId, ConfidentialData.CrowdFlowerKey)); var response = client.PostAsync(baseAddress, job.LaunchRequestCUrlData(CFJobRequest.CFWorkForce.OnDemand, unitCount)).Result; if (response.IsSuccessStatusCode) { status = new SvcStatus() { Level = 0, Description = "Job launched", Response = response }; } else { status = new SvcStatus() { Level = 2, Description = "Failed to launch", Response = response }; } } else { return incExcCountStatus.Result; } } } else { status = new SvcStatus() { Level = 2, Description = "Job Key must be greater than 0" }; } return status; }
internal async Task<SvcStatus> CreateAudioJob(ParticipantResult result, ParticipantActivity activity) { SvcStatus status = new SvcStatus(); try { string userKey = result.User.Email; using (HttpClient client = new HttpClient()) { FormUrlEncodedContent reqContent = CreateRequestCUrlData(result.User.App); Uri baseAddress = new Uri(CrowdflowerBaseUri + "jobs.json?key=" + CrowdflowerKey); HttpResponseMessage response = client.PostAsync(baseAddress, reqContent).Result; if (response.IsSuccessStatusCode) { CFJobResponse jobRes = await response.Content.ReadAsAsync<CFJobResponse>(); List<string> audioFiles = await SvcUtil.DownloadAndExtractZip(ResourceUrl, jobRes.id.ToString(), userKey, activity.Id.ToString()); if (!audioFiles.Any()) return status; CFUnitRequest unitsReq = new CFUnitRequest(); int count = 0; string uploadUnitsJson = AudioUnit.CreateCFData(audioFiles, activity, result, out count); Rows = await unitsReq.SendCfUnits(jobRes.id, uploadUnitsJson); foreach (CrowdRowResponse unit in Rows) { unit.ParticipantResultId = result.Id; } status = new SvcStatus() { Level = 0, Count = count, Description = "Job created", Response = response, CreatedRows = Rows }; } else { status = new SvcStatus() { Level = 2, Description = "Failed to create the job", Response = response }; } } } catch (Exception e) { status = new SvcStatus() { Level = 2, Description = e.Message, Response = new HttpResponseMessage(HttpStatusCode.InternalServerError) }; } return status; }
internal async Task<SvcStatus> ControlQuality(int jobId) { using (HttpClient client = new HttpClient()) { string includedCountries = "GB"; List<KeyValuePair<string, string>> lstKeyValue = new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>("job[included_countries][]", includedCountries) }; FormUrlEncodedContent reqContent = new FormUrlEncodedContent(lstKeyValue); Uri baseAddress = new Uri(String.Format("{0}jobs/{1}.json?key={2}", CrowdflowerBaseUri, jobId, CrowdflowerKey)); HttpResponseMessage response = client.PutAsync(baseAddress, reqContent).Result; SvcStatus status = new SvcStatus(); if (response.IsSuccessStatusCode) { status = new SvcStatus() { Level = 0, Description = "Included countries: " + includedCountries, Response = response }; } else { status = new SvcStatus() { Level = 2, Description = "Failed to include/exclude countries", Response = response }; } return status; } }