How do i convert a custom object to the bytes data type and back again? - python-3.x

I am trying to follow the websocket tutorial that can be found here in the readme of this python github repo:
https://github.com/aaugustin/websockets
For my use case, I want the client to not pass a string to the websocket server but rather an object. When I tried replacing the generic "Hello World!" parameter that the client sends to the server though I get the following error:
TypeError: data must be bytes or str
Ok, makes sense. Obviously websocket requires a string or a bytes object to be passed from client to server. My question is how do i easily convert a generic object of some custom class I've created to the bytes/string type using the best practice problem. Obviously, I would also like to be able to convert the object back from the bytes class to the original class type I have declared.
When searching I couldn't find anything talking about how to do this (only how to do this for strings) and tried hard casting by passing my object into the bytes() method but this threw an error.
Thoughts?

I am an idiot. Converting the object to JSON works.

Related

What is the proper way of parsing query parameters using nestJS?

Let's say we make the following GET API call to our REST API running with NestJS (and fastify):
http://localhost:5000/api/test?arrayParam=["abc","def"]&anotherParam="value"
Without parsing anything, the query object on the backend looks like this:
{
arrayParam: '["abc","def"]',
anotherParam: '"value"'
}
We can see that parameter values are strings, but in the case of arrayParam we would obviously like to work with the actual array.
I come from an expressJS background, and coming from there, there are a couple of approaches. First would be just using a JSON parser middleware, like body-parser. Or just using JSON.parse().
But what is the "proper", NestJS approach? I thought about using type decorators defined in a DTO, and assumed they would be automatically parsed to the type that I defined. But that doesn't work like I assumed it would.
I defined it like this:
#IsOptional()
#IsArray()
arrayParam?: string[];
But validation fails, since arrayParam is a string and not an array. So I assume this is not the correct approach
You are sending it incorrectly
http://localhost:5000/api/test?arrayParam[]=abc&arrayParam[]=def&anotherParam

How to get the type/interface of an unknown imported function in TypeScript

I've heard that it is bad practice to set an expected type of a variable to any.
However, I don't understand, how I can get a return type of an imported function, which returns an object, which is only used in the library.
As an example, if I would like to use the crypto.createCipheriv() function provided by Node.js, which returns a Cipher object as stated in the docs, I would not known how to give a variable this type.
const variable: ReturnType<crypto.createCipheriv>;

How do I convert an struct of type Data to an object of type CNGroup?

I'm having trouble converting a CNGroup object to a Data object and back to a CNGroup object. I decided to start rethinking the problem again. Somewhere along the way I decided that I should use the Data class to save a CNGroup object to CloudKit. I also learned that the field type to use in my CKRecord object would be of the type bytes.
Am I correct so far?
I am able to convert a CNGroup object to a Data object and back again unless I store the Data object in CloudKit and then retrieve it before I convert the Data object back to a CNGroup object, in which case I get an error when I try to access the pointee property of the typed pointer. That would be an UnsafeBufferPointer, an UnsafeMutableBufferPointer, or an UnsafePointer.
I've tried a lot of different code using different ways. It is impractical to put so much code in my post. I have used the copyBytes method and the withUnsafeBytes method of the Data object.
There is one simple code, and that is when I converted the CNGroup object to a Data object:
func convertCNGroupToData(fromCNGroup group: inout CNGroup) -> Data {
return Data(bytes: &group, count: MemoryLayout.size(ofValue: group))
}
I am looking for a simple way to do what I want. I am relooking at Apple's documentation of Data and NSData.
I am not able to be more specific with this question. I appreciate any effort to help me with this.

Model Binding Issue in Azure Function After Switching to Azure.Storage.Queues

I use Azure Functions with Queue triggers in my backend and up to this point, I'd been using the Microsoft.WindowsAzure.Storage package to handle all Azure Storage operations i.e. queues, blobs, etc. With this package, I'd simply send a MyQueueRequest object to my queue and everything worked fine.
Because the Microsoft.WindowsAzure.Storage package has been deprecated, I swithched to Azure.Storage.Queue and my Azure Function started throwing the following error:
Microsoft.Azure.WebJobs.Host: Exception binding parameter 'message'.
System.Private.CoreLib: The input is not a valid Base-64 string as it
contains a non-base 64 character, more than two padding characters, or
an illegal character among the padding characters.
I've found this article that suggests that the new library requires JSON objects to be encoded in Base64 (https://briancaos.wordpress.com/2020/10/16/sending-json-with-net-core-queueclient-sendmessageasync/).
Up to this point, I actually never even serialized my MyQueueRequest object to JSON. The model binder took care of that for me automatically.
Does this mean, going forward, before sending the message to my queue, I need to first serialize MyQueueRequest object and then Base64 encode it and then reverse the process in my Azure Functions?
Yes, for this new package you will need to do this. I ran into the same problem when trying to add POCOs to a queue. I use similar code to the article you cite.
I use the following code to handle this:
await queue.SendMessageAsync(Base64Encode(JsonSerializer.Serialize(myObject)));
Where Base64Encode is:
private string Base64Encode(string plainText)
{
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
return Convert.ToBase64String(plainTextBytes);
}
Be sure it's also UTF-8. I'm using System.Text.Json for this example, but Newtonsoft would work just as well.

serializeBinary() not found using google-protobuf on nodejs

I could really use some help. I am trying to follow along on the google-protobuf example:
https://www.npmjs.com/package/google-protobuf
However, when I try this line of code:
// Serializes to a UInt8Array.
var bytes = message.serializeBinary();
I get a Type Error saying that serializeBinary is not a function.
Is there a different function, or something I am missing? I could really use the help.
Best Regards,
Mike
Is the problem solved now?
I had the same problem, until I realized maybe I wasn't creating a real proto object and that is why it cannot recognize serializeBinary() as a function.
True enough, my message was actually a JavaScript object.
So I had to build a proto object by following this article.
https://ednsquare.com/story/working-with-protocol-buffers-in-javascript------MaDIJH
Basically, you have to initialize your proto object and then add properties to it through the set methods. In this way, the proto object is properly built. And therefore, should have the serializeBinary() function.

Resources