Editing Firebase priority via REST - rest

I want to edit the priority of a parent object when a child object is added.
I am using PHP and the REST APIs and adding the new child using POST.
But it appears that the .priority setting can only be made using PUT which resets the parent object.
I have read this post and answer:
Push to Firebase via REST API with Priority
but it applies to setting priority for new objects.
Then I tried retrieving the contents of the parent object and resetting it using .value
but that is not working for me - possibly because the object being pushed is a complex one with child objects of it's own.
So is it possible, or will it be possible in the future to PUSH a new .priority setting?

Responding to Andrew as another answer so that I can format the code:
The command you suggest will get me the existing priority but I still do not see how to change the priority. I am using PHP and here is what I have tried:
$url = $urlToParent . '/.priority/.json';
$fields = '{ ".priority" : ' . $timeStamp . ' }';
$result = doSend($url, $fields, 'PUT');
$url = $urlToParent . '/.priority/.json';
$fields = '{ ' . $timeStamp . ' }';
$result = doSend($url, $fields, 'PUT');
$url = $urlToParent . '/.json';
$fields = '{ ".priority" : ' . $timeStamp . ' }';
$result = doSend($url, $fields, 'POST');
doSend() is a PHP function that uses POST or PUT to send data.
These different attempts either delete the entire object, delete the priority setting or return an error. I am sure there must be a way to do what I want, I just haven't hit on the right combination of URL, parameters and sending method.

The simple answer is that you set the priority directly by appending .priority to the end of your URL. ie:
curl https://SampleChat.firebaseIO-demo.com/users/tom/.priority/.json
If you want to set the priority at the same time that other data is written, you can do that as well. See Writing Priorities from the REST API in the docs.
Note that priorities can only be written to locations that already contain data. If you try to write priority to a null location, the priority will be ignored and the REST call will return null.
There is no command that will both push an object and update the parent's priority at the same time (I'm having trouble coming up with a good use case for this -- leave a comment if you have something in mind).

Related

Laravel: update database record without having to set properties one by one

I'm updating a db record in laravel using eloquent like this:
($request is an object with the properties I want to update)
$book = Book::find($request->id)
$book->title = $request->title;
$book->author = $request->author;
$book->publisher = $request->publisher;
.
.
.
$book->something = $request->something;
$book->save();
It works fine but I wonder if it's possible to do it whitout having to explicity name the properties one by one since the names in $request are exactly the same as those of the model, something like:
$book = $request;
$book->save();
$book = Book::find($request->id);
$book->update($request->all());
More info here https://laravel.com/docs/5.6/queries#updates
However, before doing so, you will need to specify either a fillable or guarded attribute on the model, as all Eloquent models protect against mass-assignment by default.
A mass-assignment vulnerability occurs when a user passes an unexpected HTTP parameter through a request, and that parameter changes a column in your database you did not expect. For example, a malicious user might send an is_admin parameter through an HTTP request, which is then passed into your model's create method, allowing the user to escalate themselves to an administrator.
Read more about in documentations.

GoDaddy DNS API - Building a module with numerous commands

EDIT: I am looking for assistance on the GoDaddy API and how to form the request so I can delete a record. I can replace records and add new records and, of course, read records. I do not need assistance on building the module. Just having a problem figuring out the REST API piece for doing a delete on GoDaddy with the API.
I found this: Error 422 When Using Powershell to Update DNS Records on GoDaddy API
This gave me a good start. I have a number of functions I have already written to create or modify various different types of record. I have learned a lot along the way. Something that I have been unable to figure out so far is how to use the API to delete an existing record that is no longer needed.
I discovered that if I had an existing record of the same name and type and I tried to create a new, it would replace what was there with the new value. If it is just an A record and that is your desire, that's great. But if it is an MX or NS record, that is probably not the desired result. I am working on the helper functions to make sure that I don't blow away existing records before I publish to my module.
I am relatively new to reading API documentation and working with REST API's so I am probably just missing something basic, but if anyone could provide me with some guidance on how to configure my call so that I can clean up records that are no longer needed.
I am also having some issues formatting the SRV record properly so that it doesn't error out when I make the call. I am not sure where my problem is and I am not seeing anything in the documentation of allowed/expected values for Protocol, Service, etc.
Here is an excerpt of my code where I am calling:
try{
$ret = Invoke-WebRequest https://api.godaddy.com/v1/domains/domain/records/$type/$alias -method put -headers $headers -Body $json -ContentType "application/json"
if ($ret.StatusCode -eq 200) { Write-Verbose -Message "Success!" }
else { Write-Warning -Message "ERROR" }
} catch {
$result = $_.Exception.Response.GetResponseStream()
$reader = New-Object System.IO.StreamReader($result)
$reader.BaseStream.Position = 0
$reader.DiscardBufferedData()
$responseBody = $reader.ReadToEnd();
$responseBody = $responseBody | ConvertFrom-Json | Format-Custom | Out-String
Write-Warning -Message "ERROR: $responseBody"
}
You will recognize this code block as it is largely taken intact from the aforementioned post. I am passing in $type as a valid record type (A, CNAME, etc.). $alias is the name of an existing record. $headers is a properly formatted header with a valid key/secret pair (I am able to Get and Put other records). I have tried omitting the body altogether to delete a value and that fails. I have tried doing the body with a JSON empty set ([], [{}]) as well as passing in data and TTL values with no value in the JSON. I can't get a successful query to work. I am testing with the following:
Invoke-WebRequest "https://api.godaddy.com/v1/domains/mydomain/records/A/test2" -method put -headers $header -ContentType "application/json" -Body '[{}]'
Any guidance or pointers to documentation that I haven't found would be greatly appreciated.
I had the same problem, it's about JSON encoding. I will show you a PHP example you can adapt to anything. The format that GoDaddy will provide is like this:
[{"":"","":"","":"",.....}]
I don't know from where comes the [] but I remove them, the parse json with regular PHP functions:
$json = str_replace("]","",$json);
//print_r($json);
$json = json_decode($json,true);
Now you will get the usable object to work with.
When send data to GoDaddy, do the same (I use curl)
$data = '[{"type":"'.$type.'","name":"'.$name.'","data":"'.$data.'","ttl":'.$ttl.'}]';
A full example that I build for updating my home server dynamic IP :) Check the functions Update() and get()
https://github.com/yo3hcv/Dynamic-DNS-hosting-home-or-mobile-with-GoDaddy/blob/master/pip.php
This maybe useful for others as I ran into the same unclarity about deleting a DNS record from a Godaddy domain using their API.
Godaddy doesn't support deleting an individual record.
From their support on the question whether it was possible to delete a record:
Dear Sir/Madam,
This API call does not exist at this time and it is something on our backlog to build out but there is no ETA to have it created. Current you can use the Put endpoint on the record type to replace all but the one you do not want to keep or you may go into our UI to remove it that way.
See their api documentation for what is available (at this date July 2020).
However, what you can do is replace all the existing records with a new set.
So suppose you have three A records with subdomain SD1, SD2 and SD3 and you want to remove SD2. You could sent an update command using:
curl -X PUT "https://api.godaddy.com/v1/domains/<yourdomain>/records/A" -H "accept: application/json" -H "Content-Type: application/json" -H "Authorization: <yourAPIKEY>" -d "[ { \"data\": \"<IP for SD1>\", \"name\": \"SD1\", \"ttl\": 3600 }, { \"data\": \"<IP for SD3>\", \"name\": \"SD3\", \"ttl\": 3600 }]"
Which will result in two A records one for SD1 and one for SD3. As long as you have at least one recordtype left (this also works for MX, MS, TXT) you will be able to remove others.
If you want to remove the last you have to go back to the Godaddy GUI, unfortunately.

Sending an unbuffered response in Plack

I'm working in a section of a Perl module that creates a large CSV response. The server runs on Plack, on which I'm far from expert.
Currently I'm using something like this to send the response:
$res->content_type('text/csv');
my $body = '';
query_data (
parameters => \%query_parameters,
callback => sub {
my $row_object = shift;
$body .= $row_object->to_csv;
},
);
$res->body($body);
return $res->finalize;
However, that query_data function is not a fast one and retrieves a lot of records. In there, I'm just concatenating each row into $body and, after all rows are processed, sending the whole response.
I don't like this for two obvious reasons: First, it takes a lot of RAM until $body is destroyed. Second, the user sees no response activity until that method has finished working and actually sends the response with $res->body($body).
I tried to find an answer to this in the documentation without finding what I need.
I also tried calling $res->body($row_object->to_csv) on my callback section, but seems like that ends up sending only the last call I made to $res->body, overriding all previous ones.
Is there a way to send a Plack response that flushes the content on each row, so the user starts receiving content in real time as the data is gathered and without having to accumulate all data into a veriable first?
Thanks in advance for any comments!
You can't use Plack::Response because that class is intended for representing a complete response, and you'll never have a complete response in memory at one time. What you're trying to do is called streaming, and PSGI supports it even if Plack::Response doesn't.
Here's how you might go about implementing it (adapted from your sample code):
my $env = shift;
if (!$env->{'psgi.streaming'}) {
# do something else...
}
# Immediately start the response and stream the content.
return sub {
my $responder = shift;
my $writer = $responder->([200, ['Content-Type' => 'text/csv']]);
query_data(
parameters => \%query_parameters,
callback => sub {
my $row_object = shift;
$writer->write($row_object->to_csv);
# TODO: Need to call $writer->close() when there is no more data.
},
);
};
Some interesting things about this code:
Instead of returning a Plack::Response object, you can return a sub. This subroutine will be called some time later to get the actual response. PSGI supports this to allow for so-called "delayed" responses.
The subroutine we return gets an argument that is a coderef (in this case, $responder) that should be called and passed the real response. If the real response does not include the "body" (i.e. what is normally the 3rd element of the arrayref), then $responder will return an object that we can write the body to. PSGI supports this to allow for streaming responses.
The $writer object has two methods, write and close which both do exactly as their names suggest. Don't forget to call the close method to complete the response; the above code doesn't show this because how it should be called is dependent on how query_data and your other code works.
Most servers support streaming like this. You can check $env->{'psgi.streaming'} to be sure that yours does.
Plack is middleware. Are you using a web application framework on top of it, like Mojolicious or Dancer2, or something like Apache or Starman server below it? That would affect how the buffering works.
The link above shows an example by Plack's author:
https://metacpan.org/source/MIYAGAWA/Plack-1.0037/eg/dot-psgi/echo-stream-sync.psgi
Or you can do it easily by using Dancer2 on top of Plack and Starman or Apache:
https://metacpan.org/pod/distribution/Dancer2/lib/Dancer2/Manual.pod#Delayed-responses-Async-Streaming
Regards, Peter
Some reading material for you :)
https://metacpan.org/pod/PSGI#Delayed-Response-and-Streaming-Body
https://metacpan.org/pod/Plack::Middleware::BufferedStreaming
https://metacpan.org/source/MIYAGAWA/Plack-1.0037/eg/dot-psgi/echo-stream.psgi
https://metacpan.org/source/MIYAGAWA/Plack-1.0037/eg/dot-psgi/nonblock-hello.psgi
So copy/paste/adapt and report back please

Properly handle Asana API rate limit in perl

Please accept my apology if this is something terribly obvious that I am simply missing. I have a perl script that hits the Asana restful API at a burst rate of a bit more than 100 calls per minute. When I stress test it, I do occasionally hit their rate limit and see error 429. I know from reading Asana's documentation that it will return a "retry-after" response header but I can't figure out for the life of me how to retrieve / open / read this header. Any advice you can provide would be appreciated.
Edit:
My code is attached below. Of course I've erased sensitive information such as my API key and project number, but the core code is here. If I run this just once, it doesn't produce enough calls per minute to trigger an error. I have to run it about 3-4 times simultaneously to produce the error. One might say "well don't do that." While correct, the point of this exercise is to produce the error, so running it four times simultaneously is good.
When you do so with a valid API key and project number, occasionally you will get this error:
{"errors":[{"message":"You have made too many requests recently. Please, be chill."}]}
My question is how to retrieve the header that apparently includes a retry-after field along with a number of seconds. I may just resort to building in a delay of 20 seconds every time an error is returned, but I'd prefer to handle the error more elegantly.
#!/usr/local/bin/perl
my $counter = 0;
my $AsanaAPIcode = "...";
my $AsanaProjectID = "...";
my $AsanaFullString = 'curl -u ' . $AsanaAPIcode . ': https://app.asana.com/api/1.0/projects/' . $AsanaProjectID . '?opt_fields=archived';
my $APIoutput = `$AsanaFullString`;
print $APIoutput;
my $startTime = time;
my $totalCount = 200;
while ($counter<=$totalCount) {
print $counter . "\n";
$APIoutput = `$AsanaFullString`;
print $APIoutput . "\n";
$counter++;
}
my $endTime = time;
my $totalTime = $endTime - $startTime;
print "Total time = " . $totalTime . " seconds.\n";
print $totalCount / ($totalTime / 60) . " API calls per minute.\n";
print "end";
The 'Retry-After' is in the HTTP response, which is lost by the backtick call to curl.
A clunky solution is to use 'curl -D' (--dump-headers) which will drop all HTTP headers into the response, which you'd then have to parse and remove.
A better solution would be to use the LWP library (perldoc LWP::UserAgent).
Obviously I can't test this code without an API key...
I think you can inject these into the URL (https://name:password#app.asana.com/api...)
use LWP::UserAgent;
use LWP::Protocol::https;
my $agent = LWP::UserAgent->new(); # check LWP docs for extra params
my $request = LWP::Request->new( 'https://app.asana.com/api/1.0/projects/' . $AsanaProjectID . '?opt_fields=archived' )
my $response = $ua->request($request);
if ($response->code == 429) {
my $retry = $response->header('Retry-After');
...
}
Or...you could try CPAN for something like WWW::Asana.
http://search.cpan.org/dist/WWW-Asana/
We ran into the issue with our Asana Connector for Klok. We contacted the folks at Asana and they were willing to add the "retry after" amount into the body of the response. So, you can now grab it from the "retry_after" property:
{
"errors":[{"message":"You have made too many requests recently. Please, be chill."}],
"retry_after":30
}
This was a big help for us since we are using Adobe AIR 2.x which doesn't give us access to the response headers of an error response.
There is a CPAN module for that, WWW::Asana, and it handles the rate limit response properly.

How to read a web page content which may itself be redirected to another url?

I'm using this code to read the web page content:
my $ua = new LWP::UserAgent;
my $response= $ua->post($url);
if ($response->is_success){
my $content = $response->content;
...
But if $url is pointing to moved page then $response->is_success is returning false. Now how do I get the content of redirected page easily?
You need to chase the redirect itself.
if ($response->is_redirect()) {
$url = $response->header('Location');
# goto try_again
}
You may want to put this in a while loop and use "next" instead of "goto". You may also want to log it, limit the number of redirections you are willing to chase, etc.
[update]
OK I just noticed there is an easier way to do this. From the man page of LWP::UserAgent:
$ua->requests_redirectable
$ua->requests_redirectable( \#requests )
This reads or sets the object's list of request names that
"$ua->redirect_ok(...)" will allow redirection for. By default,
this is "['GET', 'HEAD']", as per RFC 2616. To change to include
'POST', consider:
push #{ $ua->requests_redirectable }, 'POST';
So yeah, maybe just do that. :-)