Skip to content

Instantly share code, notes, and snippets.

@glenfant
Created January 16, 2015 17:31
Show Gist options
  • Save glenfant/e7e812c24e30447aaedc to your computer and use it in GitHub Desktop.
Save glenfant/e7e812c24e30447aaedc to your computer and use it in GitHub Desktop.
Attempt to record a new query as extended REST service in ML 7
# -*- coding: utf-8 -*-
"""
Provided a new XQuery, we record it as extended REST resource in ML 7 server
"""
import httplib
import urlparse
import requests
REST_HOST = 'localhost'
REST_PORT = 8003
REST_WRITER_USER = 'rest-writer'
REST_WRITER_PASSWORD = REST_WRITER_USER
REST_ADMIN_USER = 'rest-admin'
REST_ADMIN_PASSWORD = REST_ADMIN_USER
# Authentication tokens
writer_auth = requests.auth.HTTPDigestAuth(REST_WRITER_USER, REST_WRITER_PASSWORD)
admin_auth = requests.auth.HTTPDigestAuth(REST_ADMIN_USER, REST_ADMIN_PASSWORD)
XQUERY = """\
xquery version "1.0";
declare namespace html = "http://www.w3.org/1999/xhtml";
declare namespace refer = "http://schemas.assemblee-nationale.fr/referentiel";
fn:collection()//refer:uid
"""
resources_uri = 'http://{0}:{1}/v1/config/resources/'.format(REST_HOST, REST_PORT)
def human_status(resp):
"""Provides a human readable HTTP response status"""
st_code = resp.status_code
return "{0}: {1}".format(st_code, httplib.responses[st_code])
# Creating or updating an XQuery resource
# http://docs.marklogic.com/REST/PUT/v1/config/resources/%5Bname%5D
xquery_name = 'query1' # New name
query_url = urlparse.urljoin(resources_uri, xquery_name)
print query_url
# -> http://localhost:8003/v1/config/resources/query1
headers = {'content-type': 'application/xquery'}
params = {'method': ('get', 'post', 'put')}
resp = requests.put(query_url, params=params, data=XQUERY, headers=headers, auth=admin_auth)
# Response HTTP Status
print human_status(resp)
# -> 400: Bad Request
# Response body
print resp.content
# ->
# <rapi:error xmlns:rapi="http://marklogic.com/rest-api">
# <rapi:status-code>400</rapi:status-code>
# <rapi:status>Bad Request</rapi:status>
# <rapi:message-code>RESTAPI-INVALIDCONTENT</rapi:message-code>
# <rapi:message>
# RESTAPI-INVALIDCONTENT: (err:FOER0000) Invalid content: invalid query1
# extension: could not parse XQuery extension query1; please see the
# server error log for detail XDMP-IMPORTMOD: Cannot import Main Module
# /marklogic.rest.resource/query1/assets/resource.xqy; query1 either is
# not a valid module or does not provide extension functions (delete,
# get, put, post) in the http://marklogic.com/rest-api/resource/query1
# namespace
# </rapi:message>
# </rapi:error>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment