Proyectos de Subversion Iphone Microlearning

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
// Copyright 2020 Google LLC
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//      http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
 
15
#import "FirebasePerformance/Sources/Public/FirebasePerformance/FIRHTTPMetric.h"
16
#import "FirebasePerformance/Sources/Instrumentation/FIRHTTPMetric+Private.h"
17
 
18
#import "FirebasePerformance/Sources/Common/FPRConstants.h"
19
#import "FirebasePerformance/Sources/Configurations/FPRConfigurations.h"
20
#import "FirebasePerformance/Sources/FPRConsoleLogger.h"
21
#import "FirebasePerformance/Sources/FPRDataUtils.h"
22
#import "FirebasePerformance/Sources/Instrumentation/FPRNetworkTrace.h"
23
 
24
@interface FIRHTTPMetric ()
25
 
26
/* A placeholder URLRequest used for SDK metric tracking. */
27
@property(nonatomic, strong) NSURLRequest *URLRequest;
28
 
29
@end
30
 
31
@implementation FIRHTTPMetric
32
 
33
- (nullable instancetype)initWithURL:(nonnull NSURL *)URL HTTPMethod:(FIRHTTPMethod)httpMethod {
34
  BOOL tracingEnabled = [FPRConfigurations sharedInstance].isDataCollectionEnabled;
35
  if (!tracingEnabled) {
36
    FPRLogInfo(kFPRTraceDisabled, @"Trace feature is disabled. Dropping http metric - %@",
37
               URL.absoluteString);
38
    return nil;
39
  }
40
 
41
  BOOL sdkEnabled = [[FPRConfigurations sharedInstance] sdkEnabled];
42
  if (!sdkEnabled) {
43
    FPRLogInfo(kFPRTraceDisabled, @"Dropping event since Performance SDK is disabled.");
44
    return nil;
45
  }
46
 
47
  NSMutableURLRequest *URLRequest = [[NSMutableURLRequest alloc] initWithURL:URL];
48
  NSString *HTTPMethodString = nil;
49
  switch (httpMethod) {
50
    case FIRHTTPMethodGET:
51
      HTTPMethodString = @"GET";
52
      break;
53
 
54
    case FIRHTTPMethodPUT:
55
      HTTPMethodString = @"PUT";
56
      break;
57
 
58
    case FIRHTTPMethodPOST:
59
      HTTPMethodString = @"POST";
60
      break;
61
 
62
    case FIRHTTPMethodHEAD:
63
      HTTPMethodString = @"HEAD";
64
      break;
65
 
66
    case FIRHTTPMethodDELETE:
67
      HTTPMethodString = @"DELETE";
68
      break;
69
 
70
    case FIRHTTPMethodPATCH:
71
      HTTPMethodString = @"PATCH";
72
      break;
73
 
74
    case FIRHTTPMethodOPTIONS:
75
      HTTPMethodString = @"OPTIONS";
76
      break;
77
 
78
    case FIRHTTPMethodTRACE:
79
      HTTPMethodString = @"TRACE";
80
      break;
81
 
82
    case FIRHTTPMethodCONNECT:
83
      HTTPMethodString = @"CONNECT";
84
      break;
85
  }
86
  [URLRequest setHTTPMethod:HTTPMethodString];
87
 
88
  if (URLRequest && HTTPMethodString != nil) {
89
    self = [super init];
90
    _networkTrace = [[FPRNetworkTrace alloc] initWithURLRequest:URLRequest];
91
    _URLRequest = [URLRequest copy];
92
    return self;
93
  }
94
 
95
  FPRLogError(kFPRInstrumentationInvalidInputs, @"Invalid URL");
96
  return nil;
97
}
98
 
99
- (void)start {
100
  [self.networkTrace start];
101
}
102
 
103
- (void)markRequestComplete {
104
  [self.networkTrace checkpointState:FPRNetworkTraceCheckpointStateRequestCompleted];
105
}
106
 
107
- (void)markResponseStart {
108
  [self.networkTrace checkpointState:FPRNetworkTraceCheckpointStateResponseReceived];
109
}
110
 
111
- (void)stop {
112
  self.networkTrace.requestSize = self.requestPayloadSize;
113
  self.networkTrace.responseSize = self.responsePayloadSize;
114
  // Create a dummy URL Response that will be used for data extraction.
115
  NSString *responsePayloadSize =
116
      [[NSString alloc] initWithFormat:@"%ld", self.responsePayloadSize];
117
  NSMutableDictionary<NSString *, NSString *> *headerFields =
118
      [[NSMutableDictionary<NSString *, NSString *> alloc] init];
119
  if (self.responseContentType) {
120
    [headerFields setObject:self.responseContentType forKey:@"Content-Type"];
121
  }
122
  [headerFields setObject:responsePayloadSize forKey:@"Content-Length"];
123
 
124
  if (self.responseCode == 0) {
125
    FPRLogError(kFPRInstrumentationInvalidInputs, @"Response code not set for request - %@",
126
                self.URLRequest.URL);
127
    return;
128
  }
129
  NSHTTPURLResponse *URLResponse = [[NSHTTPURLResponse alloc] initWithURL:self.URLRequest.URL
130
                                                               statusCode:self.responseCode
131
                                                              HTTPVersion:nil
132
                                                             headerFields:headerFields];
133
 
134
  [self.networkTrace didCompleteRequestWithResponse:URLResponse error:nil];
135
}
136
 
137
#pragma mark - Custom attributes related methods
138
 
139
- (NSDictionary<NSString *, NSString *> *)attributes {
140
  return [self.networkTrace attributes];
141
}
142
 
143
- (void)setValue:(NSString *)value forAttribute:(nonnull NSString *)attribute {
144
  [self.networkTrace setValue:value forAttribute:attribute];
145
}
146
 
147
- (NSString *)valueForAttribute:(NSString *)attribute {
148
  return [self.networkTrace valueForAttribute:attribute];
149
}
150
 
151
- (void)removeAttribute:(NSString *)attribute {
152
  [self.networkTrace removeAttribute:attribute];
153
}
154
 
155
@end