|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Globalization; |
| 4 | +using System.Net; |
| 5 | +using System.Net.Http; |
| 6 | +using System.Net.Http.Formatting; |
| 7 | +using System.Net.Http.Headers; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using AzureMLClient.Contracts; |
| 10 | + |
| 11 | +namespace AzureMLClient |
| 12 | +{ |
| 13 | + public static class ClientExtensions |
| 14 | + { |
| 15 | + private const string BatchUriFormat = "{0}/jobs/{1}?api-version=2.0"; |
| 16 | + private const string EndpointsUriFormat = "workspaces/{0}/webservices/{1}/endpoints"; |
| 17 | + private const string UpdateResourceUriFormat = "https://management.azureml.net/workspaces/{0}/webservices/{1}/endpoints/{2}"; |
| 18 | + |
| 19 | + public static async Task<IEnumerable<Endpoint>> GetEndpointsAsync(this WebService webServiece) |
| 20 | + { |
| 21 | + string uri = string.Format(CultureInfo.InvariantCulture, EndpointsUriFormat, webServiece.WorkspaceId, webServiece.Id) + "?decryptSecrets=true"; |
| 22 | + using (var httpClient = new HttpClient()) |
| 23 | + { |
| 24 | + httpClient.DefaultRequestHeaders.Authorization = GetAuthorizationHeaderAsync(Configurations.WorkspaceAuthToken); |
| 25 | + var response = await httpClient.GetAsync(new Uri(Configurations.BaseUri, uri)).ConfigureAwait(false); |
| 26 | + return await response.Content.ReadAsAsync<IEnumerable<Endpoint>>().ConfigureAwait(false); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + |
| 31 | + public static async Task<BatchStatus> GetBatchStatus(this Endpoint endpoint, string jobId) |
| 32 | + { |
| 33 | + using (var httpClient = new HttpClient()) |
| 34 | + { |
| 35 | + var uri = string.Format(CultureInfo.InvariantCulture, BatchUriFormat, endpoint.ApiLocation, jobId); |
| 36 | + httpClient.DefaultRequestHeaders.Authorization = GetAuthorizationHeaderAsync(endpoint.PrimaryKey); |
| 37 | + var response = await httpClient.GetAsync(new Uri(uri)).ConfigureAwait(false); |
| 38 | + return await response.Content.ReadAsAsync<BatchStatus>().ConfigureAwait(false); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + public static async Task<string> SubmitBatch(this Endpoint endpoint, BatchRequest batchRequest) |
| 43 | + { |
| 44 | + using (var httpClient = new HttpClient()) |
| 45 | + { |
| 46 | + string uri = endpoint.ApiLocation + "/jobs"; |
| 47 | + httpClient.DefaultRequestHeaders.Authorization = GetAuthorizationHeaderAsync(endpoint.PrimaryKey); |
| 48 | + var response = await httpClient.PostAsync(new Uri(uri), new ObjectContent(typeof(BatchRequest), batchRequest, new JsonMediaTypeFormatter())).ConfigureAwait(false); |
| 49 | + return await response.Content.ReadAsAsync<string>().ConfigureAwait(false); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + public static async Task<bool> UpdateResource(this Endpoint endpoint, ResourceLocations resources) |
| 54 | + { |
| 55 | + using (var httpClient = new HttpClient()) |
| 56 | + { |
| 57 | + var uri = string.Format(CultureInfo.InvariantCulture, UpdateResourceUriFormat, endpoint.WorkspaceId, endpoint.WebServiceId, endpoint.Name); |
| 58 | + httpClient.DefaultRequestHeaders.Authorization = GetAuthorizationHeaderAsync(endpoint.PrimaryKey); |
| 59 | + httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); |
| 60 | + var request = new HttpRequestMessage(new HttpMethod("PATCH"), uri); |
| 61 | + request.Content = new ObjectContent(typeof(ResourceLocations), resources, new JsonMediaTypeFormatter()); |
| 62 | + var response = await httpClient.SendAsync(request).ConfigureAwait(false); |
| 63 | + if (response.IsSuccessStatusCode) |
| 64 | + { |
| 65 | + return true; |
| 66 | + } |
| 67 | + return false; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public static async Task<Endpoint> CreateEndpoint(this WebService webservice, string name, EndpointCreate create, string workspaceAuthToken) |
| 72 | + { |
| 73 | + using (var httpClient = new HttpClient()) |
| 74 | + { |
| 75 | + var uri = string.Format(CultureInfo.InvariantCulture, UpdateResourceUriFormat, webservice.WorkspaceId, webservice.Id, name); |
| 76 | + httpClient.DefaultRequestHeaders.Authorization = GetAuthorizationHeaderAsync(workspaceAuthToken); |
| 77 | + httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); |
| 78 | + var request = new HttpRequestMessage(new HttpMethod("PUT"), uri); |
| 79 | + request.Content = new ObjectContent(typeof(EndpointCreate), create, new JsonMediaTypeFormatter()); |
| 80 | + var response = await httpClient.SendAsync(request).ConfigureAwait(false); |
| 81 | + if (response.IsSuccessStatusCode) |
| 82 | + { |
| 83 | + return await response.Content.ReadAsAsync<Endpoint>().ConfigureAwait(false); |
| 84 | + } |
| 85 | + return null; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + public static bool Completed(this BatchStatus status) |
| 90 | + { |
| 91 | + return status.StatusCode == BatchScoreStatusCode.Cancelled || |
| 92 | + status.StatusCode == BatchScoreStatusCode.Failed || |
| 93 | + status.StatusCode == BatchScoreStatusCode.Finished; |
| 94 | + } |
| 95 | + |
| 96 | + public static AuthenticationHeaderValue GetAuthorizationHeaderAsync(string key) |
| 97 | + { |
| 98 | + return new AuthenticationHeaderValue("Bearer", key); |
| 99 | + } |
| 100 | + |
| 101 | + public static string ToUri(this AzureBlobDataReference blob) |
| 102 | + { |
| 103 | + return blob.BaseLocation + blob.RelativeLocation + blob.SasBlobToken; |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments