Skip to content
This repository was archived by the owner on Apr 21, 2022. It is now read-only.

Commit 6f40fec

Browse files
committed
Use class level generic for base object client
1 parent 290385a commit 6f40fec

File tree

3 files changed

+9
-10
lines changed

3 files changed

+9
-10
lines changed

api-example-csharp/ApiClient.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ RestClient Client()
7979
}
8080

8181

82-
public abstract class BaseObjectClient
82+
public abstract class BaseObjectClient<T> where T : new()
8383
{
8484
protected ApiClient Client;
8585

@@ -98,7 +98,7 @@ string BaseResource
9898

9999
abstract public string Resource { get; }
100100

101-
public List<T> List<T>()
101+
public List<T> List()
102102
{
103103
var request = new RestRequest();
104104
request.RequestFormat = DataFormat.Json;
@@ -107,15 +107,15 @@ public List<T> List<T>()
107107
return Client.Execute<List<T>>(request);
108108
}
109109

110-
public T Fetch<T>(int id) where T : new()
110+
public T Fetch(int id)
111111
{
112112
var request = new RestRequest();
113113
request.RequestFormat = DataFormat.Json;
114114
request.Resource = BaseResource + Resource + id;
115115
return Client.Execute<T>(request);
116116
}
117117

118-
public T Create<T>(T obj) where T : new()
118+
public T Create(T obj)
119119
{
120120
var request = new RestRequest(Method.POST);
121121
request.RequestFormat = DataFormat.Json;
@@ -124,7 +124,7 @@ public List<T> List<T>()
124124
return Client.Execute<T>(request);
125125
}
126126

127-
public T Update<T>(int id, T obj) where T : new()
127+
public T Update(int id, T obj)
128128
{
129129
var request = new RestRequest(Method.PUT);
130130
request.RequestFormat = DataFormat.Json;

api-example-csharp/Inventory.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32

43

54
namespace ApiTest
@@ -46,7 +45,7 @@ public class Inventory
4645
}
4746

4847

49-
public class InventoryClient : BaseObjectClient
48+
public class InventoryClient : BaseObjectClient<Inventory>
5049
{
5150
public InventoryClient(ApiClient client) : base(client) { }
5251

api-example-csharp/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ static int Main(string[] args)
2626
inventory = inventory_client.Create(inventory);
2727

2828
// List inventory
29-
foreach (var i in inventory_client.List<Inventory>())
29+
foreach (var i in inventory_client.List())
3030
{
3131
// i.id
3232
}
3333

3434
// Get inventory
35-
inventory = inventory_client.Fetch<Inventory>(inventory.id);
35+
inventory = inventory_client.Fetch(inventory.id);
3636

3737
// Update inventory
3838
inventory.description = "New Description";

0 commit comments

Comments
 (0)