Skip to content

Commit

Permalink
Rename JSON API endpoints
Browse files Browse the repository at this point in the history
CHANGELOG_BEGIN

[JSON API - Experimental] Rename JSON API endpoints. See #4289.
    /command/create => /create
    /command/exercise => /exercise
    /contracts/lookup => /fetch
    /contracts/search => /query
    /contracts/searchForever => /stream/query
    /contracts/lookupForever => /stream/fetch

CHANGELOG_END
  • Loading branch information
leo-da committed Feb 6, 2020
1 parent 0583f57 commit 797981e
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 91 deletions.
2 changes: 1 addition & 1 deletion daml-assistant/daml-helper/src/DA/Daml/Helper/Run.hs
Expand Up @@ -743,7 +743,7 @@ withJsonApi (SandboxPort sandboxPort) (JsonApiPort jsonApiPort) args a = do
let headers =
[ ("Authorization", "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJsZWRnZXJJZCI6Ik15TGVkZ2VyIiwiYXBwbGljYXRpb25JZCI6ImZvb2JhciIsInBhcnR5IjoiQWxpY2UifQ.4HYfzjlYr1ApUDot0a6a4zB49zS_jrwRUOCkAiPMqo0")
] :: HTTP.RequestHeaders
waitForHttpServer (putStr "." *> threadDelay 500000) ("http://localhost:" <> show jsonApiPort <> "/contracts/search") headers
waitForHttpServer (putStr "." *> threadDelay 500000) ("http://localhost:" <> show jsonApiPort <> "/query") headers
a ph

-- | Whether `daml start` should open a browser automatically.
Expand Down
Expand Up @@ -538,8 +538,8 @@ quickstartTests quickstartDir mvnDir = testGroup "quickstart"
let headers =
[ ("Authorization", "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJsZWRnZXJJZCI6Ik15TGVkZ2VyIiwiYXBwbGljYXRpb25JZCI6ImZvb2JhciIsInBhcnR5IjoiQWxpY2UifQ.4HYfzjlYr1ApUDot0a6a4zB49zS_jrwRUOCkAiPMqo0")
] :: RequestHeaders
waitForHttpServer (threadDelay 100000) ("http://localhost:" <> show jsonApiPort <> "/contracts/search") headers
req <- parseRequest $ "http://localhost:" <> show jsonApiPort <> "/contracts/search"
waitForHttpServer (threadDelay 100000) ("http://localhost:" <> show jsonApiPort <> "/query") headers
req <- parseRequest $ "http://localhost:" <> show jsonApiPort <> "/query"
req <- pure req { requestHeaders = headers }
manager <- newManager defaultManagerSettings
resp <- httpLbs req manager
Expand Down
18 changes: 9 additions & 9 deletions docs/source/json-api/index.rst
Expand Up @@ -289,7 +289,7 @@ See the request documentation below on how to create an instance of ``Iou`` cont
HTTP Request
------------

- URL: ``/command/create``
- URL: ``/create``
- Method: ``POST``
- Content-Type: ``application/json``
- Content:
Expand Down Expand Up @@ -394,7 +394,7 @@ The JSON command below, demonstrates how to exercise ``Iou_Transfer`` choice on
HTTP Request
------------

- URL: ``/command/exercise``
- URL: ``/exercise``
- Method: ``POST``
- Content-Type: ``application/json``
- Content:
Expand Down Expand Up @@ -493,7 +493,7 @@ The JSON command below, demonstrates how to exercise ``Archive`` choice on ``Acc
HTT Request
-----------

- URL: ``/command/exercise``
- URL: ``/exercise``
- Method: ``POST``
- Content-Type: ``application/json``
- Content:
Expand Down Expand Up @@ -526,7 +526,7 @@ Fetch Contract by Contract ID
HTTP Request
------------

- URL: ``/contracts/lookup``
- URL: ``/fetch``
- Method: ``POST``
- Content-Type: ``application/json``
- Content:
Expand Down Expand Up @@ -589,7 +589,7 @@ Fetch Contract by Key
HTTP Request
------------

- URL: ``/contracts/lookup``
- URL: ``/fetch``
- Method: ``POST``
- Content-Type: ``application/json``
- Content:
Expand Down Expand Up @@ -658,7 +658,7 @@ Note that the retrieved contracts do not get persisted into query store database
HTTP Request
------------

- URL: ``/contracts/search``
- URL: ``/query``
- Method: ``GET``
- Content: <EMPTY>

Expand All @@ -675,7 +675,7 @@ List currently active contracts that match a given query.
HTTP Request
------------

- URL: ``/contracts/search``
- URL: ``/query``
- Method: ``POST``
- Content-Type: ``application/json``
- Content:
Expand Down Expand Up @@ -804,7 +804,7 @@ Streaming API
Contracts Query Stream
----------------------

- URL: ``/contracts/searchForever``
- URL: ``/stream/query``
- Scheme: ``ws``
- Protocol: ``WebSocket``

Expand Down Expand Up @@ -911,7 +911,7 @@ stream will continue in these cases, rather than terminating.
Some notes on behavior:

1. Each result array means "this is what would have changed if you just
polled ``/contracts/search`` iteratively." In particular, just as
polled ``/query`` iteratively." In particular, just as
polling search can "miss" contracts (as a create and archive can be
paired between polls), such contracts may or may not appear in any
result object.
Expand Down
6 changes: 3 additions & 3 deletions docs/source/json-api/search-query-language.rst
@@ -1,10 +1,10 @@
.. Copyright (c) 2020 The DAML Authors. All rights reserved.
.. SPDX-License-Identifier: Apache-2.0
/contracts/search query language
################################
Query language
##############

The body of ``POST /contracts/search`` looks like so::
The body of ``POST /query`` looks like so::

{"templateIds": [...template IDs...],
"query": {...query elements...}}
Expand Down
12 changes: 6 additions & 6 deletions language-support/ts/daml-ledger/index.ts
Expand Up @@ -145,7 +145,7 @@ class Ledger {
*/
async query<T extends object, K, I extends string>(template: Template<T, K, I>, query?: Query<T>): Promise<CreateEvent<T, K, I>[]> {
const payload = {templateIds: [template.templateId], query};
const json = await this.submit('contracts/search', payload);
const json = await this.submit('query', payload);
return jtv.Result.withException(jtv.array(decodeCreateEvent(template)).run(json));
}

Expand All @@ -165,7 +165,7 @@ class Ledger {
templateId: template.templateId,
contractId,
};
const json = await this.submit('contracts/lookup', payload);
const json = await this.submit('fetch', payload);
return jtv.Result.withException(jtv.oneOf(jtv.constant(null), decodeCreateEvent(template)).run(json));
}

Expand All @@ -180,7 +180,7 @@ class Ledger {
templateId: template.templateId,
key,
};
const json = await this.submit('contracts/lookup', payload);
const json = await this.submit('fetch', payload);
return jtv.Result.withException(jtv.oneOf(jtv.constant(null), decodeCreateEvent(template)).run(json));
}

Expand All @@ -199,7 +199,7 @@ class Ledger {
templateId: template.templateId,
payload,
};
const json = await this.submit('command/create', command);
const json = await this.submit('create', command);
return jtv.Result.withException(decodeCreateEvent(template).run(json));
}

Expand All @@ -213,7 +213,7 @@ class Ledger {
choice: choice.choiceName,
argument,
};
const json = await this.submit('command/exercise', payload);
const json = await this.submit('exercise', payload);
// Decode the server response into a tuple.
const responseDecoder: jtv.Decoder<{exerciseResult: R; contracts: Event<object>[]}> = jtv.object({
exerciseResult: choice.resultDecoder(),
Expand All @@ -236,7 +236,7 @@ class Ledger {
choice: choice.choiceName,
argument,
};
const json = await this.submit('command/exercise', payload);
const json = await this.submit('exercise', payload);
// Decode the server response into a tuple.
const responseDecoder: jtv.Decoder<{exerciseResult: R; contracts: Event<object>[]}> = jtv.object({
exerciseResult: choice.resultDecoder(),
Expand Down
4 changes: 2 additions & 2 deletions language-support/ts/daml-react/hooks.ts
Expand Up @@ -37,7 +37,7 @@ export type QueryResult<T extends object, K> = {
// to work in `useQuery` when using the default value for `queryFactory`.
const emptyQuery = {};

/// React Hook for a query against the `/contracts/search` endpoint of the JSON API.
/// React Hook for a query against the `/query` endpoint of the JSON API.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const useQuery = <T extends object, K>(template: Template<T, K>, queryFactory: () => Query<T> = () => emptyQuery as Query<T>, queryDeps?: readonly any[]): QueryResult<T, K> => {
const state = useDamlState();
Expand Down Expand Up @@ -69,7 +69,7 @@ export type FetchResult<T extends object, K> = {
loading: boolean;
}

/// React Hook for a lookup by key against the `/contracts/lookup` endpoint of the JSON API.
/// React Hook for a lookup by key against the `/fetch` endpoint of the JSON API.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const useFetchByKey = <T extends object, K>(template: Template<T, K>, keyFactory: () => K, keyDeps?: readonly any[]): FetchResult<T, K> => {
const state = useDamlState();
Expand Down
Expand Up @@ -54,7 +54,7 @@ class Endpoints(
command orElse contracts orElse parties

lazy val command: PartialFunction[HttpRequest, Future[HttpResponse]] = {
case req @ HttpRequest(POST, Uri.Path("/command/create"), _, _, _) =>
case req @ HttpRequest(POST, Uri.Path("/create"), _, _, _) =>
val et: ET[JsValue] = for {
t3 <- FutureUtil.eitherT(input(req)): ET[(Jwt, JwtPayload, String)]

Expand All @@ -76,7 +76,7 @@ class Endpoints(

httpResponse(et)

case req @ HttpRequest(POST, Uri.Path("/command/exercise"), _, _, _) =>
case req @ HttpRequest(POST, Uri.Path("/exercise"), _, _, _) =>
val et: ET[JsValue] = for {
t3 <- eitherT(input(req)): ET[(Jwt, JwtPayload, String)]

Expand Down Expand Up @@ -138,21 +138,21 @@ class Endpoints(
SprayJson.encode(as).leftMap(e => ServerError(e.shows))

lazy val contracts: PartialFunction[HttpRequest, Future[HttpResponse]] = {
case req @ HttpRequest(POST, Uri.Path("/contracts/lookup"), _, _, _) =>
case req @ HttpRequest(POST, Uri.Path("/fetch"), _, _, _) =>
val et: ET[JsValue] = for {
input <- FutureUtil.eitherT(input(req)): ET[(Jwt, JwtPayload, String)]

(jwt, jwtPayload, reqBody) = input

_ = logger.debug(s"/contracts/lookup reqBody: $reqBody")
_ = logger.debug(s"/fetch reqBody: $reqBody")

cl <- either(
decoder
.decodeContractLocator(reqBody)
.leftMap(e => InvalidUserInput(e.shows))
): ET[domain.ContractLocator[LfValue]]

_ = logger.debug(s"/contracts/lookup cl: $cl")
_ = logger.debug(s"/fetch cl: $cl")

ac <- eitherT(
handleFutureFailure(contractsService.lookup(jwt, jwtPayload, cl))
Expand All @@ -166,7 +166,7 @@ class Endpoints(

httpResponse(et)

case req @ HttpRequest(GET, Uri.Path("/contracts/search"), _, _, _) =>
case req @ HttpRequest(GET, Uri.Path("/query"), _, _, _) =>
val sourceF: Future[Error \/ SearchResult[Error \/ JsValue]] = input(req).map {
_.map {
case (jwt, jwtPayload, _) =>
Expand All @@ -184,7 +184,7 @@ class Endpoints(

httpResponse(sourceF)

case req @ HttpRequest(POST, Uri.Path("/contracts/search"), _, _, _) =>
case req @ HttpRequest(POST, Uri.Path("/query"), _, _, _) =>
val sourceF: Future[Error \/ SearchResult[Error \/ JsValue]] = input(req).map {
_.flatMap {
case (jwt, jwtPayload, reqBody) =>
Expand Down
Expand Up @@ -55,7 +55,7 @@ class WebsocketEndpoints(
import WebsocketEndpoints._

lazy val transactionWebSocket: PartialFunction[HttpRequest, Future[HttpResponse]] = {
case req @ HttpRequest(GET, Uri.Path("/contracts/searchForever"), _, _, _) =>
case req @ HttpRequest(GET, Uri.Path("/stream/query"), _, _, _) =>
Future.successful(
(for {
upgradeReq <- req.header[UpgradeToWebSocket] \/> InvalidUserInput(
Expand Down

0 comments on commit 797981e

Please sign in to comment.