Skip to content

Commit a9f1011

Browse files
Merge pull request #456 from nate-fr/master
Fix issue #403 - implement an interface for mocking and DI
2 parents 8785065 + a960d5f commit a9f1011

File tree

2 files changed

+77
-2
lines changed

2 files changed

+77
-2
lines changed

src/SendGrid/ISendGridClient.cs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// <copyright file="SendGridClient.cs" company="SendGrid">
2+
// Copyright (c) SendGrid. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4+
// </copyright>
5+
6+
using System;
7+
8+
namespace SendGrid
9+
{
10+
using System.Collections.Generic;
11+
using System.Net.Http;
12+
using System.Net.Http.Headers;
13+
using System.Threading;
14+
using System.Threading.Tasks;
15+
using Helpers.Mail;
16+
17+
/// <summary>
18+
/// A HTTP client wrapper for interacting with SendGrid's API
19+
/// </summary>
20+
public interface ISendGridClient
21+
{
22+
/// <summary>
23+
/// Gets or sets the path to the API resource.
24+
/// </summary>
25+
string UrlPath { get; set; }
26+
27+
/// <summary>
28+
/// Gets or sets the API version.
29+
/// </summary>
30+
string Version { get; set; }
31+
32+
/// <summary>
33+
/// Gets or sets the request media type.
34+
/// </summary>
35+
string MediaType { get; set; }
36+
37+
/// <summary>
38+
/// Add the authorization header, override to customize
39+
/// </summary>
40+
/// <param name="header">Authorization header</param>
41+
/// <returns>Authorization value to add to the header</returns>
42+
AuthenticationHeaderValue AddAuthorization( KeyValuePair<string, string> header );
43+
44+
/// <summary>
45+
/// Make the call to the API server, override for testing or customization
46+
/// </summary>
47+
/// <param name="request">The parameters for the API call</param>
48+
/// <param name="cancellationToken">Cancel the asynchronous call</param>
49+
/// <returns>Response object</returns>
50+
Task<Response> MakeRequest( HttpRequestMessage request, CancellationToken cancellationToken = default( CancellationToken ) );
51+
52+
/// <summary>
53+
/// Prepare for async call to the API server
54+
/// </summary>
55+
/// <param name="method">HTTP verb</param>
56+
/// <param name="requestBody">JSON formatted string</param>
57+
/// <param name="queryParams">JSON formatted query paramaters</param>
58+
/// <param name="urlPath">The path to the API endpoint.</param>
59+
/// <param name="cancellationToken">Cancel the asynchronous call.</param>
60+
/// <returns>Response object</returns>
61+
/// <exception cref="Exception">The method will NOT catch and swallow exceptions generated by sending a request
62+
/// through the internal http client. Any underlying exception will pass right through.
63+
/// In particular, this means that you may expect
64+
/// a TimeoutException if you are not connected to the internet.</exception>
65+
Task<Response> RequestAsync( SendGridClient.Method method, string requestBody = null, string queryParams = null, string urlPath = null, CancellationToken cancellationToken = default( CancellationToken ) );
66+
67+
/// <summary>
68+
/// Make a request to send an email through SendGrid asychronously.
69+
/// </summary>
70+
/// <param name="msg">A SendGridMessage object with the details for the request.</param>
71+
/// <param name="cancellationToken">Cancel the asychronous call.</param>
72+
/// <returns>A Response object.</returns>
73+
Task<Response> SendEmailAsync( SendGridMessage msg, CancellationToken cancellationToken = default( CancellationToken ) );
74+
}
75+
}

src/SendGrid/SendGridClient.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ namespace SendGrid
2020
/// <summary>
2121
/// A HTTP client wrapper for interacting with SendGrid's API
2222
/// </summary>
23-
public class SendGridClient
24-
{
23+
public class SendGridClient : ISendGridClient
24+
{
2525
/// <summary>
2626
/// Gets or sets the path to the API resource.
2727
/// </summary>

0 commit comments

Comments
 (0)