test reddit api
Application uses Reddit "Script Application Type" for consuming reddit posts and requires ClientID, ClientSecret, UserName, Password and redirectURI to Generate Access Token and eventually calling the API. Ideally we could have put the secrets in the Environment Variables of computer, but to keep it simple and test it easily I have kept the secrets in the appsettings.json file.
Please replace below setting in appsettings.json file to run the application. FilePath, RedditClientID, RedditClientSecret, RedditRedirectURI, TestUserName, TestUserPassword
There are 2 projects within the solution.
- RedditSharp.API which is .NET 6 Web API
- RedditSharp.API.Test which is xUnit project to Unit testing Controller and Business logic layer
I have created various layers of a the api as a folders within RedditSharp.API to keep it simple and easy to read, ideally many folders will become individual class libraries in a real life project. We can certainly secure our API with jWT Token auth or any other mechanism.
I have utilized Redditsharp library for consuming Reddit posts as a stream, after studying it in details, I found that it already fulfills the needs of throttling (Reqeusts per second/ per user) requirement based of the Response headers.
API Exposes below 4 endpoints:
1. /ReadRedditPostsAsStream This end-point is the starting point of running/testing the application, it starts listening for no. of subreddit's you decided to listen on. I tried with "funny", "askreddit", "memes", "worldnews" for my testing. by default it download "New Posts" (100) and keep adding new ones being posted in real time. This is a void Task which will trigger Reddit Posts as stream and keep listening until you kill the API. It will store consumed posts in in-memory data store and do console log to post the performance and also creates a log file where we can see the same details for troubleshooting / verification purposes.
2. /api/v1/Reddit/posts/{subRedditName} This end-point returns "Posts With Most Up votes" within a given subreddit, you can vary by top 5, 10, 15 etc.. default is 5
3. /api/v1/Reddit/users/{subRedditName} This end-point returns "Users With most posts" within a given subreddit, you can vary by top 5, 10, 15 etc.. default is 5
4. /api/v1/Reddit/posts/total/{subRedditName} This end-point returns "Total posts downloaded within subreddit so far."