I am currently trying to add pagination to my Instagram App and have managed to replace the List that provides the Data Source with the new data, but I want to add it to the existing List so that when I scroll back up it would show the most recent picture. Here is my approach right now:
var request = new RestRequest { RootElement = "data", Resource = "/users/self/feed" };
request.AddParameter ("access_token", instagramAccessToken);
var client = new RestClient ("https://api.instagram.com/v1");
client.ExecuteAsync (request, response => {
var rootObject = JsonConvert.DeserializeObject<RootObject> (response.Content);
string nextURL = rootObject.pagination.next_url;
//// Create Next Client
var requestBack = new RestRequest ();
var clientBack = new RestClient (nextURL);
// GET Next Response
clientBack.ExecuteAsync(requestBack, responseBack => {
var rootObjectBack = JsonConvert.DeserializeObject<RootObject> (responseBack.Content);
table.InvokeOnMainThread (() => {
// Create list that contains newly attained JSON
List<Datum> instagramData = rootObjectBack.data;
// Create dummy list for other values
List<Datum> sloppy = null;
// Send list that contains data, and the sloppy which doesnt contain anything to the Facebook and Twitter Values.
table.Source = new TableSource<Datum>(instagramData, sloppy, sloppy);
table.ReloadData ();
});
});
});
List<x> A = initial data
List<x> B = new data
A.AddRange(B);
A now has B appended to the end
to access the public List (assuming it's called "Data") inside of your existing source
((TableSource<Datum>)table.Source).Data
Related
I keep getting Grpc.Core.RpcException: Status(StatusCode="Unimplemented" ... exception when I try to use the ImageClassificationInstance objects to classify using a model I have created in the Vertex AI. Is there any way (either by using Google AiPlatform library or using HttpRequest ) to perform ImageClassification with some given image?
The following is the code that I am using
var client = builder.Build();
var instance = new ImageClassificationPredictionInstance() { Content = rawdata };
var parameters = new ImageClassificationPredictionParams() { ConfidenceThreshold = 0.5f, MaxPredictions = 5 };
var pv = ValueConverter.ToValue(parameters);
var iv = new[] { ValueConverter.ToValue(instance) };
var ep = new EndpointName("My project ID", "us-central1", "My endpoint id"); // Of course I have set the string to my project ID and my endpoint ID
var returnobj = client.Predict(ep, iv, pv); // This is where the exception pops up
I can confirm that my vertex AI AutoML model is working on the web browser to classify any uploaded picture.
You must set Endpoint on PredictionServiceClientBuilder :
var client = new PredictionServiceClientBuilder
{
Endpoint = "us-central1-aiplatform.googleapis.com"
}.Build();
I take from a website 24images URLs(8images and each image has 3 different sizes this is how the web API works and I cannot change it)
I get those as JSON and I parse them as shown here using newton soft JSON.
Each image URL is stored in property in the same class as its different size images
and different images are stored in other classes
so there are 8classes containing 3 properties which contains image url
I am trying to get 1image url from each class
I am trying to use reflection but since these classes has different names, it is hard to do it (for me at least)
I have came this far
PropertyInfo[] properties = typeof(Photos).GetProperties();
foreach (PropertyInfo property in properties)
{
object a = property.GetValue(mm);
//I cannot use a._1 or a._2 because it is not an instance of the class I want I also cannot convert it since the class names are not the same
}
If you are using Newtonsoft JSON library - then you can use the JObject class, it's an abstraction for any JSON object.
var uri = new Uri("Your API request URL");
using var client = new HttpClient();
var response = await client.GetAsync(uri);
var data = await response.Content.ReadAsStringAsync();
var jObject = JObject.Parse(data);
var img1 = jObject["_1"];
var img2 = jObject["_2"];
//And so on.
Please see the code below, this is how you can read properties.
Helper class to read properties
public static class Helper
{
public static void GetDataByProperty(Type photosType, object photoObject, string propertyNameToRead)
{
foreach (PropertyInfo photoProperty in photosType.GetProperties())
{
if (photoProperty.Name == propertyNameToRead)
{
foreach (var urlProperty in photoProperty.PropertyType.GetProperties())
{
Console.WriteLine(urlProperty.GetValue(photoObject));
}
}
}
}
}
Sample API data
var photos = new Photos
{
_1 = new __1() { _3 = "http://www.google3.com", _2 = "http://www.google2.com", _0 = "http://www.google0.com" },
};
Reading the data
Helper.GetDataByProperty(photos.GetType(), photos._1, "_1");
I have an api that returns values from the database to populate our footer dynamically. These footer values are stored as keys in Redis at the endpoint and then in the main application I'm getting the url of the current page and creating a Hash with the key being the current url and the 5 returned redis key/values from the api as HashEntries. I've shown the code below and right where the HashSet is (at the end) is where the error starts, but if I remove the cache.Hashset, the code runs fine. Do I need to prepopulate the hash or am I missing something obvious here?
//Get relative url after .com/
var url = HttpContext.Current.Request.Url.ToString();
var urlSplit = url.Split(new string[] { ".com/" }, StringSplitOptions.None);
var finalUrl = urlSplit[1];
//TODO: If url isn't in Redis, want to hit endpoint and load it into Redis
//in the background with worker service/async call.
var cache = RedisHelper.Connection.GetDatabase();
var hashExists = cache.HashExists(finalUrl, "footer:recent-content");
if (hashExists)
{
recentContent = cache.HashGet(finalUrl, "footer:recent-content");
topPages = cache.HashGet(finalUrl, "footer:top-pages");
featuredContent = cache.HashGet(finalUrl, "footer:featured-content");
topIntegrations = cache.HashGet(finalUrl, "footer:top-integrations");
featuredIntegrations = cache.HashGet(finalUrl, "footer:featured-integrations");
}
else
{
recentContent = cache.StringGet("footer:general:recent-content");
topPages = cache.StringGet("footer:general:top-pages");
featuredContent = cache.StringGet("footer:general:featured-content");
topIntegrations = cache.StringGet("footer:general:top-integrations");
featuredIntegrations = cache.StringGet("footer:general:featured-integrations");
HashEntry[] redisFooterHash =
{
new HashEntry("footer:recent-content", recentContent),
new HashEntry("footer:top-pages", topPages),
new HashEntry("footer:featured-content", featuredContent),
new HashEntry("footer:top-integrations", topIntegrations),
new HashEntry("footer:featured-integrations", featuredIntegrations)
};
cache.HashSet(finalUrl, redisFooterHash);
}
Also, the reason the keys are footer:recent-content and footer:general... is that in the future we want each page to have a dynamic footer based on it's content.
In my REST Service I have the following:
AssetController:
// GET: <AssetController>
[HttpGet("{companyID}/{machineName}")]
public Asset Get(int companyID, string machineName)
{
Database db = new Database(configuration.ConnectionString);
//DataSet ds = db.executeFunctionSelect("fngetallassets2()");
DataSet ds = db.executeViewSelect("tblasset where LOWER(name) = '" + machineName.ToLower() + "'");
//DataSet ds = db.executeDataSetProc("getallassets", null);
DataTable table = ds.Tables[0];
DataRow row = table.Rows[0];
Asset asset = new Asset
{
ID = int.Parse(row["ID"].ToString()),
CompanyID = int.Parse(row["Company_ID"].ToString()),
Name = row["Name"].ToString(),
IPAddress = row["IP_Address"].ToString(),
CreateDate = DateTime.Parse(row["Create_Date"].ToString()),
IsActive = bool.Parse(row["Is_Active"].ToString())
};
return asset;
}
This works fine... Its the PUT that I need help with
// PUT /<AssetController>/5
// Insert record into the database
[HttpPut("{asset}")]
public void Put([FromBody] string asset)
{
Database db = new Database(configuration.ConnectionString);
db.executeNonQuery("sp_AssetInsert", null);
}
Here I am trying to pass (somehow) the same asset class
In the calling windows forms I use this way to call the PUT Method:
public void InsertAsset(Asset asset)
{
ArrayList parameters = new ArrayList
{
asset.Name,
asset.IPAddress
};
RestClient client = new RestClient("https://localhost:5001/Asset/");
RestRequest request = new RestRequest(Method.PUT);
request.AddJsonBody(asset);
IRestResponse<List<string>> response = client.Execute<List<string>>(request);
if (response.StatusCode == HttpStatusCode.OK)
{
}
I get an error on Response.StatusCode = unsupportedmedia or something like this.
I need to know how to serialize or somehow pass either the class or the JSON string of it or whatever...
Can someone please help me figure out how to call the PUT methods as I have dozens of these to do.
Here is the calling and receiving code used to make this work.
calling:
RestClient client = new RestClient("https://localhost:5001/Asset/");
RestRequest request = new RestRequest(Method.PUT);
request.AddJsonBody(asset); <-- Asset is a class object
RestResponse response = (RestResponse)client.Execute(request);
if (response.StatusCode == HttpStatusCode.OK)
{
}
Receiving Code:
// PUT /<AssetController>/5
// Insert record into the database
[HttpPut]
public void Put([FromBody] Asset asset)
{
Database db = new Database(configuration.ConnectionString);
db.executeNonQuery("sp_AssetInsert", null);
}
I needed to change the [FromBody] string asset to [FromBody] Asset asset
There are several ways to pass parameters:
as url route i.e. https://localhost:5001/Asset/42/MyCompanyName
as url parameter http:// localhost:5001/Asset?companyID=42&machineName=companyname
in body, typically as a json serialized object
when you specify the route in [HttpPut("{paramaters}")] you are specifying option 1. You can use FromBody and FromUrl attributes on the parameter to control this. Simple parameters like numbers and string would typically be part of the URL, while complex objects like Asset will probably be easier to pass in the body.
See also
restsharp parameter posting
asp.net parameter binding
I am trying to extract a list of items in a SharePoint Site below the root site at host.sharepoint.com/sites/mysite. I've tried a bunch of different methods, but only one seems to work:
var host = "host.sharepoint.com:/";
var siteName = "mysite";
var listName = "MyList";
// Generate the Client Connection
var graphHelper = new ApplicationAuthenticatedClient(ClientId, Tenant, ClientSecret);
await graphHelper.ConnectAsync().ConfigureAwait(false);
// Code: itemNotFound
//Message: The provided path does not exist, or does not represent a site
//var list = await graphHelper.GraphClient.Sites[$"{host}{siteName}"].Request().GetAsync();
// Returns a Site, no Lists.
//var list = await graphHelper.GraphClient.Sites[host].Sites[siteName].Request().GetAsync();
//Code: itemNotFound
//Message: The provided path does not exist, or does not represent a site
//var list = await graphHelper.GraphClient.Sites[host].Sites[siteName].Lists[listName].Request().GetAsync();
// List retrieved, but no Items
//var site = await graphHelper.GraphClient.Sites[host].Sites[siteName].Request().Expand("lists").GetAsync();
//var list = await graphHelper.GraphClient.Sites[site.Id].Lists[listName].Request().Expand("Items").GetAsync();
//Code: invalidRequest
//Message: Can only provide expand and select for expand options
//var queryOptions = new List<QueryOption>() { new QueryOption("expand", "fields") };
// This works
var site = await graphHelper.GraphClient.Sites[host].Sites[siteName].Request().GetAsync();
var list = await graphHelper.GraphClient.Sites[site.Id].Lists[listName].Items.Request().Expand("Fields").GetAsync();
I've finally managed to get it to connect, but I'm wondering if there's a better way to navigate to the list, rather than the two API calls? (Assuming that I don't know the Site ID beforehand)
Edit: Using the Graph Explorer, I can access the items using https://graph.microsoft.com/v1.0/sites/{host}.sharepoint.com:/sites/{siteName}:/lists/{listName}/items?expand=fields, but I don't know how to (or if) access that API call in a single call in the .NET API.
It appears that I was on the right track with var list = await graphHelper.GraphClient.Sites[$"{host}{siteName}"].Request().GetAsync(); but the URI was not formatted correctly.
The correct Site ID for https://host.sharepoint.com/sites/mysite/MyList is:
Sites[host.sharepoint.com:/sites/mysite:"]
Retrieving the list from the code in my original question would look like this:
var host = "host.sharepoint.com";
var siteName = "mysite";
var listName = "MyList";
// Generate the Client Connection
var graphHelper = new ApplicationAuthenticatedClient(ClientId, Tenant, ClientSecret);
await graphHelper.ConnectAsync().ConfigureAwait(false);
var list = await graphHelper.GraphClient.Sites[$"{host}:/sites/{siteName}:"].Lists[listName].Request().GetAsync();
it's possible in one API call.
GET https://graph.microsoft.com/v1.0/sites/{host}.sharepoint.com:/sites/{siteName}:/lists/{listTitle}/items?$expand=Fields