2
votes

I want to create a brand new REST API using MarkLogic 9.x.x but without using Roxy or Gradle. I want to point to a file which has list of customized endpoints in basic MarkLogic setup and then keep my customized logic in those modules.

I've gone through the REST API documentation to create one by using CURL so that we can use default APIs provided by MarkLogic.

Any detailed explanation is appreciated.

4
If you want very a very custom API, then REST API may be too much trouble. It works great out of the box, but is a pain to customize. You might want to take a look at the declarative and interpretive rewriter documentation. Either of those can act as your API surface and route to the appropriate XQY or SJS endpoints. - wst

4 Answers

3
votes

Management REST api is really your best friend here, it is designed for this purpose. However, creating scripts that make the appropriate REST calls can be cumbersome. ml-gradle can support you with that though. It can generate a shell-script with all curl-statements for you, which you could run as-is, or use as starting point to build out your own set of deploy scripts. For details see:

https://github.com/marklogic-community/ml-gradle/wiki/Generating-a-shell-script

HTH!

3
votes

Have you considered using XQRS ?, you can create custom REST endpoints with ease using intuitive Function Annotations much like one would do with JAX-RS or Java Spring REST Services. It can be installed and used with or with-out Gradle.

You can keep your custom logic either in these REST functions, or you could write your custom logic in JavaScript code that you then import/invoke from these functions. XQRS provides you with total flexibility. You can make beautiful REST APIs with Human-Friendly URLs that sit directly on MarkLogic, taking complete control of the URL Path. It's solid as a rock and unit tested to death. It scales with MarkLogic - i.e. you add more MarkLogic e-nodes and you automatically scale your REST servers, it's totally free and open source. The project is actively maintained and support is available on GitHub. If you've got a Swagger/OpenAPI interface file, you can generate a MarkLogic Stub too which could save you a lot of time.

Functions simply become available as REST Services based on the Annotations you place on them - and you can pass query/form parameters, cookies and the request body via the Annotations. Check out this snippet for a sample flavour.

declare
  %rest:path("/factory/warehouse/wheel/{$wheel-id}")
  %rest:GET
function get-wheel($wheel-id as xs:string) {
  fn:doc($wheel-id)
};

declare
  %rest:path("/factory/warehouse/wheel/{$wheel-id}")
  %rest:PUT("{$doc}")
  %xdmp:update
function put-wheel($wheel-id as xs:string, $doc as document-node(element())) {
  xdmp:document-insert($wheel-id, $doc, map:entry("collections", "wheels"))
};

declare
  %rest:path("/factory/warehouse/wheel")
function list-wheels() {
  <wheels>{
    fn:collection("wheels")
  }</wheels>
};
1
votes

Both responses to date are correct and accurate and help, however the original question is self-answered.

I've gone through the REST API documentation to create one by using CURL so that we can use default APIs provided by MarkLogic.

That is in fact the answer to your question as stated.

1
votes

To manually deploy ML rest API, please follow below steps

  • Create Module DB
  • Create App Server Provide details for Module DB, database, port, URL rewriter
  • Deploy xquery, xsl using qConsole

    let URI = path of file

    let path = xdmp:document-get($FilePath)

    xdmp:document-insert($URI,$path,(), ())

    where endpoints.xqy will contains defined custom endpoint for your rest API and internally you can call search:search function to call data from MarkLogic

    module namespace endpoints="urn:overstory:rest:modules:endpoints";

    declare namespace rest="http://marklogic.com/appservices/rest";

    (: ---------------------------------------------------------------------- :)

    declare private variable $endpoints as element(rest:options) :=

         <request uri="^/getcontent$" endpoint="<xqy file" user-params="allow">
            <http method="GET"/>
        </request>
    

Hope this will help