Reputation: 876
I have to map response from 3rd party API to object and save it to the database. I have to keep the ID from the response so I can use it later for the next API call. How ever the 3rd party using some random pattern string while I'm using UUID for the database index. So my solution is a bit dumb I keep both ID in the database like this.
{
id : "ppkk1231whatupeverybodyhohohaharandomrandom", <- ID from the API response,
uuid : "xxxx-xxxx-xxxx-xxxx", <- My generated UUID
name: "patrick"
}
And it's a bit hard to use. Whenever I need to make a request to 3rd party I have to use my UUID to search for the 3rd party ID and make a request like this
public void updateCustomerName(customerId: UUID, updateName: String) {
customerRepository.findById(customerId)
.flatMap(x -> { 3rdpartyService.updateCustomer(x.id, updateName) });
}
So my idea is, it would be nice if I can create UUID from random string (the id that I got from the response) and be able to convert it back. So I can use it like this.
public void updateCustomerName(customerId: UUID, updateName: String) {
// no need for the database query boom!
3rdpartyService.updateCustomer(customerId.convertItBack(), updateName);
}
Upvotes: 0
Views: 1622
Reputation: 2270
UUIDs are not generally reversible. There can be n number of strings that can generate a particular UUID.
What you want is some encryption/decryption mechanism. Let's say you use AES-256 symmetric encryption. You will just have to store a secret using which you can encrypt/decrypt the id. But in case the key gets compromised, you would be in a mess. If you change your key, all existing data will get corrupted. If you don't change, you have compromised all your data outside.
The first solution you mentioned here of having a database mapping between id & UUID is not a dumb one. It is a good solution here.
All the things I mentioned above was considering that you want your ids to be internal & hidden from outside. If it is not the case & you are fine with exposing you ids outside, you can think of BASE-64 encoding your ids & use them in third party API.
Upvotes: 1