Skip to content

Commit da2b8f4

Browse files
Copilotbastianeicher
authored andcommitted
Add code examples (C#, Java, Kotlin, TypeScript) to all endpoint documentation
1 parent 301df1b commit da2b8f4

13 files changed

Lines changed: 774 additions & 0 deletions

File tree

docs/endpoints/entry.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,61 @@
33
Represent the top-level URI of an API. Used to address the various resources of the API.
44

55
The constructor of the entry endpoint requires you to specify the APIs root URL. It also provides optional parameters to override the default HTTP client, JSON serializer, [error handler](../error-handling/index.md) and [link handler](../link-handling/index.md).
6+
7+
## Usage
8+
9+
=== "C#"
10+
11+
```csharp
12+
// Create an entry endpoint
13+
var client = new EntryEndpoint(new Uri("http://example.com/api/"));
14+
15+
// With custom HTTP client
16+
var httpClient = new HttpClient { Timeout = TimeSpan.FromSeconds(30) };
17+
var client = new EntryEndpoint(httpClient);
18+
19+
// Access child endpoints
20+
var contacts = new CollectionEndpoint<Contact>(client, "contacts");
21+
```
22+
23+
=== "Java"
24+
25+
```java
26+
// Create an entry endpoint
27+
EntryEndpoint client = new EntryEndpoint(URI.create("http://example.com/api/"));
28+
29+
// With custom HTTP client
30+
HttpClient httpClient = HttpClient.newBuilder()
31+
.connectTimeout(Duration.ofSeconds(30))
32+
.build();
33+
EntryEndpoint client = new EntryEndpoint(httpClient);
34+
35+
// Access child endpoints
36+
CollectionEndpoint<Contact> contacts = new CollectionEndpoint<>(client, "contacts", Contact.class);
37+
```
38+
39+
=== "Kotlin"
40+
41+
```kotlin
42+
// Create an entry endpoint
43+
val client = EntryEndpoint(URI.create("http://example.com/api/"))
44+
45+
// With custom HTTP client
46+
val httpClient = HttpClient.newBuilder()
47+
.connectTimeout(Duration.ofSeconds(30))
48+
.build()
49+
val client = EntryEndpoint(httpClient)
50+
51+
// Access child endpoints
52+
val contacts = CollectionEndpoint(client, "contacts", Contact::class.java)
53+
```
54+
55+
=== "TypeScript"
56+
57+
```typescript
58+
// Create an entry endpoint
59+
const client = new EntryEndpoint(new URL("http://example.com/api/"));
60+
61+
// Access child endpoints
62+
const contacts = new CollectionEndpoint<Contact>(client, "contacts");
63+
```

docs/endpoints/generic/collection.md

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,131 @@ Endpoint for a collection of entities addressable as [Element endpoints](element
1212
| Set All | Entities | - | `PUT` | Replaces the entire content of the collection with new entities. |
1313

1414
Extends [Indexer endpoint](indexer.md)
15+
16+
## Usage
17+
18+
TODO: Replace usage of `ContactEndpoint`
19+
20+
=== "C#"
21+
22+
```csharp
23+
var contacts = new CollectionEndpoint<Contact>(client, "contacts");
24+
25+
// Read all entities in the collection
26+
List<Contact> allContacts = await contacts.ReadAllAsync();
27+
28+
// Read a range of entities
29+
List<Contact> someContacts = await contacts.ReadRangeAsync(new RangeItemHeaderValue(0, 49));
30+
31+
// Create a new entity
32+
ContactEndpoint newContact = await contacts.CreateAsync(new Contact { Name = "John Doe" });
33+
34+
// Get an element by ID
35+
ContactEndpoint contact = contacts["123"];
36+
// or
37+
ContactEndpoint contact = contacts.Get("123");
38+
39+
// Update multiple entities
40+
await contacts.CreateAllAsync(new[] {
41+
new Contact { Name = "Alice" },
42+
new Contact { Name = "Bob" }
43+
});
44+
45+
// Replace entire collection
46+
await contacts.SetAllAsync(new[] {
47+
new Contact { Name = "Charlie" },
48+
new Contact { Name = "Diana" }
49+
});
50+
```
51+
52+
=== "Java"
53+
54+
```java
55+
CollectionEndpoint<Contact> contacts = new CollectionEndpoint<>(client, "contacts", Contact.class);
56+
57+
// Read all entities in the collection
58+
List<Contact> allContacts = contacts.readAll();
59+
60+
// Read a range of entities
61+
List<Contact> someContacts = contacts.readRange(Range.ofLength(0, 50));
62+
63+
// Create a new entity
64+
ContactEndpoint newContact = contacts.create(new Contact("John Doe"));
65+
66+
// Get an element by ID
67+
ContactEndpoint contact = contacts.get("123");
68+
69+
// Update multiple entities
70+
contacts.createAll(List.of(
71+
new Contact("Alice"),
72+
new Contact("Bob")
73+
));
74+
75+
// Replace entire collection
76+
contacts.setAll(List.of(
77+
new Contact("Charlie"),
78+
new Contact("Diana")
79+
));
80+
```
81+
82+
=== "Kotlin"
83+
84+
```kotlin
85+
val contacts = CollectionEndpoint(client, "contacts", Contact::class.java)
86+
87+
// Read all entities in the collection
88+
val allContacts = contacts.readAll()
89+
90+
// Read a range of entities
91+
val someContacts = contacts.readRange(Range.ofLength(0, 50))
92+
93+
// Create a new entity
94+
val newContact = contacts.create(Contact("John Doe"))
95+
96+
// Get an element by ID
97+
val contact = contacts["123"]
98+
// or
99+
val contact = contacts.get("123")
100+
101+
// Update multiple entities
102+
contacts.createAll(listOf(
103+
Contact("Alice"),
104+
Contact("Bob")
105+
))
106+
107+
// Replace entire collection
108+
contacts.setAll(listOf(
109+
Contact("Charlie"),
110+
Contact("Diana")
111+
))
112+
```
113+
114+
=== "TypeScript"
115+
116+
```typescript
117+
const contacts = new CollectionEndpoint<Contact>(client, "contacts");
118+
119+
// Read all entities in the collection
120+
const allContacts = await contacts.readAll();
121+
122+
// Read a range of entities
123+
const someContacts = await contacts.readRange({ from: 0, to: 49 });
124+
125+
// Create a new entity
126+
const newContact = await contacts.create({ name: "John Doe" });
127+
128+
// Get an element by ID
129+
const contact = contacts.get("123");
130+
131+
// Update multiple entities
132+
await contacts.createAll([
133+
{ name: "Alice" },
134+
{ name: "Bob" }
135+
]);
136+
137+
// Replace entire collection
138+
await contacts.setAll([
139+
{ name: "Charlie" },
140+
{ name: "Diana" }
141+
]);
142+
```

docs/endpoints/generic/element.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,90 @@ Endpoint for an individual resource.
99
| Set | Entity | Entity | `PUT` | Sets/replaces the entity. |
1010
| Merge | Entity | Entity | `PATCH` | Modifies the existing entity by merging changes. |
1111
| Delete | - | - | `DELETE` | Deletes the element. |
12+
13+
## Usage
14+
15+
=== "C#"
16+
17+
```csharp
18+
var contact = new ElementEndpoint<Contact>(client, "contacts/123");
19+
20+
// Check if the element exists
21+
bool exists = await contact.ExistsAsync();
22+
23+
// Read the entity
24+
Contact entity = await contact.ReadAsync();
25+
26+
// Update the entire entity
27+
await contact.SetAsync(new Contact { Name = "Jane Doe", Email = "jane@example.com" });
28+
29+
// Partially update the entity
30+
await contact.MergeAsync(new { Email = "newemail@example.com" });
31+
32+
// Delete the element
33+
await contact.DeleteAsync();
34+
```
35+
36+
=== "Java"
37+
38+
```java
39+
ElementEndpoint<Contact> contact = new ElementEndpoint<>(client, "contacts/123", Contact.class);
40+
41+
// Check if the element exists
42+
boolean exists = contact.exists();
43+
44+
// Read the entity
45+
Contact entity = contact.read();
46+
47+
// Update the entire entity
48+
contact.set(new Contact("Jane Doe", "jane@example.com"));
49+
50+
// Partially update the entity
51+
Map<String, Object> updates = Map.of("email", "newemail@example.com");
52+
contact.merge(updates);
53+
54+
// Delete the element
55+
contact.delete();
56+
```
57+
58+
=== "Kotlin"
59+
60+
```kotlin
61+
val contact = ElementEndpoint(client, "contacts/123", Contact::class.java)
62+
63+
// Check if the element exists
64+
val exists = contact.exists()
65+
66+
// Read the entity
67+
val entity = contact.read()
68+
69+
// Update the entire entity
70+
contact.set(Contact("Jane Doe", "jane@example.com"))
71+
72+
// Partially update the entity
73+
contact.merge(mapOf("email" to "newemail@example.com"))
74+
75+
// Delete the element
76+
contact.delete()
77+
```
78+
79+
=== "TypeScript"
80+
81+
```typescript
82+
const contact = new ElementEndpoint<Contact>(client, "contacts/123");
83+
84+
// Check if the element exists
85+
const exists = await contact.exists();
86+
87+
// Read the entity
88+
const entity = await contact.read();
89+
90+
// Update the entire entity
91+
await contact.set({ name: "Jane Doe", email: "jane@example.com" });
92+
93+
// Partially update the entity
94+
await contact.merge({ email: "newemail@example.com" });
95+
96+
// Delete the element
97+
await contact.delete();
98+
```

docs/endpoints/generic/indexer.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,57 @@ Endpoint that addresses child endpoints by ID.
55
| Method | Input | Result | HTTP Verb | Description |
66
| ------ | ----- | -------- | --------- | ------------------------------------------------------------------- |
77
| Get | ID | Endpoint | - | Get an [Element endpoint](element.md) for a specific child element. |
8+
9+
## Usage
10+
11+
=== "C#"
12+
13+
```csharp
14+
var users = new IndexerEndpoint<UserEndpoint>(client, "users");
15+
16+
// Get a child endpoint by ID
17+
UserEndpoint user = users["123"];
18+
// or
19+
UserEndpoint user = users.Get("123");
20+
21+
// Use the child endpoint
22+
User userData = await user.ReadAsync();
23+
```
24+
25+
=== "Java"
26+
27+
```java
28+
IndexerEndpoint<UserEndpoint> users = new IndexerEndpoint<>(client, "users", UserEndpoint.class);
29+
30+
// Get a child endpoint by ID
31+
UserEndpoint user = users.get("123");
32+
33+
// Use the child endpoint
34+
User userData = user.read();
35+
```
36+
37+
=== "Kotlin"
38+
39+
```kotlin
40+
val users = IndexerEndpoint(client, "users", UserEndpoint::class.java)
41+
42+
// Get a child endpoint by ID
43+
val user = users["123"]
44+
// or
45+
val user = users.get("123")
46+
47+
// Use the child endpoint
48+
val userData = user.read()
49+
```
50+
51+
=== "TypeScript"
52+
53+
```typescript
54+
const users = new IndexerEndpoint<UserEndpoint>(client, "users");
55+
56+
// Get a child endpoint by ID
57+
const user = users.get("123");
58+
59+
// Use the child endpoint
60+
const userData = await user.read();
61+
```

0 commit comments

Comments
 (0)