public OCPIResponseLogEvent Add(OCPIResponseLogHandler callback) { if (callback == null) { throw new NullReferenceException("callback is null"); } lock (subscribers) { subscribers.Add(callback); } return(this); }
/// <summary> /// Add a method callback for the given URL template. /// </summary> /// <param name="CommonAPI">The OCPI Common API.</param> /// <param name="Hostname">The HTTP hostname.</param> /// <param name="HTTPMethod">The HTTP method.</param> /// <param name="URLTemplate">The URL template.</param> /// <param name="HTTPContentType">The HTTP content type.</param> /// <param name="URLAuthentication">Whether this method needs explicit uri authentication or not.</param> /// <param name="HTTPMethodAuthentication">Whether this method needs explicit HTTP method authentication or not.</param> /// <param name="ContentTypeAuthentication">Whether this method needs explicit HTTP content type authentication or not.</param> /// <param name="OCPIRequestLogger">A OCPI request logger.</param> /// <param name="OCPIResponseLogger">A OCPI response logger.</param> /// <param name="DefaultErrorHandler">The default error handler.</param> /// <param name="OCPIRequestHandler">The method to call.</param> public static void AddOCPIMethod(this CommonAPI CommonAPI, HTTPHostname Hostname, HTTPMethod HTTPMethod, HTTPPath URLTemplate, HTTPContentType HTTPContentType = null, HTTPAuthentication URLAuthentication = null, HTTPAuthentication HTTPMethodAuthentication = null, HTTPAuthentication ContentTypeAuthentication = null, OCPIRequestLogHandler OCPIRequestLogger = null, OCPIResponseLogHandler OCPIResponseLogger = null, HTTPDelegate DefaultErrorHandler = null, OCPIRequestDelegate OCPIRequestHandler = null, URLReplacement AllowReplacement = URLReplacement.Fail) { CommonAPI.HTTPServer. AddMethodCallback(Hostname, HTTPMethod, URLTemplate, HTTPContentType, URLAuthentication, HTTPMethodAuthentication, ContentTypeAuthentication, (timestamp, httpAPI, httpRequest) => OCPIRequestLogger?.Invoke(timestamp, null, HTTP.OCPIRequest.Parse(httpRequest, CommonAPI)), (timestamp, httpAPI, httpRequest, httpResponse) => OCPIResponseLogger?.Invoke(timestamp, null, httpRequest.SubprotocolRequest as OCPIRequest, (httpResponse.SubprotocolResponse as OCPIResponse) ?? new OCPIResponse(httpRequest.SubprotocolRequest as OCPIRequest, 2000, "OCPIResponse is null!", httpResponse.HTTPBodyAsUTF8String, httpResponse.Timestamp, httpResponse)), DefaultErrorHandler, async httpRequest => { try { // When no OCPIRequestLogger was used! if (httpRequest.SubprotocolRequest is null) { httpRequest.SubprotocolRequest = OCPIRequest.Parse(httpRequest, CommonAPI); } var OCPIResponseBuilder = await OCPIRequestHandler(httpRequest.SubprotocolRequest as OCPIRequest); var httpResponseBuilder = OCPIResponseBuilder.ToHTTPResponseBuilder(); httpResponseBuilder.SubprotocolResponse = new OCPIResponse(OCPIResponseBuilder.Request, OCPIResponseBuilder.StatusCode ?? 3000, OCPIResponseBuilder.StatusMessage, OCPIResponseBuilder.AdditionalInformation, OCPIResponseBuilder.Timestamp ?? DateTime.UtcNow, httpResponseBuilder.AsImmutable); return(httpResponseBuilder); } catch (Exception e) { return(new HTTPResponse.Builder(HTTPStatusCode.InternalServerError) { ContentType = HTTPContentType.JSON_UTF8, Content = new OCPIResponse <JObject>( JSONObject.Create( new JProperty("description", e.Message), new JProperty("stacktrace", e.StackTrace.Split(new String[] { Environment.NewLine }, StringSplitOptions.None).ToArray()), new JProperty("source", e.TargetSite.Module.Name), new JProperty("type", e.TargetSite.ReflectedType.Name) ), 2000, e.Message, null, DateTime.UtcNow, null, (httpRequest.SubprotocolRequest as OCPIRequest)?.RequestId, (httpRequest.SubprotocolRequest as OCPIRequest)?.CorrelationId ).ToJSON(json => json).ToUTF8Bytes(), Connection = "close" }); } }, AllowReplacement); }