C
C#11mo ago
Vortac

Interface Design Question

I have an interface IAuthenticatable with one method: Task<Tokens> GetAccessTokensAsync() that I'd like to use for two different API authentication classes. However, the two authenticated API responses vary, so I'm just wondering what I should do. Use separete interfaces for each API?
9 Replies
Omnissiah
Omnissiah11mo ago
vary how?
Denis
Denis11mo ago
Without any additional context from you - to overcome the difference in the return type you could make your interface generic
Vortac
VortacOP11mo ago
This is what I ended up doing, thanks! The JSON responses varied
Denis
Denis11mo ago
Well then I'm not sure whether that is the right approach If the interface method says that it's for getting access tokens, then it should probably return a string and the lifetime information of the token. The implementation of the interface via specific methods should handle the specific json differences
Vortac
VortacOP11mo ago
What's wrong with using the generic interface? Out of curiosity
Denis
Denis11mo ago
Nothing inherently wrong with a generic interface. Just that I don't it suits this specific case. I believe that you want an interface to abstract the specifics of your two implementations, that do essentially the same thing. Why would you still want to have a unique interface instance for the implementation it abstracts? I.e., why do you want ITokenGetter<A> and ITokenGetter<B>, when you could just have ITokenGetter? Does your project require this complexity?
Vortac
VortacOP10mo ago
For TokenGetter A, I receive a token and a an optional refresh token that appears every 15 days (B does not do this). The JSON class to deserialize into is also different from B Okay, so from what I can see in the docs, any properties not specified in my model class will be ignored by default, so I can remove the ones I don't need. But then I still have the difference of A having a RefreshToken that may or may not be null (it isn't null on day 15), whereas B only has AccessToken
Denis
Denis10mo ago
Well you could design a class that allows the refresh token to be null Both apis would map to that class, and the rest of your app would handle the null refresh
Vortac
VortacOP10mo ago
Thank you!
Want results from more Discord servers?
Add your server