How to get at least some data on the repository license? - c#

I search for repositories according to the documentation.
https://github.com/octokit/octokit.net/blob/master/docs/search.md
How to get at least some data on the repository license?
I mean get data from this page. enter image description here
Or the content of the file - "LICENSE.txt"
I need information from any of the areas indicated in the figure.
I try to do on using the code, but I don’t understand how to do it correctly
var repoLicen = client.Repository.GetLicenseContents("octokit", "octokit.net");
var licen = repoLicen.li???

Try this:
var licenseContents = github.Repository.GetLicenseContents("octokit", "octokit.net").Result;
GetLicenseContents returns Task this means that the method is asynchronous.
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/

Related

How to download the picture of a Google+ user only if it has changed after a certain date?

I am using Google API to get information about an authenticated user. I can get the basic profile information, such as the ID and the full name. From the profile information, I can get the URL to the picture:
var plusMeUri = new Uri($"https://www.googleapis.com/plus/v1/people/me?key=<APP-ID>&access_token=<ACCESS-TOKEN>");
string userResponse = await HttpClient.GetStringAsync(plusMeUri);
JObject userObject = JObject.Parse(userResponse);
...
var imageObject = userObject.GetValue("image") as JObject;
var pictureUrl = imageObject.GetValue("url").Value<string>();
var pictureUri = new Uri(pictureUrl);
string uri = $"{pictureUri.Scheme}://{pictureUri.Host}{pictureUri.AbsolutePath}";
var pictureRequest = new HttpRequestMessage(HttpMethod.Get, uri);
pictureRequest.Headers.IfModifiedSince = <previous-timestamp>;
HttpResponseMessage pictureResponse = await HttpClient.SendAsync(pictureRequest);
if (pictureResponse.StatusCode == HttpStatusCode.NotModified)
// No need to handle anything else
return;
Question
I do not want to download the user's picture if it has not changed. This is why I am using the IfModifiedSince property. It does work with Facebook's API but it does not seem to work with Google's. How can I make it work?
From the information given, it seems like what you're trying to do is determine whether the image you're downloading/about to download is the same image as you've downloaded before. After looking at the Google+ API docs, it looks like the header you've been using isn't officially (at least not obviously) supported by their APIs.
But this is not the only way we can determine whether the image has changed or not (in fact, date last modified isn't necessarily the best way to do this anyway). Alternative methods include:
1) diffing the two images
2) checking the url (if we can assume different resources have different urls)
1 is likely the most accurate but also likely the least efficient, so I'll leave that to you to solve if you decide to go that route. I think the most promising is #2. I went ahead and played around with the API a little bit and it looks like the image.url field changes when you update your profile picture.
For example, here are my last two Google+ profile picture URLs:
https://lh4.googleusercontent.com/-oaUVPGFNkV8/AAAAAAAAAAI/AAAAAAAAAqs/KM7H8ZIFuxk/photo.jpg?sz=50
https://lh4.googleusercontent.com/-oaUVPGFNkV8/AAAAAAAAAAI/AAAAAAAAl24/yHU99opjgN4/photo.jpg?sz=50
As such, instead of waiting for the response from the server and checking its header to decide whether the image has been updated or not, you may be able to short-circuit the entire HTTP request by simply checking whether the last image you pulled down was from the same url or not. If it was from the same URL, it's likely you've already acquired that image otherwise you may not have it so should incur the cost of downloading anyway.
In this case, your code would read something like:
var imageObject = userObject.GetValue("image") as JObject;
var pictureUrl = imageObject.GetValue("url").Value<string>();
if(pictureUrl != <previous-picture-url>)
{
// insert get new picture logic here...
}

Send and return variable with c# API call?

I have a c# script task in an ssis package designed to geocode data through my company's proprietary system. It currently works like this:
1) Pull query of addresses and put in data table
2) Loop through that table and Foreach row, build request, send request, wait for response, then insert back into the database.
The issue is that each call takes forever to return, because before going out and getting a new address on the api side, it checks a current database(string match) to ensure the address does not already exist. If not exists, then go out and get me new data from a service like google.
Because I'm doing one at a time, it makes it easy to keep the ID field with the record when I go back to insert it into the database.
Now comes the issue at hand... I was told to configure this as multi-thread or asynchronous. Here is the page I was reading on here about this topic:
ASP.NET Multithreading Web Requests
var urls = new List<string>();
var results = new ConcurrentBag<OccupationSearch>();
Parallel.ForEach(urls, url =>
{
WebRequest request = WebRequest.Create(requestUrl);
string response = new StreamReader(request.GetResponse().GetResponseStream()).ReadToEnd();
var result = JsonSerializer().Deserialize<OccupationSearch>(new JsonTextReader(new StringReader(response)));
results.Add(result);
});
Perhaps I'm thinking about this wrong, but if I send 2 requests(A & B) and lets say B actually returns first, how can I ensure that when I go back to update my database I'm updating the correct record? Can I send the ID with the API call and return it?
My thoughts are to create an array of requests, burn through them without waiting for a response and return those value in another array, that I will then loop through on my insert statement.
Is this a good way of going about this? I've never used Parrallel.ForEach, and all the info I find on it is too technical for me to visualize and apply to my situation.
Perhaps I'm thinking about this wrong, but if I send 2 requests(A & B) and lets say B actually returns first, how can I ensure that when I go back to update my database I'm updating the correct record? Can I send the ID with the API call and return it?
None of your code contains anything that looks like an "ID," but I assume everything you need is in the URL. If that is the case, one simple answer is to use a Dictionary instead of a Bag.
List<string> urls = GetListOfUrlsFromSomewhere();
var results = new ConcurrentDictionary<string, OccupationSearch>();
Parallel.ForEach(urls.Distinct(), url =>
{
WebRequest request = WebRequest.Create(url);
string response = new StreamReader(request.GetResponse().GetResponseStream()).ReadToEnd();
var result = JsonSerializer().Deserialize<OccupationSearch>(new JsonTextReader(new StringReader(response)));
results.TryAdd(url, result);
});
After this code is done, the results dictionary will contain entries that correlate each response back to the original URL.
Note: you might want to use HttpClient instead of WebClient, and you should take care to dispose of your disposable objects, e.g. StreamReader and StringReader.

Clarification on how to update (patch) objects using the Microsoft.Graph Client

The following code is the only way I found so far to update an object using the Microsoft Graph Client Library
Scenario:
Load an exisiting object (an organization)
Modify a value (add entry in securityComplianceNotificationPhones)
Send the update
Code
var client = new GraphServiceClient(...);
var org = client.Organization["orgid"].Request().GetAsync().Result;
var secPhones = new List<string>(org.SecurityComplianceNotificationPhones);
secPhones.Add("12345");
var patchOrg = new Organization();
patchOrg.SecurityComplianceNotificationPhones = secPhones;
var orgReq = new OrganizationRequest(
client.Organization[org.Id].Request().RequestUrl,
client, new Option[] {});
orgReq.UpdateAsync(patchOrg).Wait();
I needed to use the patchOrg instance because of two things:
The Graph API documentation states
"In the request body, supply the values for relevant fields that
should be updated. Existing properties that are not included in the
request body will maintain their previous values or be recalculated
based on changes to other property values. For best performance you
shouldn't include existing values that haven't changed."
If you actually do include existing values that haven't changed
(i.e. assginedLicenses) the request fails, if those existing values
are readonly.
My question is: Is/will there be a more straightforward way of updating existing objects like for example in the Azure ActiveDirectory GraphClient? Just for comparison, the same scenario in Azure Active Directory Graph
var client = new ActiveDirectoryClient(...);
var org = client.TenantDetails.GetByObjectId("orgid").ExecuteAsync().Result;
org.SecurityComplianceNotificationPhones.Add("12345");
org.UpdateAsync().Wait();
The Graph client library model is slightly different from the older SDK model the AAD client library you linked. The older model passed around objects that tried to be a bit smarter and reason about which properties were changed, only sending those. One of the main drawbacks of this model was that the library made many more service calls in the background and had a much heavier payload in each call since ExecuteAsync() would often need to retrieve every object in the request builder chain. The newer library does require the developer to do more explicit reasoning about what data is being passed but also gives greater control over network calls and payload. Each model has its tradeoffs.
To accomplish what you want, here's the approach I would recommend instead of creating a second org object altogether:
var client = new GraphServiceClient(...);
var orgRequest = client.Organization["orgid"].Request();
var org = orgRequest.Select("securityComplianceNotificationPhones").GetAsync().Result;
var secPhones = new List<string>(org.SecurityComplianceNotificationPhones);
secPhones.Add("12345");
org.SecurityComplianceNotificationPhones = secPhones;
orgRequest.UpdateAsync(org).Wait();

Azure Notification Hub installation not updating tags

I am attempting to update the tags of an installation within Azure Notification Hub after registration. I am following several guides for this, notably here and here.
Both of these guides suggest that the following code should work however it is plainly not; the tag never gets updated. There are no errors, and I can guarantee that the installationId is correct. I am guessing I am setting the path/value of the tag incorrectly.
// in constructor:
var _notificationHub = NotificationHubClient.CreateClientFromConnectionString(Settings.ConnectionStrings.NotificationHub, Settings.Defaults.NotificationHubName);
// in WebApi endpoint:
var installationUpdates = new List<PartialUpdateOperation>();
var userDetail = _userDetailRepo.Get(id);
installationUpdates.Add(new PartialUpdateOperation
{
Operation = UpdateOperationType.Replace,
Path = "/tags/interestedin", // is this incorrect?
Value = interestedIn.ToUpper()
});
userDetail.InterestedIn = interestedIn;
await Task.WhenAll(
_userDetailRepo.InsertOrReplace(userDetail),
_notificationHub.PatchInstallationAsync(installationId, installationUpdates));
Here is the installation object's tags, as per VS:
I also tried hardcoding the path to Path = "/tags/interestedin:W" but it made no difference.
Can someone tell me if I am doing something wrong here, and if so how I should amend my code. Thanks.
Unfortunately, Path = "/tags/interestedin" is not going to work as of now. We are currently working on wildcards' support. Once it is done, something like "/tags/interestedin*" will work fine for you.
While Path = "/tags/interestedin:W" should be OK. If you could provide namespace name, hub name, and a timeframe, then I'll take a look at logs to check what is going on there.

sending and retrieving data to API

I have a solution with 2 projects:
Contains the API
the module where i can select data (combobox etc...)
in the second module i select the API as reference that way i should be able to use data from the first module (my API) using Mollie.Api;
The First thing i want to do is fill a combobow with all the issuers, they should be in issuers.data:
Issuers issuers = mollieClient.GetIssuers();
foreach (Issuer issuer in issuers.data)
{
comboBox1.Items.Add(issuer.name);
}
the problem i have is: mollieClient does not exist in current context
i want to fill another combobox with methods but when i try to do it how they say i should do it doesnt work:
Method methods = new Method();
methods = Mollie.Api.Method.all();
however when i do:
Mollie.Api.Method.* the helper gives me then all the possible methods at *, but how can i get them automaticly in my combobox?
The second thing is when the method and issuer is selected i want to send that data back to the API. The API should be able to proces this and send me a message back that the transaction was a succes.
It appears like you're using mollie-api-csharp. You've written mollieClient, but according to the source Mollie.Api.MollieClient should start with a capital letter.
EDIT: Looks like you're following the how-to-use tutorial from the repo.
I think you forgot to create the MollieClient instance before getting the issuers.
MollieClient mollieClient = new MollieClient();
mollieClient.setApiKey("your_api_key_here");

Categories