Skip to content

Instantly share code, notes, and snippets.

@bnickel
Created August 26, 2016 17:57
Show Gist options
  • Save bnickel/70a31e604f2cf32090ef4e98c7a95930 to your computer and use it in GitHub Desktop.
Save bnickel/70a31e604f2cf32090ef4e98c7a95930 to your computer and use it in GitHub Desktop.
@import StacMan;
@import JRSwizzle;
NS_INLINE NSURL *TransformURLForForTier(NSURL *URL, SEAPIContext *context) {
if (context.mobileRoot || context.apiRoot) {
NSMutableString *computedUrl = [URL.absoluteString mutableCopy];
if (context.mobileRoot) {
[computedUrl replaceOccurrencesOfString:@"https://mobile.stackexchange.com" withString:context.mobileRoot options:NSCaseInsensitiveSearch range:NSMakeRange(0, [computedUrl length])];
}
if (context.apiRoot) {
[computedUrl replaceOccurrencesOfString:@"https://api.stackexchange.com" withString:context.apiRoot options:NSCaseInsensitiveSearch range:NSMakeRange(0, [computedUrl length])];
}
return [NSURL URLWithString:computedUrl];
}
return URL;
}
@implementation SEAPIRequest (Tiers)
+ (void)load
{
[self jr_swizzleMethod:@selector(URLRequestWithContext:) withMethod:@selector(SE_URLRequestWithContext:) error:NULL];
}
- (NSURLRequest *)SE_URLRequestWithContext:(SEAPIContext *)context
{
SEAPIMutableRequest *mutableRequest = [self mutableCopy];
if (context.tier != SETierProd) {
mutableRequest.baseURL = TransformURLForForTier(mutableRequest.baseURL ?: [NSURL URLWithString:@"https://api.stackexchange.com"], context);
}
return [mutableRequest SE_URLRequestWithContext:context];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment