leetcode_api_dart
Inspired by https://github.com/dustyRAIN/leetcode-api-go
Get Started
import 'package:leetcode_api_dart/leetcode_api_dart.dart';
final session = "your_leetcode_session_from_cookie";
final csrfToken = "your_leetcode_csrfToken_from_cookie";
void main() {
final api = LeetcodeApiClient(
session: session,
csrfToken: csrfToken,
);
// make api calls
}
You can pass session and csrfToken as empty, however session info will provide additional data such as the user's solve count, submission status, premium data (if applicable to the user), etc.
Available APIs Related to Leetcode Contests Page
Future<Contest> getContestInfo({required String contestSlug});
Future<ContestRanking> getContestRanking({required String contestSlug, required int page});
Available APIs Related to Leetcode Discussions Page
Future<DiscussionList> getDiscussions({
List<String> categories = const [],
List<String> tags = const [],
String? orderBy,
String? searchQuery,
required int offset,
});
Future<Discussion> getDiscussion({required int topicId});
Future<List<Comment>> getDiscussionComments({
required int topicId,
String? orderBy,
required int offset,
required int pageSize,
});
Future<List<Comment>> getCommentReplies({required int commentId});
Available APIs Related to Leetcode Problems Page
Future<ProblemList> getAllProblems({required int offset, required int pageSize});
Future<ProblemContent?> getProblemContentByTitleSlug({required String titleSlug});
Future<ProbemsByTopics> getProblemsByTopic({required String topicTag});
Future<ProblemList> getTopInterviewProblems({required int offset, required int pageSize});
Available APIs Related to Leetcode Users Page
Future<UserPublicProfile> getUserPublicProfile({required String username});
Future<TagProblemsCount> getUserSolveCountByProblemTag({required String username});
Future<UserContestRankingDetails> getUserContestRankingHistory({required String username});
Future<UserSolveCountByDifficultyDetails> getUserSolveCountByDifficulty({required String username});
Future<UserCalendar> getUserProfileCalendar({required String username});
Future<List<AcSubmission>> getUserRecentAcSubmissions({required String username, required int pageSize});
Make Your Own Call to Leetcode
Indeed the available APIs may not be sufficient, hence a GraphQL request can be made directly. The following method will only allow us to make GET calls
to prevent the misusage of the API. The method takes one parameter, which is GraphQL request payload as a string
import 'package:leetcode_api_dart/leetcode_api_dart.dart';
extension LeetcodeApiClientCustom on LeetcodeApiClient {
Future<MyCustomModel> getCustomQuery({required String param}) async {
final data = await makeGraphQLRequest(buildMyGraphQLQueryHere(param: param));
return MyCustomModel.fromJson(data);
}
}
// don't forget to add 'r' before the string, in order to pass the $ symbol without stripping it away in dart
String buildMyGraphQLQueryHere({required String param}) => r'''{
"query": "\n query Something() {\n name \nis_blocked \n} \n",
"variables": {}
}''';
Tests
In order to run tests, create a .env
file in root directory
LEETCODE_SESSION_TOKEN=your_leetcode_session_from_cookie
LEETCODE_CSRF_TOKEN=your_leetcode_csrfToken_from_cookie
Libraries
- contests
- discussions
- leetcode_api_client
- leetcode_api_dart
- models/ac_submission
- models/badge
- models/coin_reward
- models/comment
- models/company
- models/contest
- models/contest_meta
- models/contest_problem_info
- models/contest_ranking
- models/difficulty_count
- models/difficulty_enum
- models/difficulty_percentage
- models/discussion
- models/discussion_list
- models/discussion_list_item
- models/discussion_node
- models/models
- models/participant_details
- models/post
- models/problem
- models/problem_content
- models/problem_list
- models/problems_by_topic
- models/submission_in_contest
- models/tag_count
- models/tag_problems_count
- models/topic_tag
- models/user_calendar
- models/user_contest_ranking
- models/user_contest_ranking_details
- models/user_contest_ranking_history
- models/user_profile
- models/user_public_profile
- models/user_solve_count_by_difficulty
- models/user_solve_count_by_difficulty_details
- problems
- queries
- users
- utils