Convert EWS StoreId (FolderID) to Graph API restId using translateExchangeIds? - powershell

Is there any possible way to convert Get-MailboxFolderStatistics returned folderId (storeId) to Microsoft Graph API restId?
I've tried to use users/{userId}/translateExchangeIds endpoint but didn't get any useful results.
Does users/{userId}/translateExchangeIds endpoint support translating storeId to restId?
Is there any other way to translate this id's ?

You need to convert storeId to entryId by yourself and then use users/{userId}/translateExchangeIds to convert entryId to restId.
For converting storeId to entryId convert folderId from base64 string to hex string. Then parse entryId hex string and encode entryId hex string to entryId.
$folderId = "LgAAAADMaqNx7y87SYAkFzRBDsNkAQDBGxV/99wUQoEROcv7tgfjAAAWrAyWAAAB"
# convert from base64 to bytes
$folderIdBytes = [Convert]::FromBase64String($folderId)
# convert byte array to string, remove '-' and ignore first byte
$folderIdHexString = [System.BitConverter]::ToString($folderIdBytes).Replace('-','')
$folderIdHexStringLength = $folderIdHexString.Length
# get hex entry id string by removing first and last byte
$entryIdHexString = $folderIdHexString.SubString(2,($folderIdHexStringLength-4))
# convert to byte array - two chars represents one byte
$entryIdBytes = [byte[]]::new($entryIdHexString.Length / 2)
For($i=0; $i -lt $entryIdHexString.Length; $i+=2){
$entryIdTwoChars = $entryIdHexString.Substring($i, 2)
$entryIdBytes[$i/2] = [convert]::ToByte($entryIdTwoChars, 16)
}
# convert bytes to base64 string
$entryIdBase64 = [Convert]::ToBase64String($entryIdBytes)
# count how many '=' contains base64 entry id
$equalCharCount = $entryIdBase64.Length - $entryIdBase64.Replace('=','').Length
# trim '=', replace '/' with '-', replace '+' with '_' and add number of '=' at the end
$entryId = $entryIdBase64.TrimEnd('=').Replace('/','_').Replace('+','-')+$equalCharCount
folderId with value LgAAAADMaqNx7y87SYAkFzRBDsNkAQDBGxV/99wUQoEROcv7tgfjAAAWrAyWAAAB is converted to entryId AAAAAMxqo3HvLztJgCQXNEEOw2QBAMEbFX_33BRCgRE5y_u2B-MAABasDJYAAA2
Then use the converted entryId in the call
{
"inputIds" : [
"AAAAAMxqo3HvLztJgCQXNEEOw2QBAMEbFX_33BRCgRE5y_u2B-MAABasDJYAAA2"
],
"sourceIdType": "entryId",
"targetIdType": "restId"
}
Response
{
"#odata.context": "https://graph.microsoft.com/v1.0/$metadata#Collection(microsoft.graph.convertIdResult)",
"value": [
{
"sourceId": "AAAAAMxqo3HvLztJgCQXNEEOw2QBAMEbFX_33BRCgRE5y_u2B-MAABasDJYAAA2",
"targetId": "AAMkAGRkZTFiMDQxLWYzNDgtNGQ3ZS05Y2U3LWU1NWJhMTM5YTgwMAAuAAAAAADMaqNx7y87SYAkFzRBDsNkAQDBGxV-99wUQoEROcv7tgfjAAAWrAyWAAA="
}
]
}
Resources:
URL-safe

Related

How to translate/convert a Postgresql IN query as variable and send the variable using Golang and SQLC with a proper struct?

First of all, if someone has a better sentence for my question, feel free to comment.
I want to translate this query into Golang
SELECT
mou."id",
mou."name",
mou.description,
mou.img_url,
um.favorite
FROM
majors_of_universities mou
JOIN field_of_studies fos ON mou.field_of_studies_id = fos."id"
JOIN univ_major um ON mou."id" = um.majors_of_universities_id
WHERE
mou."name" ILIKE '%%'
AND fos."name" IN ( 'IT & Software', 'Analisis Data & Statistik' )
ORDER BY
mou."name"
LIMIT 99 OFFSET 0;
This query works well, btw. I'm using sqlc as a generator and by it rules (CMIIW), I changed...
'%%' to $1
'IT & Software', 'Analisis Data & Statistik' to $2
99 to $3
0 to $4
so it become a variable.
little did I know, the $2 generated into a string data type. what I want is it generated into an array of string data type, because I found out that Golang can translate an array of string from ["1", "2", "3"] into '1', '2', '3' , just like what I want to input inside postgres IN parenthesis.
in Golang side, I made a custom struct like this
type SearchMajorReq struct {
Name string `json:"name"`
FieldOfStudies []string `json:"field_of_studies"`
Limit int32 `json:"limit"`
Page int32 `json:"page"`
}
in hope that this is the correct data type to send a JSON req body like this
{
"name":"",
"field_of_studies": ["1", "2", "3"],
"limit": 10,
"page": 1
}
but it doesn't works. I have an error in FieldOfStudies part.
How can I solve this?

Add or Update Property on JSON object with Mapping Template (AWS API Gateway)

In my AWS APIGW Rest API, I'm trying to add and/or update a property on the request body (JSON) that is submitted. Every example I could find deals with constructing a new JSON object, not updating the existing one.
In the Integration Request Mapping Template, my incoming data body looks like this. If it's a new object getting posted, it won't have an ID. If it's an existing object, it will have an ID. The goal here is to ALWAYS ensure it has an ID by either getting the existing one and setting it back to the JSON object or adding a new one.
// New object getting added, No ID
{
"first_name" : "Toby",
"last_name" : "Keith"
}
// Existing object getting updated, Has ID
{
"id" : "abcdef"
"first_name" : "Toby",
"last_name" : "Keith"
}
My Integration Request Mapping Template looks like this:
## Do we have an existing id? (this is correctly pulling the existing ID)
#set( $id = $input.path('$.id') )
## If no ID, create one using the RequestID. (This is also working)
#if ( $id == "" ) #set( $id = $context.requestId ) #end
## Get the entire json request string as json object
#set( $json_obj = $input.json('$') )
## Overwrite the ID on the json body to make sure we always have one
## [HERE IS THE PROBLEM]
## This isn't setting the id back on the $json_obj
#set( $input.path('$.id') = "$id" )
{
"data": "$util.escapeJavaScript($json_obj).replaceAll("\\'","'")"
}
I want the value for "data" above to be a JSON string which includes an id as well as first_name and last_name.
I've tried numerous variations on setting the property, but no luck as of yet. None of these are working.
// Tries to update the JSON string I think, not the $json_obj
#set( $input.path('$.id') = "$id" )
// These cause error (because of quotes?)
#set( "$json_obj.id" = "BBBB" )
#set( '$json_obj.id' = "CCCC" )
// Doesn't work
#set( $input.path("$json_obj.id") = "DDDD" )
#set( $json_obj.id = "EEEE" )
As a plan B, I could break the $json_obj into key/value pairs and loop over them checking for an ID and either add or update it. Essentially building out a new JSON object, but that seems like the less preferable way than setting the property directly.
Does anyone know how to add/update a property on a JSON object with mapping templates?
I found the issue. This line is NOT converting the body payload into a JSON object like I thought. After more careful reading of the documentation, I see that it examines a JSON path expression and returns a matching JSON string.
#set( $json_obj = $input.json('$') )
To convert it into an object, I needed this syntax:
#set( $json_obj = $util.parseJson($input.json('$')) )
The corrected mapping template now looks like this
## Do we have an existing id? (this is correctly pulling the existing ID)
#set( $id = $input.path('$.id') )
## If no ID, create one using the RequestID. (This is also working)
#if ( $id == "" ) #set( $id = $context.requestId ) #end
## Get the entire json request string as json object
#set( $json_obj = $util.parseJson($input.json('$')) )
## Overwrite the ID on the json body to make sure we always have one
## This isn't setting the id back on the $json_obj
#set( $input.path('$.id') = "$id" )
{
"data": "$util.escapeJavaScript($json_obj).replaceAll("\\'","'")"
}
However, this leads to a secondary problem of now converting the JSON object back to a properly formatted JSON String. If I print out the JSON object, I get unquoted strings for some reason.
# Print out $json_obj
$page_obj
Results in
{first_name=Toby, last_name=Keith}
I'll open a new question for that issue if I am unable to solve it.
2021/11/30 UPDATE:
It's been a while since I worked on this project but for completeness, I ended up being able to set the ID on the input before pulling the object into JSON.
## Pull the existing ID
#set( $id = $input.path('$').id )
## If empty, assign new ID
#if ( $id == "" ) #set ( $id = $context.requestId ) #end
## Assign the ID to the input
#set( $input.path('$').id = $id )
## Now pull the data out as an object
#set( $data = $input.json('$') )
The real difference is path('$.id') is now path('$').id to properly assign the id to the input before extracting the input into a JSON object.

Prevent Json.NET from interpreting a string as a date

I have some attributes returned from a rest service that are served as an array of name-value pairs.
In some cases the value is a date expressed in universal sortable format:
{
"name": "Modification-Date",
"value": "2017-11-13T15:15:13.968Z"
}
When it gets parsed by the deserialiser, the value is identified as a date but given that the object the pair is deserialised into has type string for both name and value, the date is then converted to string and it loses precision: "13/11/2017 15:15:13"
This is easily visible by using a converter for the NameValue type.
if (reader.TokenType == JsonToken.StartObject)
{
var item = JObject.Load(reader);
return new NameValueFacet()
{
Name = item["name"].Value<string>(),
Value = item["value"].Value<string>()
};
}
item["value"].Type shows the type is Date.
How do I get to have Json.NET leave it as a string, "unparsed"?
You can try with Newtonsoft. See below.
JsonConvert.DeserializeObject<your_object>(your_json, new IsoDateTimeConverter{ DateTimeFormat = "dd/MM/yyy" });

How to write fields dynamically to a csv file using a tJava

I'm using Talend to extract database field names from the table and write to a csv them after replacing the "_" in field names with " ". I want to have these values against the actual headers.
eg:
|First_Name|Last_Name|
|----------|---------|
|First Name|Last Name|
My job looks similar to following.
Code in tJavaRow is as follows:
for (java.lang.reflect.Field field:
input_row.getClass().getDeclaredFields()) {
String fieldName = field.getName();
String newFieldName = null;
newFieldName = fieldName.replaceAll("_", " ");
context.columnName = newFieldName;
System.out.println("Field name is " + context.columnName );
}
How can I get the value of this context variable for each field in csv file? If I directly use it in the tmap it will only have name of the last field as value.
tmap I had between tJava and the tFileOutputDelimited.
you cannot change schema as they are treated as declared variables in java code which is generated in backend.
your schema "|First_Name|Last_Name|" is converted into as below:
String First_Name =null;
String Last_Name = null;
So you cannot change those schema column names on fly.
But you can create a record from the column names which you are retrieving from database by using the delimiter you want(lets take comma)
for (java.lang.reflect.Field field : input_row.getClass().getDeclaredFields()) {
String fieldName = field.getName();
context.columnName = context.columnName + "," + fieldName.replaceAll("_", " ");
}
And now, before writing your data into a csv file, write this header record in context.columnName into that csv file.
After writing the header record, append your data to that file by checking "Append" check box in tFileOutputDelimitted.

Basic issue retrieve array or string from file using Storable retrieve

The problem is very simple but I can't seem to find it:
I store a $string to a $filename:
store [$tempstring], $filename2[$m];
I then try to retrieve it:
my $tempinput = retrieve ($filename2[$m]);
I believe I'm just getting the reference, not the string?
Can I use a command to convert the data back to the original string?
my $ref = [ $tempstring ];
creates an array, assigns $tempstring to it (placing it in the first element), then returns a reference to that array.
So if you want the string back, you need to get the value of the first element of the referenced array.
$ref->[0]
If you had done
my $ref = \$tempstring;
instead of needlessly creating an array, you'd simply do
$$ref
to get the string.