-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add class HttpFetcher fetches data from URL endpoint. Add the FakeHttpFetcher. Use HttpFetcher on GIDSignIn for revoking the grant scope.
- Loading branch information
1 parent
b0bc583
commit 3a396b6
Showing
9 changed files
with
403 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright 2023 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
@protocol GTMSessionFetcherServiceProtocol; | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
@protocol GIDHTTPFetcher <NSObject> | ||
|
||
/// Fetches the data from an URL request. | ||
/// | ||
/// @param urlRequest The url request to fetch data. | ||
/// @param authorizer The object to add authorization to the request. | ||
/// @param comment The comment for logging purpose. | ||
/// @param completion The block that is called on completion asynchronously. | ||
- (void)fetchURLRequest:(NSURLRequest *)urlRequest | ||
#pragma clang diagnostic push | ||
#pragma clang diagnostic ignored "-Wdeprecated-declarations" | ||
withFetcherService:(id<GTMSessionFetcherServiceProtocol>)fetcherService | ||
#pragma clang diagnostic pop | ||
withComment:(NSString *)comment | ||
completion:(void (^)(NSData *_Nullable, NSError *_Nullable))completion; | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
46 changes: 46 additions & 0 deletions
46
GoogleSignIn/Sources/GIDHTTPFetcher/Implementations/Fakes/GIDFakeHTTPFetcher.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright 2023 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
#import "GoogleSignIn/Sources/GIDHTTPFetcher/API/GIDHTTPFetcher.h" | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
/// The block which provides the response for the method | ||
/// fetchURLRequest:withAuthorizer:withComment:completion:`. | ||
/// | ||
/// @param data The NSData returned if succeed, | ||
/// @param error The error returned if failed. | ||
typedef void(^GIDHTTPFetcherFakeResponseProviderBlock)(NSData *_Nullable data, | ||
NSError *_Nullable error); | ||
|
||
/// The block to set up data based on the input request for the method | ||
/// fetchURLRequest:withAuthorizer:withComment:completion:`. | ||
/// | ||
/// @param request The request from input. | ||
/// @param responseProvider The block which provides the response. | ||
typedef void (^GIDHTTPFetcherTestBlock)(NSURLRequest *request, | ||
GIDHTTPFetcherFakeResponseProviderBlock responseProvider); | ||
|
||
@interface GIDFakeHTTPFetcher : NSObject <GIDHTTPFetcher> | ||
|
||
/// Set the test block which provides the response value. | ||
- (void)setTestBlock:(GIDHTTPFetcherTestBlock)block; | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
23 changes: 23 additions & 0 deletions
23
GoogleSignIn/Sources/GIDHTTPFetcher/Implementations/Fakes/GIDFakeHTTPFetcher.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#import "GoogleSignIn/Sources/GIDHTTPFetcher/Implementations/Fakes/GIDFakeHTTPFetcher.h" | ||
|
||
@interface GIDFakeHTTPFetcher () | ||
|
||
@property(nonatomic) GIDHTTPFetcherTestBlock testBlock; | ||
|
||
@end | ||
|
||
@implementation GIDFakeHTTPFetcher | ||
|
||
- (void)fetchURLRequest:(NSURLRequest *)urlRequest | ||
#pragma clang diagnostic ignored "-Wdeprecated-declarations" | ||
withFetcherService:(id<GTMSessionFetcherServiceProtocol>)fetcherService | ||
#pragma clang diagnostic pop | ||
withComment:(NSString *)comment | ||
completion:(void (^)(NSData *_Nullable, NSError *_Nullable))completion { | ||
NSAssert(self.testBlock != nil, @"Set the test block before invoking this method."); | ||
self.testBlock(urlRequest, ^(NSData *_Nullable data, NSError *_Nullable error) { | ||
completion(data, error); | ||
}); | ||
} | ||
|
||
@end |
26 changes: 26 additions & 0 deletions
26
GoogleSignIn/Sources/GIDHTTPFetcher/Implementations/GIDHTTPFetcher.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright 2023 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
#import "GoogleSignIn/Sources/GIDHTTPFetcher/API/GIDHTTPFetcher.h" | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
@interface GIDHTTPFetcher : NSObject<GIDHTTPFetcher> | ||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
37 changes: 37 additions & 0 deletions
37
GoogleSignIn/Sources/GIDHTTPFetcher/Implementations/GIDHTTPFetcher.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#import "GoogleSignIn/Sources/GIDHTTPFetcher/Implementations/GIDHTTPFetcher.h" | ||
|
||
//#ifdef SWIFT_PACKAGE | ||
@import GTMAppAuth; | ||
//#else | ||
//#import <GTMAppAuth/GTMAppAuth.h> | ||
//#endif | ||
#import <GTMSessionFetcher/GTMSessionFetcher.h> | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
// Maximum retry interval in seconds for the fetcher. | ||
static const NSTimeInterval kFetcherMaxRetryInterval = 15.0; | ||
|
||
@implementation GIDHTTPFetcher | ||
|
||
- (void)fetchURLRequest:(NSURLRequest *)urlRequest | ||
#pragma clang diagnostic ignored "-Wdeprecated-declarations" | ||
withFetcherService:(id<GTMSessionFetcherServiceProtocol>)fetcherService | ||
#pragma clang diagnostic pop | ||
withComment:(NSString *)comment | ||
completion:(void (^)(NSData *_Nullable, NSError *_Nullable))completion { | ||
GTMSessionFetcher *fetcher; | ||
if (fetcherService) { | ||
fetcher = [fetcherService fetcherWithRequest:urlRequest]; | ||
} else { | ||
fetcher = [GTMSessionFetcher fetcherWithRequest:urlRequest]; | ||
} | ||
fetcher.retryEnabled = YES; | ||
fetcher.maxRetryInterval = kFetcherMaxRetryInterval; | ||
fetcher.comment = comment; | ||
[fetcher beginFetchWithCompletionHandler:completion]; | ||
} | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.