Imported Upstream version 1.22.0
[platform/upstream/grpc.git] / src / objective-c / examples / InterceptorSample / InterceptorSample / CacheInterceptor.h
1 /*
2  *
3  * Copyright 2019 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18
19 #import <GRPCClient/GRPCInterceptor.h>
20
21 NS_ASSUME_NONNULL_BEGIN
22
23 @interface RequestCacheEntry : NSObject<NSCopying>
24
25 @property(readonly, copy, nullable) NSString *path;
26 @property(readonly, copy, nullable) id message;
27
28 @end
29
30 @interface MutableRequestCacheEntry : RequestCacheEntry
31
32 @property(copy, nullable) NSString *path;
33 @property(copy, nullable) id<NSObject> message;
34
35 @end
36
37 @interface ResponseCacheEntry : NSObject<NSCopying>
38
39 @property(readonly, copy, nullable) NSDate *deadline;
40
41 @property(readonly, copy, nullable) NSDictionary *headers;
42 @property(readonly, copy, nullable) id message;
43 @property(readonly, copy, nullable) NSDictionary *trailers;
44
45 @end
46
47 @interface MutableResponseCacheEntry : ResponseCacheEntry
48
49 @property(copy, nullable) NSDate *deadline;
50
51 @property(copy, nullable) NSDictionary *headers;
52 @property(copy, nullable) id message;
53 @property(copy, nullable) NSDictionary *trailers;
54
55 @end
56
57 @interface CacheContext : NSObject<GRPCInterceptorFactory>
58
59 - (nullable instancetype)init;
60
61 - (nullable ResponseCacheEntry *)getCachedResponseForRequest:(RequestCacheEntry *)request;
62
63 - (void)setCachedResponse:(ResponseCacheEntry *)response forRequest:(RequestCacheEntry *)request;
64
65 @end
66
67 @interface CacheInterceptor : GRPCInterceptor
68
69 - (instancetype)init NS_UNAVAILABLE;
70
71 + (instancetype) new NS_UNAVAILABLE;
72
73 - (nullable instancetype)initWithInterceptorManager:
74                              (GRPCInterceptorManager *_Nonnull)intercepterManager
75                                        cacheContext:(CacheContext *_Nonnull)cacheContext
76     NS_DESIGNATED_INITIALIZER;
77
78 // implementation of GRPCInterceptorInterface
79 - (void)startWithRequestOptions:(GRPCRequestOptions *)requestOptions
80                     callOptions:(GRPCCallOptions *)callOptions;
81 - (void)writeData:(id)data;
82 - (void)finish;
83
84 // implementation of GRPCResponseHandler
85 - (void)didReceiveInitialMetadata:(nullable NSDictionary *)initialMetadata;
86 - (void)didReceiveData:(id)data;
87 - (void)didCloseWithTrailingMetadata:(nullable NSDictionary *)trailingMetadata
88                                error:(nullable NSError *)error;
89
90 @end
91
92 NS_ASSUME_NONNULL_END