-
Notifications
You must be signed in to change notification settings - Fork 2
Support multiple Autolab servers #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
554f433
94eaa0d
fbe4fa4
d99289f
889a547
9609740
fbcc184
1f63b48
76aac1f
6548ce3
128999f
dddc5df
c0a00ab
2b7d8f7
a36a02d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,56 @@ | ||||||||||||||||
| #ifndef _MULTI_SERVER_H_ | ||||||||||||||||
| #define _MULTI_SERVER_H_ | ||||||||||||||||
|
|
||||||||||||||||
| #include <string> | ||||||||||||||||
| #include <vector> | ||||||||||||||||
|
|
||||||||||||||||
| namespace Autolab { | ||||||||||||||||
| struct ServerInfo { | ||||||||||||||||
| std::string server_name; | ||||||||||||||||
| std::string base_uri; // domain of the autolab service | ||||||||||||||||
| std::string client_id; | ||||||||||||||||
| std::string client_secret; | ||||||||||||||||
| std::string redirect_uri; | ||||||||||||||||
| std::vector<std::string> courses; | ||||||||||||||||
| }; | ||||||||||||||||
|
|
||||||||||||||||
|
|
||||||||||||||||
| class AllServers { | ||||||||||||||||
| public: | ||||||||||||||||
| std::vector<ServerInfo> m_server_list; | ||||||||||||||||
|
|
||||||||||||||||
| AllServers(const std::string& all_servers_filename); // parses the JSON file to populate itself | ||||||||||||||||
|
|
||||||||||||||||
| std::vector<std::string> get_all_courses() const; | ||||||||||||||||
| std::vector<std::string> get_all_server_names() const; | ||||||||||||||||
|
|
||||||||||||||||
| // Returns "" if there is no such course | ||||||||||||||||
| ServerInfo get_server_from_course(const std::string& course_name) const; | ||||||||||||||||
| ServerInfo get_server_from_name(const std::string& server_name) const; | ||||||||||||||||
|
Comment on lines
+27
to
+29
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Misleading comment: return type is The comment says "Returns "" if there is no such course" but the function returns Suggested fix- // Returns "" if there is no such course
- ServerInfo get_server_from_course(const std::string& course_name) const;
- ServerInfo get_server_from_name(const std::string& server_name) const;
+ // Throws InvalidInputException if the course is not found
+ ServerInfo get_server_from_course(const std::string& course_name) const;
+ // Throws InvalidInputException if the server name is not found
+ ServerInfo get_server_from_name(const std::string& server_name) const;🤖 Prompt for AI Agents |
||||||||||||||||
| }; | ||||||||||||||||
|
|
||||||||||||||||
| struct AuthInfo { | ||||||||||||||||
| std::string server_name; | ||||||||||||||||
| bool exists; | ||||||||||||||||
| std::string access_token; | ||||||||||||||||
| std::string refresh_token; | ||||||||||||||||
| }; | ||||||||||||||||
|
|
||||||||||||||||
| class AllAuthInfo { | ||||||||||||||||
| public: | ||||||||||||||||
| std::vector<AuthInfo> m_auth_info_list; | ||||||||||||||||
| bool has_auth_for_server(const std::string& server_name); | ||||||||||||||||
| bool set_tokens_for_server(const std::string& server_name, | ||||||||||||||||
| const std::string& access_token, const std::string& refresh_token); | ||||||||||||||||
|
|
||||||||||||||||
| // Returns "" if it does not exist | ||||||||||||||||
| std::string get_access_token_from_server(const std::string& server_name); | ||||||||||||||||
| std::string get_refresh_token_from_server(const std::string& server_name); | ||||||||||||||||
|
Comment on lines
+46
to
+48
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Misleading comment: these functions throw or return empty string. Similar to Suggested fix- // Returns "" if it does not exist
- std::string get_access_token_from_server(const std::string& server_name);
+ // Throws std::runtime_error if it does not exist
+ std::string get_access_token_from_server(const std::string& server_name);
+ // Returns "" if it does not exist
std::string get_refresh_token_from_server(const std::string& server_name);📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||
| }; | ||||||||||||||||
|
|
||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| extern Autolab::AllServers g_all_servers; | ||||||||||||||||
|
|
||||||||||||||||
|
|
||||||||||||||||
| #endif /* _MULTI_SERVER_H_ */ | ||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -19,26 +19,21 @@ | |||||
| #include <rapidjson/document.h> | ||||||
|
|
||||||
| #include "autolab/autolab.h" | ||||||
| #include "autolab/multi_server.h" | ||||||
|
|
||||||
| namespace Autolab { | ||||||
|
|
||||||
| class RawClient { | ||||||
| public: | ||||||
| RawClient(const std::string &domain, const std::string &id, | ||||||
| const std::string &st, const std::string &ru, | ||||||
| void (*tk_cb)(std::string, std::string)); | ||||||
|
|
||||||
| // setters and getters | ||||||
| void set_tokens(std::string at, std::string rt); | ||||||
| const std::string get_access_token() { return access_token; } | ||||||
| const std::string get_refresh_token() { return refresh_token; } | ||||||
| void set_new_tokens_callback(void (*cb)(std::string, std::string)) { | ||||||
| new_tokens_callback = cb; | ||||||
| } | ||||||
| RawClient(); | ||||||
|
|
||||||
|
|
||||||
| /* oauth-related */ | ||||||
| void device_flow_init(std::string &user_code, std::string &verification_uri); | ||||||
| int device_flow_authorize(size_t timeout); | ||||||
| void set_auth_info_list(std::vector<AuthInfo> auth_info_list); | ||||||
| bool has_auth_for_server(const std::string& server_name); | ||||||
| void device_flow_init(std::string &user_code, std::string &verification_uri, std::string& device_code, const ServerInfo& server_info); | ||||||
| int device_flow_authorize(size_t timeout, const std::string& device_code, | ||||||
| const ServerInfo& server_info); | ||||||
|
|
||||||
| // keeps track of state and config for the current request. | ||||||
| struct request_state { | ||||||
|
|
@@ -75,8 +70,8 @@ class RawClient { | |||||
| typedef std::vector<std::pair<std::string, std::string>> Params; | ||||||
|
|
||||||
| /* REST interface methods */ | ||||||
| void get_user_info(rapidjson::Document &result); | ||||||
| void get_courses(rapidjson::Document &result); | ||||||
| void get_user_info(rapidjson::Document &result, const ServerInfo& server_info__); | ||||||
| void get_courses(rapidjson::Document &result, const ServerInfo& server_info); | ||||||
| void get_assessments(rapidjson::Document &result, const std::string &course_name); | ||||||
| void get_assessment_details(rapidjson::Document &result, const std::string &course_name, const std::string &asmt_name); | ||||||
| void get_problems(rapidjson::Document &result, const std::string &course_name, const std::string &asmt_name); | ||||||
|
|
@@ -89,15 +84,12 @@ class RawClient { | |||||
| void crud_enrollment(rapidjson::Document &result, const std::string &course_name, std::string email, Params &in_params, CrudAction action); | ||||||
|
|
||||||
| private: | ||||||
| // domain of the autolab service | ||||||
| std::string base_uri; | ||||||
|
|
||||||
| // initializes curl interface. Must be called before anything else. | ||||||
| static int curl_ready; | ||||||
| static int init_curl(); | ||||||
|
|
||||||
| // tokens-related | ||||||
| void (*new_tokens_callback)(std::string, std::string); | ||||||
|
|
||||||
| enum HttpMethod {GET, POST, PUT, DELETE}; | ||||||
| HttpMethod crud_to_http(CrudAction action); | ||||||
|
|
@@ -129,33 +121,27 @@ class RawClient { | |||||
| // private instance vars | ||||||
| int api_version; | ||||||
|
|
||||||
| std::string client_id; | ||||||
| std::string client_secret; | ||||||
| std::string redirect_uri; | ||||||
| std::string access_token; | ||||||
| std::string refresh_token; | ||||||
| std::string device_flow_device_code; | ||||||
| std::string device_flow_user_code; | ||||||
| AllAuthInfo all_auth_info; | ||||||
|
|
||||||
| // perform HTTP request and return result, default method is GET. | ||||||
| long raw_request(request_state *rstate, path_segments &path, param_list ¶ms, HttpMethod method); | ||||||
| long raw_request_optional_refresh(request_state *rstate, path_segments &path, param_list ¶ms, HttpMethod method, bool refresh); | ||||||
| long make_request(rapidjson::Document &response, path_segments &path, param_list ¶ms, HttpMethod method, bool refresh, | ||||||
| long raw_request(request_state *rstate, path_segments &path, param_list ¶ms, const ServerInfo& server_info__, HttpMethod method); | ||||||
| long raw_request_optional_refresh(request_state *rstate, path_segments &path, param_list ¶ms, const ServerInfo& server_info__, HttpMethod method, bool refresh); | ||||||
| long make_request(rapidjson::Document &response, path_segments &path, param_list ¶ms, const ServerInfo& server_info__, HttpMethod method, bool refresh, | ||||||
| const std::string &download_dir, const std::string &suggested_filename, const std::string &upload_filename); | ||||||
|
|
||||||
| void clear_device_flow_strings(); | ||||||
|
|
||||||
| bool save_tokens_from_response(rapidjson::Document &response); | ||||||
| bool get_token_from_authorization_code(std::string authorization_code); | ||||||
| bool perform_token_refresh(); | ||||||
| bool save_tokens_from_response(rapidjson::Document &response, const std::string& server_name); | ||||||
| bool get_token_from_authorization_code(std::string authorization_code, const ServerInfo& server_name); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Misleading parameter name: The parameter is named Suggested fix- bool get_token_from_authorization_code(std::string authorization_code, const ServerInfo& server_name);
+ bool get_token_from_authorization_code(std::string authorization_code, const ServerInfo& server_info);📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||
| bool perform_token_refresh(const ServerInfo& server_info); | ||||||
|
|
||||||
| bool document_has_error(request_state *rstate, const std::string &error_msg); | ||||||
| void init_regular_path(path_segments &path); | ||||||
| void init_regular_params(param_list ¶ms); | ||||||
| void init_regular_params(param_list ¶ms, const ServerInfo& server_info__); | ||||||
| void init_oauth_token_path(path_segments &path); | ||||||
| void init_device_flow_init_path(path_segments &path); | ||||||
| void init_device_flow_authorize_path(path_segments &path); | ||||||
| void update_access_token_in_params(param_list ¶ms); | ||||||
| void update_access_token_in_params(param_list ¶ms, const ServerInfo& server_info__); | ||||||
| }; | ||||||
|
|
||||||
| } | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| #ifndef _ALL_SERVERS_DIRNAME_H_ | ||
| #define _ALL_SERVERS_DIRNAME_H_ | ||
|
|
||
| #include <string> | ||
|
|
||
| /* Fill this in with where all the server text files are stored. Make sure it | ||
| is an absolute path. */ | ||
| std::string all_servers_dirname = "/path/to/all_servers/"; | ||
|
|
||
| #endif /* _ALL_SERVERS_DIRNAME_H_ */ | ||
|
Comment on lines
+1
to
+10
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Static initialization order fiasco risk. Per context snippet at Consider using a function that returns a reference to a local static (lazy initialization) to guarantee order: inline const std::string& get_all_servers_dirname() {
static std::string dirname = "/path/to/servers/";
return dirname;
}🤖 Prompt for AI Agents |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix typos and phrasing in setup instructions.
There are a couple of user-facing text issues:
"secrest"should be"secrets", and"Inside of this directory"should be"Inside this directory".✏️ Suggested doc edits
📝 Committable suggestion
🧰 Tools
🪛 LanguageTool
[grammar] ~34-~34: Ensure spelling is correct
Context: ...rypto/secrets.h.template", changing the secrest to strings of your choosing. Then, cre...
(QB_NEW_EN_ORTHOGRAPHY_ERROR_IDS_1)
[style] ~39-~39: This phrase is redundant. Consider using “Inside”.
Context: ...ll_servers_dirname` field in the file. Inside of this directory, insert files that follo...
(OUTSIDE_OF)
🤖 Prompt for AI Agents