Skip to content

Commit 5979115

Browse files
committed
Added a simple example that uses the Twitter Search API.
1 parent e118310 commit 5979115

File tree

7 files changed

+83
-18
lines changed

7 files changed

+83
-18
lines changed

boost/network/uri/decode.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@ OutputIterator decode(const SinglePassRange &range,
9393
const OutputIterator &out) {
9494
return decode(boost::begin(range), boost::end(range), out);
9595
}
96+
97+
inline
98+
std::string decoded(const std::string &input) {
99+
std::string decoded;
100+
decode(input, std::back_inserter(decoded));
101+
return decoded;
102+
}
96103
} // namespace uri
97104
} // namespace network
98105
} // namespace boost

boost/network/uri/encode.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,13 @@ OutputIterator encode(const SinglePassRange &range,
161161
const OutputIterator &out) {
162162
return encode(boost::begin(range), boost::end(range), out);
163163
}
164+
165+
inline
166+
std::string encoded(const std::string &input) {
167+
std::string encoded;
168+
encode(input, std::back_inserter(encoded));
169+
return encoded;
170+
}
164171
} // namespace uri
165172
} // namespace network
166173
} // namespace boost

libs/network/doc/uri.rst

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
The URI template
2-
================
1+
The URI class
2+
=============
33

44
In addition to protocol implementations, the :mod:`cpp-netlib`
5-
provides a powerful URI template. The URI template implements a
6-
parser based on `RFC 3986`_.
5+
provides a powerful URI class. The class implements a parser based
6+
on `RFC 3986`_ and `RFC 2732`_.
77

88
Generic URI Syntax Overview
99
```````````````````````````
@@ -60,31 +60,24 @@ elements. Another example, using HTTP is given below:
6060
The difference here between the path in a hierarchical URI and that in
6161
the example above for the non-hierarchical URI.
6262

63-
``basic_uri``
64-
`````````````
63+
The ``uri`` class
64+
`````````````````
6565

66-
The ``basic_uri`` definition is consistent with that of
67-
``basic_message``::
68-
69-
template <class Tag>
70-
class basic_uri;
71-
72-
As it stands, the template only supplies a URI parser and no builder.
66+
As of version 0.9.3, ``uri`` supplies a URI parser and builder.
7367
To use the parser, it's as simple as supplying a string to the
7468
constructor::
7569

76-
boost::network::uri::uri instance("http://cpp-netlib.github.com/");
70+
using namespace boost::network;
71+
uri::uri instance("http://cpp-netlib.github.com/");
7772
assert(boost::is_valid(instance));
78-
std::cout << "scheme: " << boost::network::uri::scheme(instance) << std::endl
79-
<< "host: " << boost::network::uri::host(instance) << std::endl;
73+
std::cout << "scheme: " << uri::scheme(instance) << std::endl
74+
<< "host: " << uri::host(instance) << std::endl;
8075

8176
The command-line output of this program will be::
8277

8378
scheme: http
8479
host: cpp-netlib.github.com
8580

86-
A future version of ``basic_uri`` will provide a full builder API.
87-
8881
``URI Concept``
8982
```````````````
9083

@@ -127,3 +120,4 @@ A future version of ``basic_uri`` will provide a full builder API.
127120
.. _`RFC 3986`: http://tools.ietf.org/html/rfc3986
128121
.. _`RFC 2368`: http://tools.ietf.org/html/rfc2368
129122
.. _`RFC 3513`: http://tools.ietf.org/html/rfc3513
123+
.. _`RFC 2732`: http://tools.ietf.org/html/rfc2732

libs/network/example/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ add_executable(http_client http_client.cpp)
1212
add_executable(simple_wget simple_wget.cpp)
1313
add_executable(atom_reader atom/atom.cpp atom/main.cpp)
1414
add_executable(rss_reader rss/rss.cpp rss/main.cpp)
15+
add_executable(twitter_search twitter/search.cpp)
1516
add_executable(hello_world_server http/hello_world_server.cpp)
1617
if (UNIX)
1718
add_executable(fileserver http/fileserver.cpp)
@@ -21,6 +22,7 @@ add_dependencies(http_client cppnetlib-uri cppnetlib-client-connections)
2122
add_dependencies(simple_wget cppnetlib-uri cppnetlib-client-connections)
2223
add_dependencies(atom_reader cppnetlib-uri cppnetlib-client-connections)
2324
add_dependencies(rss_reader cppnetlib-uri cppnetlib-client-connections)
25+
add_dependencies(twitter_search cppnetlib-uri cppnetlib-client-connections)
2426
add_dependencies(uri cppnetlib-uri)
2527
set(BOOST_CLIENT_LIBS
2628
${Boost_PROGRAM_OPTIONS_LIBRARY}
@@ -59,6 +61,12 @@ target_link_libraries(rss_reader
5961
cppnetlib-uri
6062
cppnetlib-client-connections)
6163

64+
target_link_libraries(twitter_search
65+
${BOOST_CLIENT_LIBS}
66+
${CMAKE_THREAD_LIBS_INIT}
67+
cppnetlib-uri
68+
cppnetlib-client-connections)
69+
6270
target_link_libraries(hello_world_server
6371
${BOOST_SERVER_LIBS}
6472
${CMAKE_THREAD_LIBS_INIT})
@@ -68,6 +76,7 @@ if (OPENSSL_FOUND)
6876
target_link_libraries(simple_wget ${OPENSSL_LIBRARIES})
6977
target_link_libraries(atom_reader ${OPENSSL_LIBRARIES})
7078
target_link_libraries(rss_reader ${OPENSSL_LIBRARIES})
79+
target_link_libraries(twitter_search ${OPENSSL_LIBRARIES})
7180
target_link_libraries(hello_world_server ${OPENSSL_LIBRARIES})
7281
endif (OPENSSL_FOUND)
7382

@@ -84,6 +93,7 @@ set_target_properties(http_client PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CPP-NETL
8493
set_target_properties(simple_wget PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CPP-NETLIB_BINARY_DIR}/example)
8594
set_target_properties(atom_reader PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CPP-NETLIB_BINARY_DIR}/example)
8695
set_target_properties(rss_reader PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CPP-NETLIB_BINARY_DIR}/example)
96+
set_target_properties(twitter_search PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CPP-NETLIB_BINARY_DIR}/example)
8797
set_target_properties(hello_world_server PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CPP-NETLIB_BINARY_DIR}/example)
8898
if (UNIX)
8999
set_target_properties(fileserver PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CPP-NETLIB_BINARY_DIR}/example)

libs/network/example/atom/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "atom.hpp"
88
#include <boost/network/protocol/http/client.hpp>
9+
#include <boost/foreach.hpp>
910
#include <iostream>
1011

1112
int main(int argc, char * argv[]) {

libs/network/example/rss/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "rss.hpp"
88
#include <boost/network/protocol/http/client.hpp>
9+
#include <boost/foreach.hpp>
910
#include <iostream>
1011

1112
int main(int argc, char * argv[]) {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright (c) Glyn Matthews 2011.
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// (See accompanying file LICENSE_1_0.txt or copy at
4+
// http://www.boost.org/LICENSE_1_0.txt)
5+
6+
7+
#include <boost/network/protocol/http/client.hpp>
8+
#include <boost/fusion/include/adapt_struct.hpp>
9+
#include <boost/variant/recursive_variant.hpp>
10+
#include <boost/spirit/home/qi.hpp>
11+
#include <boost/foreach.hpp>
12+
#include <iostream>
13+
14+
// This example uses the Twitter Search API.
15+
//
16+
// https://dev.twitter.com/docs/using-search
17+
18+
int main(int argc, char * argv[]) {
19+
using namespace boost::network;
20+
21+
if (argc != 2) {
22+
std::cout << "Usage: " << argv[0] << " <query>" << std::endl;
23+
return 1;
24+
}
25+
26+
try {
27+
http::client client;
28+
29+
uri::uri search_uri("http://search.twitter.com/search.json");
30+
31+
std::cout << "Searching Twitter for query: " << argv[1] << std::endl;
32+
uri::uri search_1;
33+
search_1 << search_uri << uri::query("q", uri::encoded(argv[1]));
34+
http::client::request request(search_1.string());
35+
http::client::response response = client.get(request);
36+
37+
std::cout << body(response) << std::endl;
38+
}
39+
catch (std::exception &e) {
40+
std::cerr << e.what() << std::endl;
41+
}
42+
43+
return 0;
44+
}
45+

0 commit comments

Comments
 (0)