Updating project parameters in AtTask using restsharp put method - c#

I am struggling a little in my project with Attask.
My aim is to update the alignmentValues of a project using custom form data..
I have been able to create new alignment values to update but I am unable to execute it using put method...
the request I want to execute is
PUT /attask/api/project/4c7...?updates=
{
alignmentValues: [
{
scoreCardOptionID: "2222...54d0",
scoreCardQuestionID : "8897...54d1",...
},....
]
}
my code snippet is
var request = new RestRequest("project/{id}", Method.PUT);
request.AddUrlSegment("id", pid);
request.RequestFormat = DataFormat.Json;
JObject _putData = new JObject();
_putData.Add("alignmentValues",newAnswers);
and for updates object I tried few combinations
request.AddParameter("updates",_putData,ParameterType.RequestBody); //no effect
request.AddBody(new {name = "updates", value = _putData}); //no effect
With this body approach I am even unable to update the name of project.
But when I supply the parameters as query string, it successfully updates the name but fails for alignment values as the url becomes too large
var request = new RestRequest("project/{id}?updates=" + _putData , Method.PUT);
Above works if _putData is small...like name = "TEST"..but fails for big json array..
Any suggestions on how to update values using addbody/addobject/addjsonobject/addparameter...because I need to send request in body because of its large size...
Thanks in advance.

Well this is not an answer but I made it to work by fluke..
SO the main problem still remains..I can't use body with PUT request..Even if I use its shows no result..so I had to go with query string only...
Now for my problem on big parameters in the query string, I was sending the whole alignmentvalues object where I had to update only two fields within that object.
So in a trial and error approach i only passed three fields within the object - answer ID and two fields to be updated...And it reduced the query string size and fortunately for me it worked..

Related

Azure datafactory: Headers in REST dataset with C#

I'm currently building a datafactory that needs to be rolled out to a whole slew of tenants. So I've opted to write C# code to create all the datafactory artifacts using a configuration file.
I've got almost everything done. I just run into a snag when creating the REST stuff. I need to add Headers to a WebActivity and to a REST Dataset.
I've tried doing this using a string which resulted in every character in the string becoming a separate header.
I've tried doing this using the HeaderOption class which resulted in... well... nothing. Empty headers in the DataFactory.
What is the best way to go about this?
Example code for the WebActivity (I'm assuming that getting this to work will mean getting the Dataset to work as well);
new WebActivity
{
Name = config.TokenActionName,
Method = config.TokenMethod,
Url = config.TokenUrl,
Headers = config.TokenHeader,
Body = restBody
},
Where config.TokenHeader is either
config.TokenHeader = "\"Content-Type\": \"application/x-www-form-urlencoded\""
or
config.TokenHeader = new HeaderOption("Content-Type", "application/x-www-form-urlencoded");

TFS 2018 API: Can access workitems but not workitem API end point

I created a middleware app that will pull work item data from TFS.
I was able to do this using the workitems end point.
http://sampleserver:8080/tfs/sampleproject/_apis/wit/wiql?api-version=4.0/workitems?ids=1,2,3
Now, I also need to get the work item links per work item. Per docu I would need to access the workitem with expand items. But unfortunately, work item end point does not seem to work.
http://sampleserver:8080/tfs/sampleproject/_apis/wit/wiql?api-version=4.0/workitem/3
Am I missing something here?
According to your description, looks like you just want the URL of created WorkItem, so that anyone when click on URL, created Work Item will be Open.
https://tfsurl:8080/tfs/DefaultCollection/PatrickProject/_workitems/edit/172/
The URL should be above format and here DefaultCollection is the collection name and the PatrickProject is the project name. I used this url and got rid of the id '172' in this case and use the ID of newly created work item. This would return the URL to go to the work item HTML page.
So it's a fixed format, if you have Newly Created WorkItem ID and collection name , project name, you just need to follow above format and change the last value of work item ID. That's it , ignore of which work item type you created.
If you want do this with code, do not use Rest API, you need to use client API, sample snippet:
var tfsURI = new Uri("http://test:8080/tfs");
var networkCredential1 = new NetworkCredential("test", "test!");
ICredentials credential = (ICredentials)networkCredential1;
Microsoft.VisualStudio.Services.Common.WindowsCredential winCred = new Microsoft.VisualStudio.Services.Common.WindowsCredential(credential);
VssCredentials vssCredentials = new VssCredentials(winCred);
using (TfsTeamProjectCollection collection = new TfsTeamProjectCollection(tfsURI, vssCredentials))
{
collection.EnsureAuthenticated();
TswaClientHyperlinkService hyperlinkService =
collection.GetService<TswaClientHyperlinkService>();
String TFSurl = hyperlinkService.GetWorkItemEditorUrl(17648).ToString(); //17648 WorkItem ID
}
Hope this Helps!
To get specific work item information you need to use the Get Workitem API call so try http://sampleserver:8080/tfs/sampleproject/_apis/wit/workitems/3?api-version=4.0 instead.
You can also use this http://sampleserver:8080/tfs/sampleproject/_apis/wit/workitems/3?$expand=Links&api-version=4.0 and this will return the work with Id of 3 and all it's links (parent, attached files, changetset, etc.)
Notice that the api-version=4.0 with change depending on the version of TFS/Service you are using and should always be the last string in the REST call.

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.

Adding a SOAP Header from a string to a request built from WSDL

I'm connecting to a WSDL through visual studio to build a web request in c#, but having an issue with authentication. The company we're working with requires information to be in the header, but doesn't expose the information they need through the WSDL, saying that, "this exact header needs to be included in all requests".
Theoretically I'd like to just get the header built in a StringBuilder object and attached to the client object, but I can't see how to do that easily. Everything I use, like WCFExtras+ or even just System.ServiceModel.Channels.MessageHeader wants an object, values, and a ns (which is obvious with C# being an OOPL).
I tried doing something like this, but i realized you can't just add XML manually like this, it strips out < and > into web-safe text equivalents:
using (var client = new productsClient())
{
using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))
{
MessageHeader<string> header = new MessageHeader<string>("wsse:Security", true, "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", true);
var untyped = header.GetUntypedHeader(hardcodedHeader, "http://api.xxxcon.com/xxx");
OperationContext.Current.OutgoingMessageHeaders.Add(untyped);
var request = new productRequest();
response = client.getProduct(request);
}
}
Perhaps using the MessageHeader.CreateHeader is the answer? I can use this to create a simple header that has one line
(in the shape of value) but when i try to nest the requests together to create a parent and child relationship:
var username = System.ServiceModel.Channels.MessageHeader.CreateHeader
("wsse:UsernameToken ", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
, usernameString);
var pw = System.ServiceModel.Channels.MessageHeader.CreateHeader
("wsse:Password", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
, passwordString);
var header1 = System.ServiceModel.Channels.MessageHeader.CreateHeader
("wsse:UsernameToken ", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
, "", username);
But again, this throws an error. I suspect this class wasn't meant to nest xml objects like this.
All in all, I'd like to actually use the WSDL and not have to manually generate all the XML, but it seems i'm hindered by object-orientation, when i really want to just say, "here's the header, just trust me that everything is there. slap this baby right on".
Is anybody familiar with what to do in this circumstance? Thank you in advance.

Facebook Graph API {id}/feed?limit=x - restrict to messages since a certain message id

I have a small problem, I am working on an aggregation application that is collecting messages from pages in realtime.
This is working fine, but I get the same message on every call and then filter out the messages that I have already seen manually.
This means that a large amount of data is being transferred every time I make a call to the graph api.
Is there a way to limit the message as messages since this message id?
currently using the c# Facebook SDK
var fb = new FacebookClient("access_token");
dynamic parameters = new ExpandoObject();
parameters.limit = _facebookMessagesToRetrieveAtATime.ToString(CultureInfo.InvariantCulture);
//Want to add a new param here to say messages since this id.
var facebookUrl = String.Format("{0}/feed", "Page ID");
dynamic resp = fb.Get(facebookUrl, parameters);
Thanks in advance.
You can use the since url parameter in your calls, as described at https://developers.facebook.com/docs/graph-api/using-graph-api/v2.1#paging
This would make it necessary that you store somewhere in your application the timestamp when you last requested the respective feed
This would yield in
var facebookUrl = String.Format("{0}/feed?since={last_update_timestamp}", "Page ID");
where {last_update_timestamp} is the timestamp (unixtime in seconds) of the last update.

Categories