Debian Codegraph @

Release/samples/CasaLens/casalens.h

raw
1 /***
2 * Copyright (C) Microsoft. All rights reserved.
3 * Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
4 *
5 * =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
6 *
7 * casalens.h: Listener code: Given a location/postal code, the listener queries different services
8 * for weather, things to do: events, movie and pictures and returns it to the client.
9 *
10 * =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
11 ****/
12 #pragma once
13
14 #include "stdafx.h"
15
16 #include "cpprest/containerstream.h"
17 #include "cpprest/filestream.h"
18 #include "cpprest/http_client.h"
19 #include "cpprest/http_listener.h"
20 #include "cpprest/json.h"
21 #include "cpprest/producerconsumerstream.h"
22 #include "cpprest\http_listener.h"
23 #pragma warning(push)
24 #pragma warning(disable : 4457)
25 #include <agents.h>
26 #pragma warning(pop)
27 #include <ctime>
28 #include <locale>
29
30 //
31 // Credentials class to manage the service URLs, key name and api-keys.
32 //
33 class casalens_creds
34 {
35 public:
36 static const utility::string_t events_url;
37 static const utility::string_t movies_url;
38 static const utility::string_t images_url;
39 static const utility::string_t weather_url;
40 static const utility::string_t bmaps_url;
41 static const utility::string_t gmaps_url;
42
43 static const utility::string_t events_keyname;
44 static const utility::string_t events_key;
45 static const utility::string_t movies_keyname;
46 static const utility::string_t movies_key;
47 static const utility::string_t images_keyname;
48 static const utility::string_t images_key;
49 static const utility::string_t bmaps_keyname;
50 static const utility::string_t bmaps_key;
51 };
52
53 //
54 // This class implements the CasaLens server functionality.
55 // It maintains a http_listener, registers to listen for requests.
56 // Also implements the handlers to respond to requests and methods to fetch
57 // and aggregate data from different services.
58 //
59 class CasaLens
60 {
61 public:
62 CasaLens() {}
63 CasaLens(utility::string_t url);
64
65 pplx::task<void> open();
66 pplx::task<void> close();
67
68 void handle_get(web::http::http_request message);
69 void handle_post(web::http::http_request message);
70
71 private:
72 // Error handlers
73 static void handle_error(pplx::task<void>& t);
74 pplx::task<web::json::value> handle_exception(pplx::task<web::json::value>& t, const utility::string_t& field_name);
75
76 // methods to fetch data from the services
77 pplx::task<web::json::value> get_events(const utility::string_t& post_code);
78 pplx::task<web::json::value> get_weather(const utility::string_t& post_code);
79 pplx::task<web::json::value> get_pictures(const utility::string_t& location, const utility::string_t& count);
80 pplx::task<web::json::value> get_movies(const utility::string_t& post_code);
81
82 // Utility functions
83 bool is_number(const std::string& s);
84 std::wstring get_date();
85
86 void fetch_data(web::http::http_request message, const std::wstring& postal_code, const std::wstring& location);
87 void get_data(web::http::http_request message, const std::wstring& location);
88
89 static const int num_events = 5;
90 static const int num_images = 5;
91 static const int num_movies = 5;
92
93 static const utility::string_t events_json_key;
94 static const utility::string_t movies_json_key;
95 static const utility::string_t weather_json_key;
96 static const utility::string_t images_json_key;
97 static const utility::string_t error_json_key;
98
99 // Lock to the in memory cache (m_data)
100 concurrency::reader_writer_lock m_rwlock;
101
102 // key: City name or postal code
103 // Value: JSON response data to be sent to clients
104 // We are caching the data for multiple requests.
105 std::map<utility::string_t, web::json::value> m_data;
106
107 // m_htmlcontentmap contains data about the html contents of the website, their mime types
108 // key: relative URI path of the HTML content being requested
109 // value: Tuple where:
110 // Element1: relative path on the disk of the file being requested
111 // Element2: Mime type/content type of the file
112 std::map<utility::string_t, std::tuple<utility::string_t, utility::string_t>> m_htmlcontentmap;
113
114 // HTTP listener
115 web::http::experimental::listener::http_listener m_listener;
116 };