public async Task <List <DomainFace.DetectResult> > DetectAsync(string url, string returnFaceAttributes, bool returnFaceId = false, bool returnFaceLandmarks = false) { dynamic body = new JObject(); body.url = url; StringContent queryString = new StringContent(body.ToString(), System.Text.Encoding.UTF8, "application/json"); using (var client = new HttpClient()) { client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", APIReference.FaceAPIKey); var response = await client.PostAsync($"https://{APIReference.FaceAPIZone}.api.cognitive.microsoft.com/face/v1.0/detect?returnFaceId={returnFaceId}&returnFaceLandmarks={returnFaceLandmarks}&returnFaceAttributes={returnFaceAttributes}", queryString); List <DomainFace.DetectResult> result = null; if (response.IsSuccessStatusCode) { var json = await response.Content.ReadAsStringAsync(); result = JsonConvert.DeserializeObject <List <DomainFace.DetectResult> >(json); } else { var json = await response.Content.ReadAsStringAsync(); NotSuccessfulResponse fex = JsonConvert.DeserializeObject <NotSuccessfulResponse>(json); throw new Exception($"{fex.error.code} - {fex.error.message}"); } return(result); } }
public async Task <bool> UpdateAsync(string largePersonGroupId, string name, string userData) { dynamic body = new JObject(); body.name = name; body.userData = userData; StringContent queryString = new StringContent(body.ToString(), System.Text.Encoding.UTF8, "application/json"); using (var client = new HttpClient()) { client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", APIReference.FaceAPIKey); var response = await client.PatchAsync($"https://{APIReference.FaceAPIZone}.api.cognitive.microsoft.com/face/v1.0/largepersongroups/{largePersonGroupId}", queryString); bool result = false; if (response.IsSuccessStatusCode) { result = true; } else { var json = await response.Content.ReadAsStringAsync(); NotSuccessfulResponse fex = JsonConvert.DeserializeObject <NotSuccessfulResponse>(json); throw new Exception($"{fex.error.code} - {fex.error.message}"); } return(result); } }
public async Task <DomainLargeFaceList.GetTrainingStatusResult> GetTrainingStatusAsync(string largeFaceListId) { using (var client = new HttpClient()) { client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", APIReference.FaceAPIKey); var response = await client.GetAsync($"https://{APIReference.FaceAPIZone}.api.cognitive.microsoft.com/face/v1.0/largefacelists/{largeFaceListId}/training"); DomainLargeFaceList.GetTrainingStatusResult result = null; if (response.IsSuccessStatusCode) { var json = await response.Content.ReadAsStringAsync(); result = JsonConvert.DeserializeObject <DomainLargeFaceList.GetTrainingStatusResult>(json); } else { var json = await response.Content.ReadAsStringAsync(); NotSuccessfulResponse fex = JsonConvert.DeserializeObject <NotSuccessfulResponse>(json); throw new Exception($"{fex.error.code} - {fex.error.message}"); } return(result); } }
public async Task <List <DomainLargePersonGroup.ListResult> > ListAsync(string start, int top) { using (var client = new HttpClient()) { client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", APIReference.FaceAPIKey); var response = await client.GetAsync($"https://{APIReference.FaceAPIZone}.api.cognitive.microsoft.com/face/v1.0/largepersongroups?start={start}&top={top}"); List <DomainLargePersonGroup.ListResult> result = null; if (response.IsSuccessStatusCode) { var json = await response.Content.ReadAsStringAsync(); result = JsonConvert.DeserializeObject <List <DomainLargePersonGroup.ListResult> >(json); } else { var json = await response.Content.ReadAsStringAsync(); NotSuccessfulResponse fex = JsonConvert.DeserializeObject <NotSuccessfulResponse>(json); throw new Exception($"{fex.error.code} - {fex.error.message}"); } return(result); } }
public async Task <DomainLargeFaceList.AddFaceResult> AddFaceAsync(string largeFaceListId, string url, string userData, string targetFace) { dynamic body = new JObject(); body.url = url; StringContent queryString = new StringContent(body.ToString(), System.Text.Encoding.UTF8, "application/json"); using (var client = new HttpClient()) { client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", APIReference.FaceAPIKey); var response = await client.PostAsync($"https://{APIReference.FaceAPIZone}.api.cognitive.microsoft.com/face/v1.0/largefacelists/{largeFaceListId}/persistedFaces?userData={userData}&targetFace={targetFace}", queryString); DomainLargeFaceList.AddFaceResult result = null; if (response.IsSuccessStatusCode) { var json = await response.Content.ReadAsStringAsync(); result = JsonConvert.DeserializeObject <DomainLargeFaceList.AddFaceResult>(json); } else { var json = await response.Content.ReadAsStringAsync(); NotSuccessfulResponse fex = JsonConvert.DeserializeObject <NotSuccessfulResponse>(json); throw new Exception($"{fex.error.code} - {fex.error.message}"); } return(result); } }
public void ReadNotSuccessfulResponseWithStatusCodeTest() { dynamic jNotSuccessfulResponse = new JObject(); dynamic jNotSuccessfulResponseError = new JObject(); jNotSuccessfulResponseError.statusCode = 403; jNotSuccessfulResponseError.message = "Out of call volume quota. Quota will be replenished in 2 days."; jNotSuccessfulResponse.error = jNotSuccessfulResponseError; var rException = JsonConvert.SerializeObject(jNotSuccessfulResponse); NotSuccessfulResponse exception = JsonConvert.DeserializeObject<NotSuccessfulResponse>(rException); Assert.True(exception.error.code == 403.ToString()); }
public void ReadNotSuccessfulResponseWithCodeTest() { dynamic jNotSuccessfulResponse = new JObject(); dynamic jNotSuccessfulResponseError = new JObject(); jNotSuccessfulResponseError.code = "Unspecified"; jNotSuccessfulResponseError.message = "Access denied due to invalid subscription key. Make sure you are subscribed to an API you are trying to call and provide the right key."; jNotSuccessfulResponse.error = jNotSuccessfulResponseError; var rException = JsonConvert.SerializeObject(jNotSuccessfulResponse); NotSuccessfulResponse exception = JsonConvert.DeserializeObject<NotSuccessfulResponse>(rException); Assert.True(exception.error.code == "Unspecified"); }
public async Task <List <DomainFace.FindSimilarResult> > FindSimilarAsync(string faceId, string faceListId, string largeFaceListId, string[] faceIds, int maxNumOfCandidatesReturned, string mode) { dynamic body = new JObject(); body.faceId = faceId; if (faceListId != string.Empty) { body.faceListId = faceListId; } if (largeFaceListId != string.Empty) { body.largeFaceListId = largeFaceListId; } if (faceIds.Length > 0) { body.faceIds = JArray.FromObject(faceIds); } body.maxNumOfCandidatesReturned = maxNumOfCandidatesReturned; body.mode = mode; StringContent queryString = new StringContent(body.ToString(), System.Text.Encoding.UTF8, "application/json"); using (var client = new HttpClient()) { client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", APIReference.FaceAPIKey); var response = await client.PostAsync($"https://{APIReference.FaceAPIZone}.api.cognitive.microsoft.com/face/v1.0/findsimilars", queryString); List <DomainFace.FindSimilarResult> result = null; if (response.IsSuccessStatusCode) { var json = await response.Content.ReadAsStringAsync(); result = JsonConvert.DeserializeObject <List <DomainFace.FindSimilarResult> >(json); } else { var json = await response.Content.ReadAsStringAsync(); NotSuccessfulResponse fex = JsonConvert.DeserializeObject <NotSuccessfulResponse>(json); throw new Exception($"{fex.error.code} - {fex.error.message}"); } return(result); } }
public async Task <bool> DeleteAsync(string largePersonGroupId) { using (var client = new HttpClient()) { client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", APIReference.FaceAPIKey); var response = await client.DeleteAsync($"https://{APIReference.FaceAPIZone}.api.cognitive.microsoft.com/face/v1.0/largepersongroups/{largePersonGroupId}"); bool result = false; if (response.IsSuccessStatusCode) { result = true; } else { var json = await response.Content.ReadAsStringAsync(); NotSuccessfulResponse fex = JsonConvert.DeserializeObject <NotSuccessfulResponse>(json); throw new Exception($"{fex.error.code} - {fex.error.message}"); } return(result); } }