Esempio n. 1
0
def test_endpoint_list():
  r = app.post_json('/users', {'username': '******', 'password': '******'})
  assert r.status_int == 201

  r = app.post_json('/comments', {'body': 'What a nice API !'})
  assert r.status_int == 201

  r = app.get('/')
  print(r)

  json = r.json
  assert len(json['_links']) == 2
  assert { 'rel': 'users', 'href': build_uri('/users') } in json['_links']
  assert { 'rel': 'comments', 'href': build_uri('/comments') } in json['_links']
Esempio n. 2
0
File: db.py Progetto: pkgr/APIMorph
    def get_links(self):
        links = []
        links.append({"rel": "self", "href": "/{0}/{1}".format(self.collection, self.id)})

        if "_links" in self.document:
            links.extend(self.document["_links"])

        for link in links:
            if "http" not in link["href"]:
                link["href"] = build_uri(link["href"])

        return links
Esempio n. 3
0
File: db.py Progetto: pkgr/APIMorph
    def get_links(self, page, page_size):
        links = []
        if page > 1:
            links.append({"rel": "prev", "href": "/{0}?page={1}&size={2}".format(self.collection, page - 1, page_size)})

        if page * page_size < self.cursor.count():
            links.append({"rel": "next", "href": "/{0}?page={1}&size={2}".format(self.collection, page + 1, page_size)})

        for link in links:
            if "http" not in link["href"]:
                link["href"] = build_uri(link["href"])

        return {"links_": links}
Esempio n. 4
0
    def all(self):
        uri = build_uri(PROVIDER_URL, self.path)
        parameters = {PROVIDER_LIMIT_PARAMETER: self.limit}

        response = get(uri, params=parameters)

        status_code = response.status_code
        if status_code != self.success_code:
            raise MetadataProviderError(f"Got an unexpected status code: {status_code}")

        try:
            return response.json()
        except:
            raise MetadataProviderError(f"Could not parse response: '{response.text}'")
Esempio n. 5
0
def create_resource(endpoint):
  endpoint_metadata = { '_id': endpoint, 'rel': endpoint, 'href': build_uri('/' + endpoint) }
  
  try:
    mongo[META_COLLECTION].insert(endpoint_metadata)
  except DuplicateKeyError: pass

  resource_id = mongo[endpoint].insert(request.json)
  resource = MongoResource(endpoint, resource_id)

  headers={
      "Content-Type": "application/json; charset=utf8",
      "Location": "/users/{0}".format(resource_id)
      }

  return HTTPResponse(status=201, headers=headers, body=dumps(resource.to_response()))