RegisterEndpointHandler() public method

Get called when a client (i.e. javascript) does an HTTP api call
public RegisterEndpointHandler ( string pattern, EndpointHandler handler, bool handleOnUiThread = true ) : void
pattern string Simple string or regex to match APIs that this can handle. This must match what comes after the ".../api/" of the URL
handler EndpointHandler The method to call
handleOnUiThread bool For safety, this defaults to true, but that can kill performance if you don't need it (BL-3452)
return void
Example #1
0
        public void RegisterWithServer(EnhancedImageServer server)
        {
            server.RegisterEndpointHandler(kBrandingImageUrlPart, request =>
            {
#if DEBUG
                // The book templates are allowed to use the branding api.  All real books
                // should not use this facility.
                if (request.CurrentBook == null || request.CurrentBook.FolderPath == null ||
                    !Book.BookStorage.IsStaticContent(request.CurrentBook.FolderPath))
                {
                    //Debug.Fail("Books should no longer have branding api urls");
                }
#endif
                var fileName = request.RequiredFileNameOrPath("id");
                var path     = FindBrandingImageFileIfPossible(_collectionSettings.BrandingProjectKey, fileName.NotEncoded, request.CurrentBook.GetLayout());

                // And this is perfectly normal, to not have a branding image at all, for a particular page:
                if (string.IsNullOrEmpty(path))
                {
                    request.Failed("");
                    // the HTML will need to be able to handle this invisibly... see http://stackoverflow.com/questions/22051573/how-to-hide-image-broken-icon-using-only-css-html-without-js
                    return;
                }
                request.ReplyWithImage(path);
            }, false);
        }
Example #2
0
        public static string GetString(EnhancedImageServer server, string endPoint, string query = "",
			ContentType returnType = ContentType.Text, EndpointHandler handler = null, string endOfUrlForTest = null)
        {
            if(handler != null)
            {
                server.RegisterEndpointHandler(endPoint, handler);
            }
            server.StartListening();
            var client = new WebClientWithTimeout
            {
                Timeout = 3000,
            };
            client.Headers[HttpRequestHeader.ContentType] = returnType == ContentType.Text ? "text/plain" : "application/json";

            if(endOfUrlForTest != null)
            {
                return client.DownloadString(ServerBase.ServerUrlWithBloomPrefixEndingInSlash + "api/" + endOfUrlForTest);
            }
            else
            {
                if(!string.IsNullOrEmpty(query))
                    query = "?" + query;
                return client.DownloadString(ServerBase.ServerUrlWithBloomPrefixEndingInSlash + "api/" + endPoint + query);
            }
        }
Example #3
0
        public void RegisterWithServer(EnhancedImageServer server)
        {
            server.RegisterEndpointHandler("collection/defaultFont", request =>
            {
                var bookFontName = request.CurrentCollectionSettings.DefaultLanguage1FontName;
                if (String.IsNullOrEmpty(bookFontName))
                {
                    bookFontName = "sans-serif";
                }
                request.ReplyWithText(bookFontName);
            });

            server.RegisterEndpointHandler("readers/ui/.*", HandleRequest, true);
            server.RegisterEndpointHandler("readers/io/.*", HandleRequest, false);

            //we could do them all like this:
            //server.RegisterEndpointHandler("readers/loadReaderToolSettings", r=> r.ReplyWithJson(GetDefaultReaderSettings(r.CurrentCollectionSettings)));
        }
Example #4
0
 public static void RegisterWithServer(EnhancedImageServer server)
 {
     server.RegisterEndpointHandler("help/.*", (request) =>
     {
         var topic = request.LocalPath().ToLowerInvariant().Replace("api/help","");
         Show(Application.OpenForms.Cast<Form>().Last(), topic);
         //if this is called from a simple html anchor, we don't want the browser to do anything
         request.SucceededDoNotNavigate();
     });
 }
Example #5
0
        public void RegisterWithServer(EnhancedImageServer server)
        {
            server.RegisterEndpointHandler(kBrandingImageUrlPart, request =>
            {
                var fileName = request.RequiredFileNameOrPath("id");

                var path = BloomFileLocator.GetOptionalBrandingFile(_collectionSettings.BrandingProjectName, fileName.NotEncoded);
                if(string.IsNullOrEmpty(path))
                {
                    request.Failed("");
                    // the HTML will need to be able to handle this invisibly... see http://stackoverflow.com/questions/22051573/how-to-hide-image-broken-icon-using-only-css-html-without-js
                    return;
                }
                request.ReplyWithImage(path);
            });
        }
Example #6
0
        public static string PostString(EnhancedImageServer server, string endPoint, string data, ContentType returnType,
			EndpointHandler handler = null)
        {
            if(handler != null)
            {
                server.RegisterEndpointHandler(endPoint, handler);
            }
            server.StartListening();
            var client = new WebClientWithTimeout
            {
                Timeout = 3000
            };
            client.Headers[HttpRequestHeader.ContentType] = returnType == ContentType.Text ? "text/plain" : "application/json";

            return client.UploadString(ServerBase.ServerUrlWithBloomPrefixEndingInSlash + "api/" + endPoint, "POST", data);
        }
Example #7
0
        public void RegisterWithServer(EnhancedImageServer server)
        {
            server.RegisterEndpointHandler(kBrandingImageUrlPart, request =>
            {
                var fileName = request.RequiredFileNameOrPath("id");
                var path     = FindBrandingImageFileIfPossible(_collectionSettings.BrandingProjectName, fileName.NotEncoded);

                // And this is perfectly normal, to not have a branding image at all, for a particular page:
                if (string.IsNullOrEmpty(path))
                {
                    request.Failed("");
                    // the HTML will need to be able to handle this invisibly... see http://stackoverflow.com/questions/22051573/how-to-hide-image-broken-icon-using-only-css-html-without-js
                    return;
                }
                request.ReplyWithImage(path);
            });
        }
 public void RegisterWithServer(EnhancedImageServer server)
 {
     server.RegisterEndpointHandler("keyboarding/useLongPress", (ApiRequest request) =>
     {
         try
         {
             //detect if some keyboarding system is active, e.g. KeyMan. If it is, don't enable LongPress
             var form = Application.OpenForms.Cast<Form>().Last();
             request.ReplyWithText(SIL.Windows.Forms.Keyboarding.KeyboardController.IsFormUsingInputProcessor(form)
                 ? "false"
                 : "true");
         }
         catch(Exception error)
         {
             request.ReplyWithText("true"); // This is arbitrary. I don't know if it's better to assume keyman, or not.
             NonFatalProblem.Report(ModalIf.None, PassiveIf.All, "Error checking for keyman","", error);
         }
     }, handleOnUiThread:false);
 }
Example #9
0
 public void RegisterWithServer(EnhancedImageServer server)
 {
     server.RegisterEndpointHandler("bookSettings", HandleBookSettings);
     server.RegisterEndpointHandler("imageInfo", HandleImageInfo);
 }
 public static void RegisterWithServer(EnhancedImageServer server)
 {
     server.RegisterEndpointHandler(kPrefix+"/.*", ExternalLinkController.HandleRequest);
 }
Example #11
0
        public void RegisterWithServer(EnhancedImageServer server)
        {
            server.RegisterEndpointHandler("audio/startRecord", HandleStartRecording);
            server.RegisterEndpointHandler("audio/endRecord", HandleEndRecord);
            server.RegisterEndpointHandler("audio/enableListenButton", HandleEnableListenButton);
            server.RegisterEndpointHandler("audio/deleteSegment", HandleDeleteSegment);
            server.RegisterEndpointHandler("audio/currentRecordingDevice", HandleCurrentRecordingDevice);
            server.RegisterEndpointHandler("audio/checkForSegment", HandleCheckForSegment);
            server.RegisterEndpointHandler("audio/devices", HandleAudioDevices);

            Debug.Assert(ServerBase.portForHttp > 0,"Need the server to be listening before this can be registered (BL-3337).");
        }
 public void RegisterWithServer(EnhancedImageServer server)
 {
     server.RegisterEndpointHandler("addPage", HandleAddPage);
     server.RegisterEndpointHandler("changeLayout", HandleChangeLayout);
 }
 public void RegisterWithServer(EnhancedImageServer server)
 {
     // Not sure this needs UI thread, but it can result in saving the page, which seems
     // safest to do that way.
     server.RegisterEndpointHandler("book/settings", HandleBookSettings, true);
 }
 public void RegisterWithServer(EnhancedImageServer server)
 {
     server.RegisterEndpointHandler("pageTemplates", HandleTemplatesRequest);
     // Being on the UI thread causes a deadlock on Linux/Mono.  See https://silbloom.myjetbrains.com/youtrack/issue/BL-3818.
     server.RegisterEndpointHandler("pageTemplateThumbnail", HandleThumbnailRequest, false);
 }
Example #15
0
 public void RegisterWithServer(EnhancedImageServer server)
 {
     server.RegisterEndpointHandler("image/info", HandleImageInfo);
 }
 public void RegisterWithServer(EnhancedImageServer server)
 {
     server.RegisterEndpointHandler("bookSettings", HandleBookSettings);
     server.RegisterEndpointHandler("imageInfo", HandleImageInfo);
 }
Example #17
0
 public static void RegisterWithServer(EnhancedImageServer server)
 {
     server.RegisterEndpointHandler("toolbox/settings", HandleSettings);
 }