Skip to content

Commit 4a9190d

Browse files
committed
refactor, add README and fix double hook
1 parent a87e813 commit 4a9190d

File tree

4 files changed

+42
-5
lines changed

4 files changed

+42
-5
lines changed

resources/sdk/pureclouddotnet/extensions/Client/AbstractHttpClient.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public void SetPostRequestHook(PostRequestHook hook)
9494
}
9595

9696
/// <summary>
97-
/// Applies the pre-request hook if available
97+
/// Applies the pre-request hook if set
9898
/// </summary>
9999
/// <param name="request">The original request</param>
100100
/// <returns>The modified request</returns>
@@ -116,7 +116,7 @@ protected IHttpRequest ApplyPreRequestHook(IHttpRequest request)
116116
}
117117

118118
/// <summary>
119-
/// Applies the post-request hook if available
119+
/// Applies the post-request hook if set
120120
/// </summary>
121121
/// <param name="response">The original response</param>
122122
/// <returns>The modified response</returns>

resources/sdk/pureclouddotnet/extensions/Client/DefaultHttpClient.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ private bool ValidateServerCertificate(object sender, X509Certificate certificat
172172
/// </summary>
173173
public override IHttpResponse Execute(IHttpRequest httpRequest)
174174
{
175-
httpRequest = ApplyPreRequestHook(httpRequest);
176175
IHttpResponse resp;
177176

178177
if (usingMTLS) //using HttpWebRequest
@@ -181,14 +180,16 @@ public override IHttpResponse Execute(IHttpRequest httpRequest)
181180
}
182181
else //using RestSharp (HttpClient)
183182
{
183+
httpRequest = ApplyPreRequestHook(httpRequest);
184184
var request = PrepareRestRequest((HttpRequestOptions)httpRequest);
185185

186186
var restResp = restClient.Execute(request);
187187

188188
resp = ConvertToHttpResponse(restResp);
189+
resp = ApplyPostRequestHook(resp);
189190
}
190191

191-
return ApplyPostRequestHook(resp);
192+
return resp;
192193
}
193194

194195
private IHttpResponse ConvertToHttpResponse(RestResponse response)

resources/sdk/pureclouddotnet/templates/README.mustache

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,39 @@ var certPass = "x509Password";
633633
Configuration.Default.ApiClient.SetMTLSCertificates(certPath, certPass);
634634
```
635635

636+
### Using Pre Commit and Post Commit Hooks
637+
638+
For any custom requirements like pre validations or post cleanups (for ex: OCSP and CRL validation), we can inject the prehook and posthook functions.
639+
The SDK's default client will make sure the injected hook functions are executed.
640+
641+
The Pre/Post Hook functions must have the following method signature shown below
642+
643+
```csharp
644+
IHttpRequest preHook(IHttpRequest request)
645+
IHttpResponse postHook(IHttpRepsonse response)
646+
```
647+
648+
Here is an example of using a Pre Hook function:
649+
650+
```csharp
651+
private IHttpRequest preHook(IHttpRequest request)
652+
{
653+
try {
654+
Console.WriteLine("Running PreHook: Certificate Validation Checks");
655+
656+
// custom validation here
657+
658+
Console.WriteLine("Certificate Validation Complete");
659+
} catch (Exception ex) {
660+
Console.WriteLine($"Error in prehook validation: {ex.Message}");
661+
throw ex; // Reject request if validation fails
662+
}
663+
}
664+
665+
Configuration.Default.ApiClient.HttpClient.SetPreRequestHook(preHook);
666+
667+
```
668+
636669
### Building from Source
637670

638671
If you're working inside Visual Studio, adding the files to your project allows you to edit and build inside an IDE.

resources/sdk/pureclouddotnet/templates/test-SdkTests.mustache

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ namespace {{packageName}}.Tests
341341
Console.WriteLine("Running Pre-Request Hook");
342342
var options = (HttpRequestOptions)request;
343343
344-
options.AddHeaderParam("X-Test-Header", "Pre-Hook-Test");
344+
options.AddHeaderParam("Test-Header", "Pre-Hook-Test");
345345
346346
Console.WriteLine($"Pre-Hook: Making {options.Method} request to {options.Url}");
347347
@@ -509,6 +509,9 @@ namespace {{packageName}}.Tests
509509
Console.WriteLine($"Deleted user with ID {userId}");
510510
}
511511

512+
///<Summary>
513+
/// CleanUp
514+
///</Summary>
512515
[Test, Order(7)]
513516
public void CleanUp()
514517
{

0 commit comments

Comments
 (0)