Parcourir la source

add endpoint to download latest release details from GitHub

Chris Mullins il y a 8 ans
Parent
commit
9e8ed78840

+ 6 - 2
lib/GithubClient/GithubClient.cpp

@@ -2,7 +2,7 @@
 #include <FS.h>
 
 Stream& GithubClient::stream(const String& path) {
-  if (!client.connect(GITHUB_RAW_DOMAIN, 443)) {
+  if (!client.connect(domain.c_str(), 443)) {
     Serial.println(F("Failed to connect to github over HTTPS."));
   }
   
@@ -11,7 +11,7 @@ Stream& GithubClient::stream(const String& path) {
   }
   
   client.printf(
-    "GET %s HTTP/1.1\r\nHost: %s\r\nConnection: close\r\n\r\n",
+    "GET %s HTTP/1.1\r\nHost: %s\r\nUser-Agent: esp8266_milight_hub\r\nConnection: close\r\n\r\n",
     path.c_str(), 
     domain.c_str()
   );
@@ -98,6 +98,10 @@ String GithubClient::buildRepoPath(const String& username, const String& repo, c
   String path = String("/") + username + "/" + repo + "/master/" + repoPath;
   return path;
 }
+
+String GithubClient::buildApiRequest(const String &username, const String &repo, const String &path) {
+  return String("/repos/") + username + "/" + repo + path;
+}
   
 GithubClient GithubClient::rawDownloader() {
   return GithubClient(GITHUB_RAW_DOMAIN, GITHUB_RAW_FINGERPRINT);

+ 1 - 0
lib/GithubClient/GithubClient.h

@@ -27,6 +27,7 @@ public:
   static GithubClient apiClient();
   
   static String buildRepoPath(const String& username, const String& repo, const String& path);
+  static String buildApiRequest(const String& username, const String& repo, const String& path);
   
   uint8_t buffer[GITHUB_CLIENT_BUFFER_SIZE];
   

+ 24 - 0
lib/WebServer/MiLightHttpServer.cpp

@@ -20,6 +20,7 @@ void MiLightHttpServer::begin() {
   server.onPattern("/download_update/:component", HTTP_GET, [this](const UrlTokenBindings* b) { handleDownloadUpdate(b); });
   server.on("/web", HTTP_POST, [this]() { server.send(200, "text/plain", "success"); }, handleUpdateFile(WEB_INDEX_FILENAME));
   server.on("/about", HTTP_GET, [this]() { handleAbout(); });
+  server.on("/latest_release", HTTP_GET, [this]() { handleGetLatestRelease(); });
   server.on("/system", HTTP_POST, [this]() { handleSystemPost(); });
   server.on("/firmware", HTTP_POST, 
     [this](){
@@ -53,6 +54,29 @@ void MiLightHttpServer::begin() {
   server.begin();
 }
 
+void MiLightHttpServer::handleGetLatestRelease() {
+  GithubClient client = GithubClient::apiClient();
+  String path = GithubClient::buildApiRequest(
+    MILIGHT_GITHUB_USER,
+    MILIGHT_GITHUB_REPO,
+    "/releases/latest"
+  );
+  
+  Serial.println(path);
+  
+  // This is an ugly hack, but probably not worth optimizing. The nice way
+  // to do this would be to extract the content len from GitHub's response
+  // and stream the body to the server directly. But this would require parsing
+  // headers in the response from GitHub, which seems like more trouble than
+  // it's worth.
+  const String& fsPath = "/_cv.json";
+  client.download(path, fsPath);
+  
+  File file = SPIFFS.open(fsPath, "r");
+  server.streamFile(file, "application/json");
+  SPIFFS.remove(fsPath);
+}
+
 void MiLightHttpServer::handleClient() {
   server.handleClient();
 }

+ 1 - 0
lib/WebServer/MiLightHttpServer.h

@@ -39,6 +39,7 @@ protected:
   void handleUpdateSettings();
   void handleGetRadioConfigs();
   void handleAbout();
+  void handleGetLatestRelease();
   void handleSystemPost();
   void handleListenGateway(const UrlTokenBindings* urlBindings);
   void handleSendRaw(const UrlTokenBindings* urlBindings);