Querying Orion with metadata value of String type - metadata

I am trying to query Orion, version 2.2.0-next, so to return records matching metadata value of different strings.
My JSON looks like this:
"AttributeName": {
"type": "String",
"value": "Some Value",
"metadata": {
"AttributeMeta": {
"type": "String",
"value": "Some Other Value"
}
}
}
I am querying Orion as follows:
http://myip:1026/v2/entities?type=myType&mq=AttributeName.AttributeMeta==Some%20Other%20Value&options=count
Unfortunately, the return is empty.
Any ideas, suggestions?
EDIT:
Entity creation code in PHP:
$data[] = array("id" => (mktime() + $i)."_$client", "type" => "myType", "datetime" => array("type" => "DateTime", "value" => date("Y-m-d\TH:i:s", mktime($ho, $mi, $se, $mo, $da, $ye))), "Parameter1" => array("type" => "String", "value" => (string)$parameter1), "Parameter2" => array("type" => "Float", "value" => (float)$Parameter2), "Parameter3" => array("type" => "String", "value" => (string)$parameter3, "metadata" => array("Parameter3Meta" => array("type" => "String", "value" => "$parameter3meta"))), "location" => array("type" => "geo:point", "value" => "$lat, $lon"), "range" => array("type" => "Float", "value" => (float)$range), "Parameter4" => array("type" => "String", "value" => (string)$parameter4));
curl_setopt($ch, CURLOPT_URL, "http://$ip/v2/entities");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data[$i]));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("fiware-service: $tenant", "fiware-servicepath: /$subtenant", "Content-Type: application/json"));
curl_setopt($ch, CURLOPT_PORT, 1026);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 400);
$response = curl_exec($ch);
EDIT 2: In the above query two headers are used: fiware-service: tenant and fiware-servicepath: /subtenant

Try removing 'type=myType'.
It's not clear what's the type of the entity you are trying to extract with the query.

Related

Get responses from External API need Login session

I want to get data from External API. but, I must login to get session before Getting data and I must Logout after get data
this is my code
use GuzzleHttp\Client;
$client = new Client();
$res = $client->request('POST', 'Login_URL', [
'json' => [
"JSON param"
]
]);
$res = $client->request('GET', 'Get_URL');
$res = $client->request('POST', 'Logout_URL');
but I can only the first step (Login). and I getting error message in the second step to get data
Client error: `GET "Get_URL" ` resulted in a `401 Unauthorized` response:{"message":"You are not logged in."}
how I can run all this code with login session on first step ?
this code for you to do login and logout session
$cookie_jar = tempnam("tmp", "cookie");
//login
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => '_URL',
CURLOPT_RETURNTRANSFER=> true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION=> true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_COOKIEJAR => $cookie_jar,
CURLOPT_POSTFIELDS =>'{
"name" : "****",
"password" : "****",
"company" : "****"
}',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json'),
)
);
$response = curl_exec($curl);
//get data
curl_setopt_array($curl, array(
CURLOPT_URL => '_URL',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_COOKIEFILE => $cookie_jar
)
);
$datapo = json_decode(curl_exec($curl));
//logout
curl_setopt_array($curl, array(
CURLOPT_URL => '_URL',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => '{
"name" : "****",
"password" : "****",
"company" : "****"
}',
CURLOPT_COOKIEFILE => $cookie_jar
)
);
$response = curl_exec($curl);

Perl Rest Web Service Parameters

I am trying to call REST web service as follows:
my $client = REST::Client->new();
$client->setHost("http://myhost.com");
$client->POST('/xx/yy/Submit',
$params,
{'Content-type' => 'application/json'}
);
my $response = $client->responseContent();
I do not how to create parameter list, api definition is as follows:
{
"Credential": {
"Username": "String",
"Password": "String"
},
"DataCoding": "Default",
"Header": {
"From": "String",
"ValidityPeriod": 0
},
"Message": "String",
"To": ["String"]
}
I tried following but did not work:
my %params = (Credential => {
Username => $username,
Password => $password
},
DataCoding => 'Default',
Header => {
From => $from,
ValidityPeriod => 0
},
Message => "Test",
To => ['90535xxxx','90542xxxxx']
);
$client->POST('/v1/xml/syncreply/Submit',
encode_json(\%params),
{'Content-type' => 'application/json',
'Accept' => 'application/json'
}
);
It gives following error:
<ErrorCode>SerializationException</ErrorCode>
Error: System.Runtime.Serialization.SerializationException: There was an error deserializing the object of type Barabut.Gw.Submit. The data at the root level is invalid. Line 1, position 1. ---&gt; System.Xml.XmlException: The data at the root level is invalid. Line 1, position 1.
I wrote end-point address incorrect, it worked at the format I wrote, sorry for this situation.

Wildfly Postgres Connection Failed

I've tried to add Postgres as datasource in Wildfly but keep on getting the JDBC connection error as follow :
Failed to create JDBC connection.
An internal error occurred. Details Unexpected HTTP response: 500
Request {
"name" => "PostgresDS",
"enabled" => true,
"jndi-name" => "java:/PostgresDS",
"user-name" => "admin",
"password" => "123",
"pool-name" => "PostgresDS_Pool",
"use-ccm" => false,
"jta" => false,
"connection-url" => "jdbc:postgresql://localhost:5432/postgresdb",
"driver-class" => "org.postgresql.Driver",
"driver-name" => "postgresql-9.4.1208.jar",
"valid-connection-checker-class-name" => "org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLValidConnectionChecker",
"background-validation" => true,
"exception-sorter-class-name" => "org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLExceptionSorter",
"operation" => "add",
"address" => [
("subsystem" => "datasources"),
("data-source" => "PostgresDS")
] }
Response
Internal Server Error {
"outcome" => "failed",
"failure-description" => "WFLYCTL0212: Duplicate resource [
(\"subsystem\" => \"datasources\"),
(\"data-source\" => \"PostgresDS\") ]",
"rolled-back" => true }

How do I use map to merge an array of hashes?

Hoping someone could explain the proper process for this. I have a data structure like this:
[
{
Author => "101",
WK1 => "",
WK10 => "",
WK11 => "",
WK12 => "",
WK13 => "",
WK14 => "X",
WK15 => "",
},
{
Author => "102",
WK1 => "",
WK10 => "",
WK11 => "",
WK12 => "X",
WK13 => "X",
WK14 => "",
WK15 => "",
WK2 => "X",
WK3 => "X",
WK4 => "",
},
{
Author => "101",
WK1 => "",
WK10 => "",
WK11 => "",
WK12 => "",
WK13 => "",
WK14 => "X",
WK15 => "X",
},
]
WK keys may or may not exist. The Author key will always exist, and I’m trying to merge those duplicates. Overwriting values is ok (if defined).
Should I try map (not sure how), or would it be better to create a new hash using the Author as key, pushing the other values into an array? Something like
foreach $x (#$data) {
%new_hash = ...
...
}
scratching head
It sounds like you are starting with something like the following:
my $records = [
{ Author => "101", WK1 => "", WK2 => "X", WK3 => "X" },
{ Author => "101", WK1 => "X", WK2 => "", WK4 => "X" },
{ Author => "102", WK1 => "", WK2 => "", WK3 => "X" },
];
It sounds like you want to produce the following:
my $records_by_author = (
"101" => { Author => "101", WK1 => "X", WK2 => "", WK3 => "X", WK4 => "X" },
"102" => { Author => "102", WK1 => "", WK2 => "", WK3 => "X" },
);
The following will do that:
my %records_by_author;
for my $record (#$records) {
my $author = $record->{Author};
$records_by_author{$author} = {
( $records_by_author{$author} ? %{ $records_by_author{$author} } : () ),
%$record,
};
}
If instead you want the following output:
my $records = [
{ Author => "101", WK1 => "X", WK2 => "", WK3 => "X", WK4 => "X" },
{ Author => "102", WK1 => "", WK2 => "", WK3 => "X" },
];
Just add the following:
$records = [ values(%records_by_author) ];
It you want this output and you want to preserve the original order, let me know.
I suggest merging along the lines of the code below. From each anonymous hash in #$data, grab the author identifier and then update the merged WK values where we never overwrite a non-empty string with an empty one.
You could do the same with map, and that would be a worthwhile exercise. It seems to read much more naturally as nested structural loops.
my %authorwk;
for (my $i = 0; #$data; ++$i) {
local $_ = shift #$data;
die "$0: missing Author in record $i" unless exists $_->{Author};
my $author = $_->{Author};
while (my($wk,$present) = each %$_) {
next unless $wk =~ /^WK/;
$authorwk{$author}{$wk} = $present
if $present || !exists $authorwk{$author}{$wk};
}
}
map is inappropriate here: a series of slice assignments is all that is necessary
It looks like this. Note that the only duplicate author 101 has an identical set of keys in both instances, so I've added key WK7 to the first instance and WK8 to the second so that you can see that they are both added to the result
use strict;
use warnings 'all';
my $data = [
{
Author => 101,
WK7 => "7", WK1 => "", WK10 => "", WK11 => "",
WK12 => "", WK13 => "", WK14 => "X", WK15 => "",
},
{
Author => 102,
WK1 => "", WK10 => "", WK11 => "", WK12 => "X",
WK13 => "X", WK14 => "", WK15 => "", WK2 => "X",
WK3 => "X", WK4 => "",
},
{
Author => 101,
WK8 => "8", WK1 => "", WK10 => "", WK11 => "",
WK12 => "", WK13 => "", WK14 => "X", WK15 => "X",
},
];
my %authors;
for my $item ( #$data ) {
my $author = $item->{Author};
#{ $authors{$author} }{keys %$item} = values %$item;
}
use Data::Dump;
dd \%authors;
output
{
101 => {
Author => 101,
WK1 => "",
WK10 => "",
WK11 => "",
WK12 => "",
WK13 => "",
WK14 => "X",
WK15 => "X",
WK7 => 7,
WK8 => 8,
},
102 => {
Author => 102,
WK1 => "",
WK10 => "",
WK11 => "",
WK12 => "X",
WK13 => "X",
WK14 => "",
WK15 => "",
WK2 => "X",
WK3 => "X",
WK4 => "",
},
}

Calls to update a user return 200 OK but do not update user

Back story:
Migrating from the Provisioning API to the Admin SDK Directory API. Using Perl.
I can successfully obtain a Bearer token, and I can use the token to obtain an individual User Resource and User Resource listing of the entire domain. This all works fine. I have ensured that I'm using the proper scope in the token request (https://www.googleapis.com/auth/admin.directory.user).
Problem:
Calls to update a user are returning 200 OK (expected) but the changes are not being picked up.
Using LWP to PUT the update request. Here's a dump of the LWP object after the request comes back. You can see that I'm getting a 200 OK response and a User Resource object as part of the response. You can also see that the returned User Resource does not reflect the changes I've sent in the request. I have confirmed in the admin console for the domain that the changes were not picked up.
Any help would be appreciated.
'_content' => '{
"kind": "admin#directory#user",
"id": "somenumber",
"etag": "\\"etag\\"",
"primaryEmail": "user#googletestdomain",
"name": {
"givenName": "user",
"familyName": "name",
"fullName": "user name"
},
"isAdmin": false,
"isDelegatedAdmin": false,
"lastLoginTime": "2014-10-02T17:20:02.000Z",
"creationTime": "2010-01-04T22:27:44.000Z",
"agreedToTerms": true,
"suspended": false,
"changePasswordAtNextLogin": false,
"ipWhitelisted": false,
"emails": [
{
"address": "user#googletestdomain",
"primary": true
},
],
"customerId": "C01id",
"orgUnitPath": "/",
"isMailboxSetup": true,
"includeInGlobalAddressList": true
}
'
'_headers' => HTTP::Headers=HASH(0x2031048)
'::std_case' => HASH(0x2031240)
'alternate-protocol' => 'Alternate-Protocol'
'client-date' => 'Client-Date'
'client-peer' => 'Client-Peer'
'client-response-num' => 'Client-Response-Num'
'client-ssl-cert-issuer' => 'Client-SSL-Cert-Issuer'
'client-ssl-cert-subject' => 'Client-SSL-Cert-Subject'
'client-ssl-cipher' => 'Client-SSL-Cipher'
'client-ssl-socket-class' => 'Client-SSL-Socket-Class'
'x-content-type-options' => 'X-Content-Type-Options'
'x-frame-options' => 'X-Frame-Options'
'x-xss-protection' => 'X-XSS-Protection'
'alternate-protocol' => '443:quic,p=0.01'
'cache-control' => 'no-cache, no-store, max-age=0, must-revalidate'
'client-date' => 'Mon, 27 Oct 2014 17:48:14 GMT'
'client-peer' => '173.194.79.95:443'
'client-response-num' => 1
'client-ssl-cert-issuer' => '/C=US/O=Google Inc/CN=Google Internet Authority G2'
'client-ssl-cert-subject' => '/C=US/ST=California/L=Mountain View/O=Google Inc/CN=*.googleapis.com'
'client-ssl-cipher' => 'ECDHE-RSA-AES128-GCM-SHA256'
'client-ssl-socket-class' => 'IO::Socket::SSL'
'connection' => 'close'
'content-type' => 'application/json; charset=UTF-8'
'date' => 'Mon, 27 Oct 2014 17:48:14 GMT'
'etag' => '"etag"'
'expires' => 'Fri, 01 Jan 1990 00:00:00 GMT'
'pragma' => 'no-cache'
'server' => 'GSE'
'vary' => ARRAY(0x20311b0)
0 'Origin'
1 'Referer'
2 'X-Origin'
'x-content-type-options' => 'nosniff'
'x-frame-options' => 'SAMEORIGIN'
'x-xss-protection' => '1; mode=block'
'_msg' => 'OK'
'_protocol' => 'HTTP/1.1'
'_rc' => 200
'_request' => HTTP::Request=HASH(0x1f5dc90)
'_content' => '{"name":{"givenName":"BBB","familyName":"BBB"}}'
'_headers' => HTTP::Headers=HASH(0x224fa08)
'::std_case' => HASH(0x1f28c90)
'if-ssl-cert-subject' => 'If-SSL-Cert-Subject'
'authorization' => 'Bearer mytokenhere'
'content-length' => 47
'user-agent' => 'libwww-perl/6.05'
'_method' => 'PUT'
'_uri' => URI::https=SCALAR(0x1cbc8b8)
-> 'https://www.googleapis.com/admin/directory/v1/users/user#googletestdomain'
'_uri_canonical' => URI::https=SCALAR(0x1cbc8b8)
-> REUSED_ADDRESS
Here's a sample of the code used:
#!/usr/bin/perl -w
use JSON;
use LWP::UserAgent;
my $auth_token = 'myauthtoken';
my $changes = {
'name' => {
'givenName' => 'BBB',
},
};
my $json = new JSON;
my $ur = $json->encode($changes,{utf8 => 1});
my $url = 'https://www.googleapis.com/admin/directory/v1/users/user#googletestdomain';
my $ua = LWP::UserAgent->new(timeout => 30);
my $res = $ua->put($url,
'Authorization' => 'Bearer '.$auth_token,
'Content' => $ur,
);
Setting the Content-Type header in the PUT request to application/json fixed the problem.