/// <summary> /// For Unit Testing, allowing mocking of the LevelUpHttpRestfulService. Note /// that the AssemblyInfo.cs file enables internals to be visible to the unit /// test project. /// </summary> internal static T Create <T>(AgentIdentifier identifier, LevelUpEnvironment enviornment, IRestfulService restService) where T : ILevelUpClientModule { var client = new LevelUpClient(GetDefaultLevelUpRequestEngine(identifier, enviornment, restService)); return((T)(object)client); }
/// <summary> /// Create a composed interface, containing a collection of the more fine-grained /// LevelUp client interfaces. /// </summary> /// <typeparam name="T">The type of composed interface to generate.</typeparam> /// <param name="identifier">An object that retains data about the caller /// (company name, etc.) as an informational and diagnostic tool for the /// LevelUp backend.</param> /// <param name="enviornment">The target enviornment (sandbox, production, etc.)</param> /// <returns>The LevelUp client interface of the specified type T.</returns> public static T CreateComposedInterface <T>(AgentIdentifier identifier, LevelUpEnvironment enviornment) where T : IComposedInterface { IRestfulService httpRestService = new LevelUpHttpRestfulService(); var client = new LevelUpClient(GetDefaultLevelUpRequestEngine(identifier, enviornment, httpRestService)); return((T)(object)client); }
/// <summary> /// Create a LevelUp client interface with a set timeout value /// </summary> /// <typeparam name="T">The type of LevelUp client interface to generate.</typeparam> /// <param name="identifier">An object that retains data about the caller /// (company name, etc.) as an informational and diagnostic tool for the /// LevelUp backend.</param> /// <param name="enviornment">The target enviornment (sandbox, production, etc.)</param> /// <param name="timeoutInMs">Time out value in milliseconds</param> /// <returns>The LevelUp client interface of the specified type T.</returns> public static T Create <T>(AgentIdentifier identifier, LevelUpEnvironment enviornment, int timeoutInMs) where T : ILevelUpClientModule { IRestfulService httpRestService = new LevelUpHttpRestfulService(timeoutInMs); var client = new LevelUpClient(httpRestService, identifier, enviornment); return((T)(object)client); }
/// <summary> /// Gets a LevelUpClient Module interface that will mimic, for any rest call, the provided /// expected RestResponse. Callers are expected to populate this expected response, including /// the json content string, based on what they expect to come over the wire according to the /// LevelUp platform API docs. /// </summary> /// <typeparam name="T">The specific type of ILevelUpClientModule to return.</typeparam> /// <param name="expectedResponse">The desired RestResponse that the caller would want platform /// to produce. Note that this response will be applied to every rest command. </param> /// <param name="expectedRequestUrl">If the caller wishes to verify the endpoint that is /// used in the request, they can do that here.</param> /// <param name="expectedAccessToken">If the caller wishes to verify authorization functionality /// for a specific command, they can provide an expected access token, without which the returned /// rest response will contain an unauthorized status code.</param> /// <param name="environmentToUse">Environment to use to build URI of the request.</param> internal static T GetMockedLevelUpModule <T>(RestResponse expectedResponse, string expectedRequestUrl, string expectedAccessToken, LevelUpEnvironment environmentToUse) where T : ILevelUpClientModule { return(GetMockedLevelUpModule <T, Request>(expectedResponse, null, expectedAccessToken: expectedAccessToken, expectedRequestUrl: expectedRequestUrl, environmentToUse: environmentToUse)); }
private static T GenerateMockLevelUpModuleWhereEveryHttpVerbDoesTheSameThing <T>( Func <string, string, string, string, IRestResponse> expectedResponseForAnyCall, LevelUpEnvironment targetEnvironment) where T : ILevelUpClientModule { var service = GenerateMockIRestfulService( expectedResponseForAnyCall, expectedResponseForAnyCall, (a, b, c) => expectedResponseForAnyCall(a, null, b, c), // GET and DELETE return the same specified RestResponse (a, b, c) => expectedResponseForAnyCall(a, null, b, c)); // object, they just don't provide a body. return(LevelUpClientFactory.Create <T>(new AgentIdentifier("SDK Unit Tests", "", "", ""), targetEnvironment, service.Object)); }
/// <summary> /// Creates an IRequestVisitor<IResponse> (i.e. the "request execution engine") using the default /// implementation for each of the requisite visitors. Unless a 3rd-party client wants to override /// a particular visitor (in order to modify the way that the SDK creates and interperates calls /// to the LevelUp api) for some reason, these defaults are what should be used in the creation of /// the LevelUpClient implementation class. /// </summary> private static IRequestVisitor <IResponse> GetDefaultLevelUpRequestEngine(AgentIdentifier identifier, LevelUpEnvironment enviornment, IRestfulService httpRestService) { IRequestVisitor <string> endpointCreator = new RequestEndpointCreator(enviornment); IRequestVisitor <string> headerAuthorizationTokenCreator = new RequestHeaderTokenCreator(); IRequestVisitor <Dictionary <HttpStatusCode, LevelUpRestWrapper.ResponseAction> > customHttpStatusCodeHandlers = new AdditionalHttpStatusCodeHandlers(); IRequestVisitor <IResponse> requestExecutionEngine = new RequestExecutionEngine(httpRestService, identifier, endpointCreator, headerAuthorizationTokenCreator, customHttpStatusCodeHandlers); return(requestExecutionEngine); }
/// <summary> /// Gets a LevelUpClient Module interface that will mimic, for any rest call, the provided /// expected RestResponse. Callers are expected to populate this expected response, including /// the json content string, based on what they expect to come over the wire according to the /// LevelUp platform API docs. /// </summary> /// <typeparam name="T">The specific type of ILevelUpClientModule to return.</typeparam> /// <typeparam name="U">The expected request model object type.</typeparam> /// <param name="expectedResponse">The desired RestResponse that the caller would want platform /// to produce. Note that this response will be applied to every rest command. </param> /// <param name="expectedRequestBody">If the caller wishes to verify the body data that is /// used in the request, they can do that here.</param> /// <param name="expectedAccessToken">If the caller wishes to verify authorization functionality /// for a specific command, they can provide an expected access token, without which the returned /// rest response will contain an unauthorized status code.</param> /// <param name="expectedRequestUrl">If the caller wishes to verify the endpoint that is /// used in the request, they can do that here.</param> /// <param name="environmentToUse">Environment to use to build URI of the request. Default value is Sandbox</param> internal static T GetMockedLevelUpModule <T, U>(RestResponse expectedResponse, string expectedRequestBody, string expectedAccessToken, string expectedRequestUrl, LevelUpEnvironment environmentToUse) where T : ILevelUpClientModule where U : Request { // This is not a very smart mock, in that every type of request will generate the same // response -- GET/POST/PUT/DELETE all just call this delegate. var returnsExpectedResponse = new Func <string, string, string, string, IRestResponse>( (string url, string body, string accessToken, string userAgent) => { if (!string.IsNullOrEmpty(expectedRequestUrl) && expectedRequestUrl != url) { Assert.Fail("The specified expected url [{0}] does not match " + "the url generated in the code [{1}].", expectedRequestUrl, url); } if (!string.IsNullOrEmpty(expectedRequestBody)) { Type Q = typeof(U).GetProperty("Body")?.PropertyType ?? typeof(U); TestUtilities.VerifyJsonIsEquivalent(expectedRequestBody, body, Q); } if (expectedAccessToken != null && !string.IsNullOrEmpty(accessToken) && !accessToken.Contains(expectedAccessToken)) { Assert.Fail("The specified expected access token [{0}] does not match " + "the access token generated in code [{1}].", expectedAccessToken, accessToken); } return(expectedResponse); }); return(GenerateMockLevelUpModuleWhereEveryHttpVerbDoesTheSameThing <T>(returnsExpectedResponse, environmentToUse)); }
/// <summary> /// Gets a LevelUpClient Module interface that will mimic, for any rest call, the provided expected RestResponses /// using a paging format. This mock will handle adding paging link headers to the returned RestResponse /// objects and serving up the subsequent RestResponse when those links are used. /// </summary> /// <typeparam name="T">A LevelUpModule type that uses paging.</typeparam> /// <param name="expectedResponses">A collection of expected response objects, which will each be treated /// as a page, in the correct paging order.</param> /// <param name="requestUrlBase">The url that is expected to retrieve the first page.</param> /// <param name="environmentToUse">Environment to use to build URI of the request.</param> internal static T GetMockedLevelUpModuleWithPaging <T>(RestResponse[] expectedResponses, string requestUrlBase, LevelUpEnvironment environmentToUse) where T : IQueryOrders { var len = expectedResponses.Length; if (len < 1) { throw new ArgumentException("This mock requires that the caller specify at least one expected response."); } var GetFullUrlForPage = new Func <int, string>(x => string.Format("{0}?page={1}", requestUrlBase, x)); // Dictionary is indexed by the incoming url and contains a tuple that houses a response for that particular // incoming url and the url of the next page. var responses = new Dictionary <string, Tuple <RestResponse, string> >(); // If the request does not specify a page number, we will return the first page of results, with any applicable link to page 2 string urlForPageTwo = len > 1 ? GetFullUrlForPage(2) : null; responses.Add(requestUrlBase, new Tuple <RestResponse, string>(expectedResponses[0], urlForPageTwo)); for (int i = 0; i < len; i++) { string pageUrl = GetFullUrlForPage(i + 1); string nextPageUrl = (i + 2 <= len) ? GetFullUrlForPage(i + 2) : null; responses.Add(pageUrl, new Tuple <RestResponse, string>(expectedResponses[i], nextPageUrl)); } var returnsExpectedResponse = new Func <string, string, string, string, IRestResponse>( (string url, string body, string accessToken, string userAgent) => { if (!responses.ContainsKey(url)) { if (!url.ToLower().Contains(requestUrlBase.ToLower())) { Assert.Fail("The specified url [" + url + "] is unknown to the mocked LevelUpClient object."); } // If the specified page does not exist in the mock's record, but the base // url is valid, we'll return a 204 as per the platform api docs. return(new RestResponse { StatusCode = HttpStatusCode.NoContent }); } Tuple <RestResponse, string> responseData = responses[url]; RestResponse retval = responseData.Item1; if (responseData.Item2 != null) { var header = new Parameter { Type = ParameterType.HttpHeader, Name = "Link", Value = string.Format("<{0}>; rel=\"next\"", responseData.Item2) }; retval.Headers.Add(header); } return(retval); }); return(GenerateMockLevelUpModuleWhereEveryHttpVerbDoesTheSameThing <T>(returnsExpectedResponse, environmentToUse)); }
internal LevelUpClient(IRestfulService restService, AgentIdentifier identifier, LevelUpEnvironment targetEnviornment) { _restWrapper = new LevelUpRestWrapper(restService, identifier); _targetEnviornment = targetEnviornment; }
public RequestEndpointCreator(LevelUpEnvironment targetEnvironment) { TargetEnviornment = targetEnvironment; }