internal static int RequestTimeout = 20; // Timeout in seconds #endregion Fields #region Methods /// <summary> /// /// </summary> /// <param name="ip"></param> /// <param name="port"></param> /// <param name="depotKey"></param> /// <param name="key"></param> /// <param name="completionCallback"></param> /// <param name="errorCallback"></param> public static void Software_GET(string ip, ushort port, string depotKey, string key, Action<Software_v1> completionCallback = null, Action<string> errorCallback = null) { try { Dictionary<String, String> headers = new Dictionary<String, String> { { "acceptversion", "application/starcounter.warehouse.software-v1+json" } }; string url = string.Format("http://{0}:{1}/warehouse/api/depots/{2}/applications/{3}", ip, port, depotKey, key); Http.GET(url, headers, (Response response) => { try { if (response.IsSuccessStatusCode) { if (completionCallback != null) { Software_v1 result = new Software_v1(); result.PopulateFromJson(response.Body); try { completionCallback(result); } catch { } } } else if (response.StatusCode == (ushort)System.Net.HttpStatusCode.NotFound) { if (errorCallback != null) { string errorMessage = string.Format("Failed to retrive the application ({0}) from the Warehouse ({1}:{2}) store (DepotKey:{3}), Check the settings.", key, ip, port, depotKey); try { errorCallback(errorMessage); } catch { } } } else { if (errorCallback != null) { string errorMessage = string.Format("Failed to retrive the application ({0}) from the Warehouse ({1}:{2}) store (DepotKey:{3}), Status: ({4}) {5}, {6}. Check the settings.", key, ip, port, depotKey, response.StatusCode, response.StatusDescription, response.Body); try { errorCallback(errorMessage); } catch { } } } } catch (Exception e) { if (errorCallback != null) { try { errorCallback(e.Message); } catch { } } } }, RestApi.RequestTimeout); } catch (Exception e) { if (errorCallback != null) { try { errorCallback(e.Message); } catch { } } } }
/// <summary> /// Register /// </summary> public static void Register() { HandlerOptions opt = new HandlerOptions() { SkipHandlersPolicy = true, SkipRequestFilters = true }; #region Get MultiHost Depots (todo rename?) Starcounter.Handle.GET("/warehouse/api/multihostdepots/{?}", (string domain, Request request) => { string domainName = HttpUtility.UrlDecode(domain); // check if hostname exist HostService hostService = Db.SQL<HostService>("SELECT o FROM Warehouse.HostService o WHERE o.DomainName=?", domainName).First; if (hostService == null) { return System.Net.HttpStatusCode.NotFound; } string eTag = request.Headers["If-None-Match"]; string depotHostingEtag = string.Format("{0:X8}", hostService.DepotHostingModified.GetHashCode()); if (eTag == depotHostingEtag) { return System.Net.HttpStatusCode.NotModified; } QueryResultRows<DepotHostService> depotHostServices = Db.SQL<DepotHostService>("SELECT o FROM Warehouse.DepotHostService o WHERE o.HostService=?", hostService); Response response = new Response(); DepotHostServices_v1 result = new DepotHostServices_v1(); result.Items.Data = depotHostServices; response.Resource = result; response.StatusCode = (ushort)System.Net.HttpStatusCode.OK; response.Headers["ETag"] = depotHostingEtag; return response; }, opt); #endregion #region Get Default Depot Starcounter.Handle.GET("/warehouse/api/depots", (Request request) => { Response response = new Response(); Depots_v1 depots = new Depots_v1(); depots.Depots.Data = Db.SQL<Depot>("SELECT o.Depot FROM Warehouse.DefaultDepot o"); response.Resource = depots; response.StatusCode = (ushort)System.Net.HttpStatusCode.OK; return response; }, opt); Starcounter.Handle.GET("/warehouse/api/depots?{?}", (string parameters, Request request) => { // TODO: Handle parameters Response response = new Response(); Depots_v1 depots = new Depots_v1(); depots.Depots.Data = Db.SQL<Depot>("SELECT o.Depot FROM Warehouse.DefaultDepot o"); response.Resource = depots; response.StatusCode = (ushort)System.Net.HttpStatusCode.OK; return response; }, opt); #endregion #region Get Depot Starcounter.Handle.GET("/warehouse/api/depots/{?}", (string depotKey, Request request) => { Depot depot = Db.SQL<Depot>("SELECT o FROM Warehouse.Depot o WHERE o.DepotKey=?", depotKey).First; if (depot != null) { string eTag = request.Headers["If-None-Match"]; string depotEtag = string.Format("{0:X8}", depot.Modified.GetHashCode()); if (eTag == depotEtag) { return System.Net.HttpStatusCode.NotModified; } Response response = new Response(); Depot_v1 depotJson = new Depot_v1(); depotJson.Data = depot; response.Resource = depotJson; response.StatusCode = (ushort)System.Net.HttpStatusCode.OK; response.Headers["ETag"] = depotEtag; return response; } return (ushort)System.Net.HttpStatusCode.NotFound; }, opt); #endregion #region Get Applications Starcounter.Handle.GET("/warehouse/api/depots/{?}/applications", (string depotKey) => { Depot depot = Db.SQL<Depot>("SELECT o FROM Warehouse.Depot o WHERE o.DepotKey=?", depotKey).First; if (depot != null) { Response response = new Response(); DepotSoftwares_v1 depotItemsJson = new DepotSoftwares_v1(); depotItemsJson.DepotKey = depotKey; var software = Db.SQL<Software>("SELECT o.Software FROM Warehouse.DepotSoftware o WHERE o.Depot=?", depot); depotItemsJson.Softwares.Data = software; response.Resource = depotItemsJson; response.StatusCode = (ushort)System.Net.HttpStatusCode.OK; return response; } return (ushort)System.Net.HttpStatusCode.NotFound; }, opt); #endregion #region Get Application Starcounter.Handle.GET("/warehouse/api/depots/{?}/applications/{?}", (string depotKey, string id, Request request) => { Depot depot = Db.SQL<Depot>("SELECT o FROM Warehouse.Depot o WHERE o.DepotKey=?", depotKey).First; if (depot != null) { Software software = depot.FindSoftware(id); if (software != null) { Response response = new Response(); Software_v1 softwareJson = new Software_v1(); softwareJson.Depot = depot; softwareJson.Data = software; if (software is Suite) { softwareJson.SuiteContents.Data = Db.SQL<Software>("SELECT o.Software FROM Warehouse.SuiteSoftware o WHERE o.Suite=?", software); softwareJson.Apps.Data = ((Suite)software).GetContentApps(); // TODO // app.IconUrl is null // Version.url is null } response.Resource = softwareJson; response.StatusCode = (ushort)System.Net.HttpStatusCode.OK; return response; } } return (ushort)System.Net.HttpStatusCode.NotFound; }, opt); #endregion #region Get software versions Starcounter.Handle.GET("/warehouse/api/depots/{?}/applications/{?}/versions", (string depotKey, string id, Request request) => { DepotSoftware depotSoftware = Db.SQL<DepotSoftware>("SELECT o FROM Warehouse.DepotSoftware o WHERE o.Depot.DepotKey=? AND o.Software.ID=?", depotKey, id).First; if (depotSoftware != null) { Response response = new Response(); Versions_v1 versionsJson = new Versions_v1(); versionsJson.Depot = depotSoftware.Depot; versionsJson.Versions.Data = depotSoftware.Software.Versions; response.Resource = versionsJson; response.StatusCode = (ushort)System.Net.HttpStatusCode.OK; return response; } return (ushort)System.Net.HttpStatusCode.NotFound; }, opt); #endregion #region Get software versions with parameters Starcounter.Handle.GET("/warehouse/api/depots/{?}/applications/{?}/versions?{?}", (string depotKey, string id, string parameters, Request request) => { DepotSoftware depotSoftware = Db.SQL<DepotSoftware>("SELECT o FROM Warehouse.DepotSoftware o WHERE o.Depot.DepotKey=? AND o.Software.ID=?", depotKey, id).First; if (depotSoftware != null) { NameValueCollection collection = System.Web.HttpUtility.ParseQueryString(parameters); string starcounterVersion = collection["starcounterversion"]; bool content; if (!bool.TryParse(collection["content"], out content)) { content = false; } if (starcounterVersion == null && (content == false || depotSoftware.Software is App)) { return Self.GET(string.Format("/warehouse/api/depots/{0}/applications/{1}/versions", depotKey, id)); } Response response = new Response(); Versions_v1 versionsJson = new Versions_v1(); versionsJson.Depot = depotSoftware.Depot; if (depotSoftware.Software is Suite && content) { IList<Version> result = GetAllSuiteContentVersion(depotSoftware.Software as Suite, starcounterVersion); if (result != null) { versionsJson.Versions.Data = result; } } else { foreach (Version version in depotSoftware.Software.Versions) { if (version.IsCompatibleWith(starcounterVersion)) { versionsJson.Versions.Add().Data = version; break; } } } response.Resource = versionsJson; response.StatusCode = (ushort)System.Net.HttpStatusCode.OK; return response; } return (ushort)System.Net.HttpStatusCode.NotFound; }, opt); #endregion #region Get version json/binary Starcounter.Handle.GET("/warehouse/api/depots/{?}/applications/{?}/versions/{?}", (string depotKey, string softwareId, string id, Request request) => { Depot depot = Db.SQL<Depot>("SELECT o FROM Warehouse.Depot o WHERE o.DepotKey=?", depotKey).First; if (depot != null) { Software software = depot.FindSoftware(softwareId); if (software != null) { Version version = Db.SQL<Version>("SELECT o FROM Warehouse.Version o WHERE o.Software=? AND o.ID=?", software, id).First; if (version != null) { if (request.Headers["Accept"] == "application/octet-stream") { if (File.Exists(version.BinaryFile)) { string fileName = Path.GetFileName(version.BinaryFile); Response binResponse = new Response() { StatusCode = (ushort)System.Net.HttpStatusCode.OK }; binResponse.StreamedBody = new StreamReader(version.BinaryFile).BaseStream; binResponse.Headers["Content-Disposition"] = "attachment; filename=\"" + fileName + "\""; return binResponse; } return new Response() { StatusCode = (ushort)System.Net.HttpStatusCode.NotFound }; } Response response = new Response(); Version_v1 versionJson = new Version_v1(); versionJson.DepotKey = depotKey; versionJson.Data = version; response.Resource = versionJson; response.StatusCode = (ushort)System.Net.HttpStatusCode.OK; return response; } } } return (ushort)System.Net.HttpStatusCode.NotFound; }, opt); #endregion }