Re-pathing request DTOs

I have a pre-defined (as in, I cannot modify) request DTO with a Route already assigned to it, that looks soemthing like this:

    [Route("/import", "POST")]
    public class ImportEventsRequest : List<MixpanelEvent>, IReturn<ImportEventsResponse>, IPost
    {
        [DataMember(Name = "strict")]
        public int Strict { get; set; }

        [DataMember(Name = "project_id")]
        public string ProjectId { get; set; }
    }

(just so happens it is one that I use to make outbound calls to api.mixpanel.com from the back-end of my API, using a JsonServiceClient).

Now, I want to re-use that request DTO in my own Stub API (used in testing), for which I have a AppHostBase running in a console app.

In that testing stub console app, this is used instead of the internet, during testing) I have my own dedicated service, as we would expect, something like this:

    public class StubMixpanel : StubServiceBase
    {
        public ImportEventsResponse Post(ImportEventsRequest request)
        {
             ...stuff for logging for testing purposes....

            Response.StatusCode = (int)HttpStatusCode.OK;
            return new ImportEventsResponse
            {
                Code = 200,
                NumRecordsImported = 1,
                Status = "OK"
            };
        }
    }

None of this is a challenge with ServiceStack.

When I stand up my stub testing console app, (on say https://localhost:5656 I now accept requests directly on the path / (AppHost.HandlerFactoryPath="").
So, for the request above I would taking inbound requests to my testing stub on: https://localhost:5656/import.

However, it is not what I want, since Mixpanel is just one of many integrations we have stubs for, and we occasionally have path collisions with other 3rd party services. I need to segregate the API’s of each 3rd party integration.

What I want to do is move my /import path down to https://localhost:5656/mixpanel/import and have all mixpanel API requests hang off this path /mixpanel. Which won’t collide with other integrations.

What is a good way to do this seggregation in ServiceStack? in configuring the ServiceHost (or Filters or whatever?), bearing in mind I have to re-path a ton of other APIs, without changing their declared [Route] statements?

Ideally, it would be something I do per Service class, since that represents the segregation.

Rather than hosting all your services in one host, you could host them separately by assembly. This will avoid collisions, as far as calling each service, you would have a few options.

  • Your clients could retain mapping of host/request DTO to test; OR
  • A single host could use a custom IServiceGatewayFactory forwarding requests to the other hosts, holding the assembly to host mapping in the single server.

Your custom gateway could map incoming requests to known prefixes like /import if that is how you want to separate them. This assumes you are requesting your Test App with the DTO directly, rather than the Route path

Alternatively you could use a custom ServiceStackHost overriding GetRouteAttributes to handle the separation logic for your Test app. Separately by assembly and prefix will likely be the least amount of custom handling since collisions shouldn’t occur within the same assembly for Request DTOs.

Or you could use the Fluent API to do your own reflection to discover and add Routes.

Hope that helps.