Decode a serialized BrokeredMessage via Rest on non-Windows - azure

I'm building a Xamarin client that reads messages from the Azure Service Bus.
My REST code can successfully pull a message off the Service Bus, but what I'm getting back appears to be binary (as in non-Text...I know it's all binary ;) )
This is the test code on Windows:
byte[] response = webClient.UploadData(fullAddress, "DELETE", new byte[0]);
MemoryStream ms = new MemoryStream(response);
BrokeredMessage bm = new BrokeredMessage(ms);
responseStr = bm.GetBody<string>();
My problem is on Xamarin/Mono, I don't have a BrokeredMessage.
So my question is how to I de-serialize a BrokeredMessage by hand?
Here's the first few bytes of the response variable looks like:
40 06 73 74 72 69 6e 67 08 33 68 74 74 70 3a 2f 2f 73 63 68
All the examples, I've found, say that I should be getting back XML....it 'almost' looks like XML but the 06 and the 08 are throwing me off.
I'm sure that I'm missing something simple, but I can't find it.
Any guidance would be welcome.

I figured it out so I'm posting the answer just in case someone else runs into the same problem.
response = webClient.UploadData(fullAddress, "DELETE", new byte[0]);
responseStr = System.Text.Encoding.UTF8.GetString(response);
Dictionary<string, object> result = new Dictionary<string, object>();
foreach (var headerKey in webClient.ResponseHeaders.AllKeys)
result.Add(headerKey, webClient.ResponseHeaders[headerKey]);
MemoryStream ms = new MemoryStream(response);
DataContractSerializer serializer = new DataContractSerializer(typeof(string));
XmlDictionaryReader reader = XmlDictionaryReader.CreateBinaryReader(ms, XmlDictionaryReaderQuotas.Max);
object deserializedBody = serializer.ReadObject(reader);
responseStr = (string)deserializedBody;
result.Add("body", responseStr);
The BrokeredMessage properties are stored in the ResponseHeaders

Related

Node MySQL 5.7 latin1_swedish_ci using mysql2

I'm getting a special characters from a latin1_swedish_ci database. It contains a huge amount of data, and migration is not a option :(. The new app has all its files encoding are utf8, and we are looking for conversion solution, from latin1 to uft8. The charset on mysql2, plus set names, etc.. I also try any other suggestions using iconv (version dependency) from internet that I could not make them work, So I ended up developing some code that seems works and fixes the problem.
However, it is very obvious...do you see something wrong in the code?
let data = JSON.stringify(rows); // list of mysql objects swedish encoding to string
data = Buffer.from(data, "latin1"); // to bynary
data = data.toString("utf8"); // to utf8
rows = JSON.parse(data); // to json
String example before apply the code below:
Distributeurs: N° 5/6
Thanks!
OK, (warning: my node skills are low), but this code will convert the word ångström (first from UTF8 to latin1 and then) from latin1 to UTF8:
const buffer = require('buffer');
const latin1Buffer = buffer.transcode(Buffer.from("ångström"), "utf8", "latin1");
const latin1String = latin1Buffer.toString("latin1");
let rows = latin1String;
console.log("Buffer latin1 encoding: ", latin1Buffer);
console.log("String in latin1:", rows);
console.log("");
rows = latin1String;
let data2 = buffer.transcode(Buffer.from(rows, "latin1"), "latin1", "utf8" );
console.log("Buffer in UTF8:", data2);
console.log("String in UTF8: ", data2.toString());
output:
Buffer latin1 encoding: <Buffer e5 6e 67 73 74 72 f6 6d>
String in latin1: ångström
Buffer in UTF8: <Buffer c3 a5 6e 67 73 74 72 c3 b6 6d>
String in UTF8: ångström

unpacking serial data "01 04 04 3f 9f 97 00 a9 8e"

I got lt-1400(water level transmitter), output(RS485), I send this(b'\x01\x04\x00\x02\x00\x02\xd0\x0b\x00\x00\x00') to USB to TTL max485 and I resiveing tis kind of data (b'\x01\x04\x04\x3f\x9f\xae\x80\xba\x7e').how can I translate it.
receiving and sending data I am using python script||||||||
`
import serial
import time # import the module
while True:
ComPort = serial.Serial('COM11') # open COM3
ComPort.baudrate = 9600 # set Baud rate to 9600
ComPort.bytesize = 8 # Number of data bits = 8
ComPort.parity = 'N' # No parity
ComPort.stopbits = 1 # Number of Stop bits = 1
data = bytearray(b'\x01\x04\x00\x02\x00\x02\xd0\x0b\x00\x00\x00')
No = ComPort.write(data)
print (ComPort.isOpen())
#print(data) # print the data
dataIn = ComPort.read(8) # Wait and read data
print(dataIn)
ComPort.close() # print the received data
#time.sleep(2)
ComPort.close() # Close the Com port `
||||||||||||||||||||||
translating data python script|||||||
`
import struct
data = bytearray(b'\x01\x04\x04\x3f\x9f\xae\x80\xba\x7e')
number = struct.unpack('hhl', data)
number=str(number)
print(number)
#print(num2)
`
however, I have everything still I don't get the necessary data please help me If you can I would be happy

Saving hex data to file in NodeJS

Say I've got a string
"020000009B020000C0060000"
and I want to turn it into a string such that if it was saved as a file and opened it in a hex editor it would display the same hex as I put in.
How do I do this?
I've never had to use buffers or anything, but in trying to do this I've seemingly had to, and I don't really know what I'm doing.
(or whether I should be using buffers at all- I've been searching for hours and all I can find are answers that use buffers so I've just taken their examples and yet nothing is working)
I turned the hex string into a buffer
function hexToBuffer(hex) {
let typedArray = new Uint8Array(hex.match(/[\da-f]{2}/gi).map(function (h) {
return parseInt(h, 16)
}))
return typedArray
}
and this seemed to work because logging hexToBuffer("020000009B020000C0060000").buffer what it returns gave me this:
ArrayBuffer {
[Uint8Contents]: <02 00 00 00 9b 02 00 00 c0 06 00 00>,
byteLength: 12
}
which has the same hex as I put in, so it seems to be working fine,
then to make the array buffer into a string I did this.
let dataView = new DataView(buffer);
let decoder = new TextDecoder('utf-8');
let string = decoder.decode(dataView)
just to test that it worked, I saved it to a file.
fs.writeFileSync(__dirname+'/test.txt', string)
Opening test.txt in a hex editor shows different data:
02000000EFBFBD020000EFBFBD060000
If I instead do
fs.writeFileSync(__dirname+'/test.txt', hexToBuffer("020000009B020000C0060000"))
then I get the correct data- but then if I read the file with fs and then add to it it's once again not the same values.
let test = fs.readFileSync(__dirname+'/test.txt', 'utf8)
let example2 = test+'example'
fs.writeFileSync(__dirname+'/test.txt', example2)
now test.txt begins with 02000000EFBFBD020000EFBFBD060000 instead of 020000009B020000C0060000. What do I do?
First of all, you can use the Buffer.from(string[, encoding]) method to create a Buffer from a string whilst also specifying the encoding - in your case it will be "hex":
const b1 = Buffer.from("020000009B020000C0060000", "hex")
Now save it to a file:
fs.writeFileSync(path.resolve('./test'), b1)
Then we can check that the file contains the same hex values as in the string by using xxd on the command line:
$ xxd test
0200 0000 9b02 0000 c006 0000
Looking good!
Now we can read the file back into a buffer as well, making sure to tell it the encoding of the file is "hex" again:
const b2 = fs.readFileSync(path.resolve("./test"), "hex")
Finally, turn it back into a string again with the Buffer.toString() method:
console.log(b2.toString()) // => "020000009b020000c0060000"

How do I apply a Shared Advanced Segment to my data using a service account?

I am using the Google Analytics Reporting API v4 with Python3 to extract data from a Google Analytics dashboard that is shared to me by my boss. I am using a private key and a service account.
What I want to do: Use a Custom/Advanced Segment that was created by the Admin of the Google Analytics dashboard. This admin has changed the Advanced Segment to a collaborative segment, so I can access the segment on my account.
I am able to extract data for predefined segments like (gaid::-3). But this is only the test case. The problem arises when I try to use a Custom/Advanced Segment instead of a predefined Google segment.
Code:
"""Hello Analytics Reporting API V4."""
from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials
SCOPES = ['https://www.googleapis.com/auth/analytics.readonly']
KEY_FILE_LOCATION = 'fakefilelocation.json'
VIEW_ID = '111111'
def initialize_analyticsreporting():
"""Initializes an Analytics Reporting API V4 service object.
Returns:
An authorized Analytics Reporting API V4 service object.
"""
credentials = ServiceAccountCredentials.from_json_keyfile_name(
KEY_FILE_LOCATION, SCOPES)
# Build the service object.
analytics = build('analyticsreporting', 'v4', credentials=credentials)
return analytics
def get_report(analytics):
"""Queries the Analytics Reporting API V4.
Args:
analytics: An authorized Analytics Reporting API V4 service object.
Returns:
The Analytics Reporting API V4 response.
"""
return analytics.reports().batchGet(
body={
'reportRequests': [
{
"viewId":VIEW_ID,
"dimensions":[{"name": "ga:segment"},{"name":"ga:dimension4"}],
"dateRanges":[{"startDate":"2017-10-10","endDate":"2017-10-10"}],
"metrics":[{"expression":"ga:sessions","alias":"sessions"}],
"segments":[{"segmentId": "gaid::1111111111111"}]
}]
}
).execute()
def print_response(response):
"""Parses and prints the Analytics Reporting API V4 response.
Args:
response: An Analytics Reporting API V4 response.
"""
for report in response.get('reports', []):
columnHeader = report.get('columnHeader', {})
dimensionHeaders = columnHeader.get('dimensions', [])
metricHeaders = columnHeader.get('metricHeader', {}).get('metricHeaderEntries', [])
for row in report.get('data', {}).get('rows', []):
dimensions = row.get('dimensions', [])
dateRangeValues = row.get('metrics', [])
for header, dimension in zip(dimensionHeaders, dimensions):
print(header + ': ' + dimension)
for i, values in enumerate(dateRangeValues):
print('Date range: ' + str(i))
for metricHeader, value in zip(metricHeaders, values.get('values')):
print(metricHeader.get('name') + ': ' + value)
def main():
analytics = initialize_analyticsreporting()
response = get_report(analytics)
print_response(response)
if __name__ == '__main__':
main()
But this results in an error:
---------------------------------------------------------------------------
HttpError Traceback (most recent call last)
<ipython-input-10-f6bd25c075a6> in <module>()
76
77 if __name__ == '__main__':
---> 78 main()
<ipython-input-10-f6bd25c075a6> in main()
72 def main():
73 analytics = initialize_analyticsreporting()
---> 74 response = get_report(analytics)
75 print_response(response)
76
<ipython-input-10-f6bd25c075a6> in get_report(analytics)
41 "dateRanges":[{"startDate":"2017-10-10","endDate":"2017-10-10"}],
42 "metrics":[{"expression":"ga:sessions","alias":"sessions"}],
---> 43 "segments":[{"segmentId": "gaid::1111111111111"}]
44 }]
45 }
C:\ProgramData\Anaconda3\lib\site-packages\oauth2client\_helpers.py in positional_wrapper(*args, **kwargs)
131 elif positional_parameters_enforcement == POSITIONAL_WARNING:
132 logger.warning(message)
--> 133 return wrapped(*args, **kwargs)
134 return positional_wrapper
135
C:\ProgramData\Anaconda3\lib\site-packages\googleapiclient\http.py in execute(self, http, num_retries)
840 callback(resp)
841 if resp.status >= 300:
--> 842 raise HttpError(resp, content, uri=self.uri)
843 return self.postproc(resp, content)
844
HttpError: <HttpError 403 when requesting https://analyticsreporting.googleapis.com/v4/reports:batchGet?alt=json returned "User does not have sufficient permissions for this advanced segment.">
I'm pretty sure that this is not the result of a bad segment ID, because I tried a fake one ("1111111111111") and it gave me a different error.
To Summarize
I want to use Google Analytics Reporting API v4 to extract and print data that has been sorted by a Custom/Advanced Segment which is owned by the Admin user, not by me. This way, if the Admin changes the settings on the Custom Segment, the changes will flow through to my data extraction naturally.
If you change the visibility setting of the segment to "Collaborators and I can apply/edit Segment in this View" the service account should be able to see it.
In the Google Analytics web interface, edit the segment (admin > view > segments > pick the appropriate segment). In the top right, where it says "Segment is visible and shared in current View," click "Change" and apply the new setting.
Google support link

WAV file not working. How can I fix it?

I have this file which came form a cellphone company's call center. It's supposed to be a recording of a conversation I had with them. I tried to open it using Windows Media Player, VLC Media Player, and Audacity. But it didn't work.
Do you have any idea how to fix this?
Here's all the text that came in the email with the file:
If you are having difficulty playing the attached multimedia file(s) using Windows Media Player, contact your technical support representative
------------ Segment-related information -------------
Contact ID = 9120617573350001032
Start Time = 14/07/2014 10:02:13
Local Start Time = 14/07/2014 10:02:13
End Time = 14/07/2014 10:16:11
Local End Time = 14/07/2014 10:16:11
Extension = 11134
Agent = שלו, מלי
PBX ID = 11134
Duration = 00:13:58
Dialed From (ANI) = 11134
Dialed To (DNIS) = 0545920155
Remarked On =
Remarked By =
Remark =
Acquisition Module = 17
Channel = 286
Screen Acquisition Module = 14
With Screens = 1
Direction = 2
Switch Call Id = 2144221398988751
WrapUp Time = 00:00:34
Switch Name = Cosmocom2
Custom Data 1 = 1
Custom Data 2 = 10.91.12.53
Custom Data 3 = 11134:10.19.221.155:6815 Custom Data 4 = 2144221398988751000 Custom Data 5 = שירות חול Custom Data 6 = 0545920155 Custom Data 7 = XEN16113 Custom Data 8 = Custom Data 9 = חול חבילות Custom Data 10 = 4806591 Custom Data 11 = Custom Data 12 = Custom Data 13 = Custom Data 14 = Custom Data 15 = Custom Data 16 = Custom Data 17 = Custom Data 18 = Custom Data 19 = Custom Data 20 = Custom Data 21 = Custom Data 22 = Custom Data 23 = Custom Data 24 = Custom Data 25 = Custom Data 26 = Custom Data 27 = Custom Data 28 = Custom Data 29 = Custom Data 30 = Custom Data 31 = Custom Data 32 = Custom Data 33 = Custom Data 34 = Custom Data 35 = Custom Data 36 = Custom Data 37 = Custom Data 38 = Custom Data 39 = Custom Data 40 = Custom Data 41 = Custom Data 42 = Custom Data 43 = Custom Data 44 = Custom Data 45 = Custom Data 46 = Custom Data 47 = Custom Data 48 = Custom Data 49 = Custom Data 50 = Custom Data 51 = Custom Data 52 = Custom Data 53 = Custom Data 54 = Custom Data 55 = Custom Data 56 = Custom Data 57 = Custom Data 58 = Custom Data 59 = Custom Data 60 = Custom Data 61 = Custom Data 62 = Custom Data 63 = Custom Data 64 = Custom Data 65 = Custom Data 66 = Custom Data 67 = Custom Data 68 = Custom Data 69 = Custom Data 70 = Custom Data 71 = Custom Data 72 = Custom Data 73 = Custom Data 74 = Attached file name: AudioOnly_Site_01_AudioModule_17_AudioChannel_286_14-Jul-2014_07.02.13.500.WAV
===
And here's a link to the file itself: https://drive.google.com/file/d/0B1r5xyHzGXL9Rl8td2dQdXFlU3puVWZ5aXFCcUZ4V3h2UmE4/view?usp=sharing
According to the fmt chunk in the WAV header, the audio data is encoded using a format named WAVE_FORMAT_VOICEAGE_AMR_WB from VoiceAge Corporation. That is not a commonly used format on desktops, but is more commonly used in mobile devices. You need to install an audio codec that supports that format, or else you should ask the call center to convert their WAV file into a more common desktop format, like PCM or GSM 6.10.

Resources