Commit b07a588c authored by Laurent Wouters's avatar Laurent Wouters
Browse files

Rewriting hypermedia client in OCaml

parent 1f1ec7298bda
......@@ -15,7 +15,7 @@ let fetchPreviews = name =>
Fetch.fetchWithInit(
Hypermedia.baseUrl ++ "/" ++ name ++ "/",
Fetch.RequestInit.make(
~headers=Fetch.HeadersInit.make(Hypermedia.headersForResources),
~headers=Fetch.HeadersInit.makeWithArray(Hypermedia.headersForResources),
(),
),
)
......
......@@ -12,7 +12,7 @@ let fetchRootSchema = () =>
Fetch.fetchWithInit(
Hypermedia.baseUrl ++ "/schema",
Fetch.RequestInit.make(
~headers=Fetch.HeadersInit.make(Hypermedia.headersForSchema),
~headers=Fetch.HeadersInit.makeWithArray(Hypermedia.headersForSchema),
(),
),
)
......
type reference = {ref: string};
type schemaLink = {
href: string,
rel: string,
submissionSchema: reference,
targetSchema: reference,
title: string,
};
type rootSchema = {
links: list(schemaLink),
title: string,
};
type entityPreview = {
id: string,
title: string,
meta: string,
};
type itemLink = {
anchor: string,
href: string,
rel: string,
};
type valueSchema =
| Primitive(string)
| Object(objectSchema)
| Array(arraySchema)
and propertySchema = {
name: string,
meta: valueSchema,
}
and objectSchema = {
links: list(itemLink),
properties: list(propertySchema),
}
and arraySchema = {
title: string,
links: list(schemaLink),
items: valueSchema,
};
type propertyValue =
| Simple(string)
| Collection(list(string));
type property = {
name: string,
value: propertyValue,
};
type entity = {
id: string,
properties: Js.Dict.t(propertyValue),
};
module Decode = {
let reference = json => Json.Decode.{ref: json |> field("$ref", string)};
let schemaLink = json =>
Json.Decode.{
href: json |> field("href", string),
rel: json |> field("rel", string),
submissionSchema: json |> field("submissionSchema", reference),
targetSchema: json |> field("targetSchema", reference),
title: json |> field("title", string),
};
let rootSchema = json =>
Json.Decode.{
title: json |> field("title", string),
links: json |> field("links", Json.Decode.list(schemaLink)),
};
let entityPreview = json =>
Json.Decode.{
id: json |> field("id", string),
title: json |> field("title", string),
meta: json |> field("type", string),
};
let entityPreviews = json => Json.Decode.list(entityPreview, json);
let propertyValue = (json: Js.Json.t) =>
if (Js_array.isArray(json)) Json.Decode.list(Json.Decode.string, json)
else Json.Decode.string(json);
};
let baseUrl = "http://localhost:6543";
let headersForSchema = [%bs.raw
{|
{
"Content-Type": "application/schema+json",
"Accept": "application/schema+json"
}
|}
];
let headersForResources = [%bs.raw
{|
{
"Content-Type": "application/json",
"Accept": "application/json"
}
|}
];
\ No newline at end of file
let baseUrl = "http://localhost:6543";;
let headersForSchema = [|
("Content-Type", "application/schema+json");
("Accept", "application/schema+json")
|];;
let headersForResources = [|
("Content-Type", "application/json");
("Accept", "application/json")
|];;
type reference = { ref: string }
type schemaLink = {
href: string;
rel: string;
submissionSchema: reference;
targetSchema: reference;
title: string;
}
type rootSchema = {
links: schemaLink list;
title: string;
}
type entityPreview = {
id: string;
title: string;
meta: string;
}
type itemLink = {
anchor: string;
href: string;
rel: string;
}
type valueSchema =
| Primitive of string
| Object of objectSchema
| Array of arraySchema
and propertySchema = {
name: string;
meta: valueSchema;
}
and objectSchema = {
links: itemLink list;
properties: propertySchema list;
}
and arraySchema = {
title: string;
links: schemaLink list;
items: valueSchema;
}
type propertyValue =
| Simple of string
| Collection of string list;;
type property = {
name: string;
value: propertyValue;
}
type entity = {
id: string;
properties: property list;
}
module Decode = struct
let reference json = {
ref = (Json.Decode.field "$ref" Json.Decode.string json)
};;
let schemaLink json = {
href = (Json.Decode.field "href" Json.Decode.string json);
rel = (Json.Decode.field "rel" Json.Decode.string json);
submissionSchema = (Json.Decode.field "submissionSchema" reference json);
targetSchema = (Json.Decode.field "targetSchema" reference json);
title = (Json.Decode.field "title" Json.Decode.string json);
};;
let rootSchema json = {
title = (Json.Decode.field "title" Json.Decode.string json);
links = (Json.Decode.field "links" (fun x -> Json.Decode.list schemaLink x) json)
};;
let entityPreview json = {
id = (Json.Decode.field "id" Json.Decode.string json);
title = (Json.Decode.field "title" Json.Decode.string json);
meta = (Json.Decode.field "type" Json.Decode.string json);
};;
let entityPreviews json = (Json.Decode.list entityPreview json);;
end
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment