-
Notifications
You must be signed in to change notification settings - Fork 11
Creating
There are several ways to create a client, so you can choose the most suitable one.
The NClientGallery class contains already configured clients and factories. There are two types of gallery usage:
Static option:
IMyClient myClient = NClientGallery.Clients.GetRest()
.For<IMyClient>(host: "http://localhost:8080")
.Build();Creating the NClientGallery instance:
INClientGallery nclientGallery = new NClientGallery();
IMyClient myClient = nclientGallery.GetRest()
.For<IMyClient>(host: "http://localhost:8080")
.Build();The gallery also has pre-configured client factories, that will be convenient for creating clients with the same settings. Create them using NClientGallery.ClientFactories.* methods:
INClientFactory clientFactory = NClientGallery.ClientFactories.GetRest()
.For(factoryName: "myFactory")
.Build();
IMyClient myClient = clientFactory.Create<IMyClient>(host: "http://localhost:8080");Created through the gallery pre-configured clients and factories can be configured with your own settings using optional methods With...:
ILoggerFactory loggerFactory = ...;
IMyClient myClient = NClientGallery.Clients.GetRest()
.For<IMyClient>(host: "http://localhost:8080")
.WithLogging(loggerFactory)
.Build();The gallery contains several types of clients:
- Rest: REST client with System.Text.Json serializer. The request is considered unsuccessful if the response code is not equal to 2**.
- Custom: without pre-configuration.
The custom client is a special case because it has no presets, so you will have to fully configure it by yourself:
IMyClient myClient = NClientGallery.Clients.GetCustom()
.For<IMyClient>(host: "http://localhost:8080")
.UsingRestApi()
.UsingHttpTransport()
.UsingJsonSerializer()
...
.Build();Methods starting with Using... are required, you will not be able to skip them.
After all the required settings are set, the optional methods will become available.
Optional methods start with With....
You can create builders for clients without a gallery, there is a set of classes for this: NClientRestBuilder (Rest) and NClientBuilder (Custom).
Usage example:
INClientRestBuilder nclientRestBuilder = new NClientRestBuilder();
IMyClient myClient = nclientRestBuilder
.For<IMyClient>(host: "http://localhost:8080")
.Build();There are similar classes for factories: NClientRestFactoryBuilder and NClientFactoryBuilder.