public static async Task <Dictionary <string, dynamic> > RefreshToken(string clientid) { Dictionary <string, dynamic> result = new Dictionary <string, dynamic>(); try { HttpClient client = await ArchesHttpClient.GetHttpClient(); var serializer = new JavaScriptSerializer(); var stringContent = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("refresh_token", StaticVariables.archesToken["refresh_token"]), new KeyValuePair <string, string>("client_id", clientid), new KeyValuePair <string, string>("grant_type", "refresh_token"), }); var response = await client.PostAsync(System.IO.Path.Combine(StaticVariables.archesInstanceURL, "o/token/"), stringContent); response.EnsureSuccessStatusCode(); string responseBody = await response.Content.ReadAsStringAsync(); dynamic responseJSON = serializer.Deserialize <dynamic>(@responseBody); dynamic results = responseJSON; result.Add("access_token", results["access_token"]); result.Add("refresh_token", results["refresh_token"]); result.Add("expires_in", results["expires_in"]); result.Add("token_type", results["token_type"]); result.Add("scope", results["scope"]); result.Add("timestamp", DateTime.Now); } catch (Exception ex) { throw new System.ArgumentException($"Failed to refresh the Token: {ex.Message}", ex); } return(result); }
private async Task <string> GetClientId() { string clientid = ""; try { HttpClient client = await ArchesHttpClient.GetHttpClient(); var serializer = new JavaScriptSerializer(); var stringContent = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("username", Username.Text), new KeyValuePair <string, string>("password", Password.Password), }); var response = await client.PostAsync(System.IO.Path.Combine(StaticVariables.archesInstanceURL, "auth/get_client_id"), stringContent); response.EnsureSuccessStatusCode(); string responseBody = await response.Content.ReadAsStringAsync(); dynamic responseJSON = serializer.Deserialize <dynamic>(@responseBody); dynamic results = responseJSON; clientid = results["clientid"]; } catch (Exception ex) { throw new System.ArgumentException("Failed to get a client ID", ex); } return(clientid); }
private async Task GetInstances() { try { HttpClient client = await ArchesHttpClient.GetHttpClient(); HttpResponseMessage response = await client.GetAsync(System.IO.Path.Combine(StaticVariables.archesInstanceURL, "search/resources")); response.EnsureSuccessStatusCode(); string responseBody = await response.Content.ReadAsStringAsync(); var serializer = new JavaScriptSerializer(); dynamic responseJSON = serializer.Deserialize <dynamic>(@responseBody); JavaScriptSerializer jsonSerializer = new JavaScriptSerializer(); dynamic results = responseJSON["results"]["hits"]["hits"]; int count = 0; string names = ""; foreach (dynamic element in results) { count++; string displayname = element["_source"]["displayname"]; names += $"{count}. {displayname} \n"; } ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show($"{count} Instances:\n{names}"); } catch (Exception ex) { throw new System.ArgumentException("Address is wrong", ex); } }
public static async Task <List <GeometryNode> > GetGeometryNode() { List <GeometryNode> nodeidResponse = new List <GeometryNode>(); try { HttpClient client = await ArchesHttpClient.GetHttpClient(); if ((DateTime.Now - StaticVariables.archesToken["timestamp"]).TotalSeconds > (StaticVariables.archesToken["expires_in"] - 300)) { StaticVariables.archesToken = await MainDockpaneView.RefreshToken(StaticVariables.myClientid); } client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", StaticVariables.archesToken["access_token"]); HttpResponseMessage response = await client.GetAsync(System.IO.Path.Combine(StaticVariables.archesInstanceURL, "api/nodes/?datatype=geojson-feature-collection&perms=write_nodegroup")); response.EnsureSuccessStatusCode(); string responseBody = await response.Content.ReadAsStringAsync(); var serializer = new JavaScriptSerializer(); dynamic results = serializer.Deserialize <dynamic>(@responseBody); foreach (dynamic element in results) { nodeidResponse.Add(new GeometryNode(element["resourcemodelname"], element["name"], element["nodeid"])); } } catch (HttpRequestException e) { System.ArgumentException argEx = new System.ArgumentException("The nodeid cannot be retrieved from the Arches server", e); throw argEx; } return(nodeidResponse); }
private async Task <Dictionary <String, Boolean> > CheckInstancePermission(string resourceInstanceID) { Dictionary <String, Boolean> result = new Dictionary <String, Boolean>(); try { HttpClient client = await ArchesHttpClient.GetHttpClient(); if ((DateTime.Now - StaticVariables.archesToken["timestamp"]).TotalSeconds > (StaticVariables.archesToken["expires_in"] - 300)) { StaticVariables.archesToken = await MainDockpaneView.RefreshToken(StaticVariables.myClientid); } client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", StaticVariables.archesToken["access_token"]); var response = await client.GetAsync(System.IO.Path.Combine(StaticVariables.archesInstanceURL, string.Format("api/instance_permissions/?resourceinstanceid={0}", resourceInstanceID))); response.EnsureSuccessStatusCode(); string responseBody = await response.Content.ReadAsStringAsync(); var serializer = new JavaScriptSerializer(); dynamic results = serializer.Deserialize <dynamic>(@responseBody); result.Add("read", results["read"]); result.Add("edit", results["edit"]); result.Add("delete", results["delete"]); } catch (HttpRequestException ex) { throw new System.ArgumentException(ex.Message, ex); } return(result); }
public static async Task <Dictionary <string, string> > SubmitToArches(string tileid, string nodeid, string esrijson, string geometryFormat, string submitOperation) { Dictionary <String, String> result = new Dictionary <String, String>(); try { HttpClient client = await ArchesHttpClient.GetHttpClient(); if ((DateTime.Now - StaticVariables.archesToken["timestamp"]).TotalSeconds > (StaticVariables.archesToken["expires_in"] - 300)) { StaticVariables.archesToken = await MainDockpaneView.RefreshToken(StaticVariables.myClientid); } var serializer = new JavaScriptSerializer(); var stringContent = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("tileid", tileid), new KeyValuePair <string, string>("nodeid", nodeid), new KeyValuePair <string, string>("data", esrijson), new KeyValuePair <string, string>("format", geometryFormat), new KeyValuePair <string, string>("operation", submitOperation), }); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", StaticVariables.archesToken["access_token"]); var response = await client.PostAsync(System.IO.Path.Combine(StaticVariables.archesInstanceURL, "api/node_value/"), stringContent); response.EnsureSuccessStatusCode(); string responseBody = await response.Content.ReadAsStringAsync(); dynamic responseJSON = serializer.Deserialize <dynamic>(@responseBody); if (responseJSON.ContainsKey("nodegroup_id")) { result.Add("nodegroup_id", responseJSON["nodegroup_id"]); } if (responseJSON.ContainsKey("resourceinstance_id")) { result.Add("resourceinstance_id", responseJSON["resourceinstance_id"]); } if (responseJSON.ContainsKey("tileid")) { result.Add("tileid", responseJSON["tileid"]); } } catch (HttpRequestException ex) { throw new System.ArgumentException(ex.Message, ex); } return(result); }
private async Task <Dictionary <string, string> > GetResource(string resourceid, string token) { StaticVariables.archesInstanceURL = InstanceURL.Text; Dictionary <String, String> result = new Dictionary <String, String>(); try { HttpClient client = await ArchesHttpClient.GetHttpClient(); var serializer = new JavaScriptSerializer(); string header = "Bearer " + token; try { client.DefaultRequestHeaders.Add("Authorization", header); } catch (System.FormatException e) { Console.WriteLine("Message :{0} ", e.Message); } var response = await client.GetAsync(StaticVariables.archesInstanceURL + $"resources/{resourceid}?format=json"); ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show(StaticVariables.archesInstanceURL + $"resources/{resourceid}?format=json"); response.EnsureSuccessStatusCode(); string responseBody = await response.Content.ReadAsStringAsync(); dynamic responseJSON = serializer.Deserialize <dynamic>(@responseBody); result.Add("resourceid", responseJSON["resourceinstanceid"]); result.Add("graphid", responseJSON["graph_id"]); result.Add("displayname", responseJSON["displayname"]); } catch (HttpRequestException e) { Console.WriteLine("Message :{0} ", e.Message); } return(result); }