/include/clients/twitpic/client.hpp

https://github.com/yutopp/oauth-lib-cpp · C++ Header · 103 lines · 81 code · 18 blank · 4 comment · 0 complexity · 52a7a1c2c28998ca8cc30fe3b60b2649 MD5 · raw file

  1. #ifndef YUTOPP_TWITPIC_CLIENT_HPP
  2. #define YUTOPP_TWITPIC_CLIENT_HPP
  3. #include "../basic_client.hpp"
  4. #include "policy.hpp"
  5. #include "request.hpp"
  6. #include "../twitter.hpp"
  7. namespace webapp
  8. {
  9. namespace clients
  10. {
  11. namespace twitpic
  12. {
  13. template<class Protocol = protocol::http, class Policy = policy::v2>
  14. class client
  15. : public basic_client<client<Protocol, Policy>, Protocol, Policy>
  16. {
  17. friend basic_client<client, Protocol, Policy>;
  18. public:
  19. explicit client( const string_type& key )
  20. : key_( key )
  21. {}
  22. client( const auth_key_type& auth_key, const string_type& key )
  23. : basic_client( auth_key )
  24. , key_( key )
  25. {}
  26. template<class Protocol_, class Policy_>
  27. client( const twitter::client<Protocol_, Policy_>& twitter, const string_type& key )
  28. : basic_client( twitter.get_auth_key() )
  29. , key_( key )
  30. {}
  31. template<class Protocol_, class Policy_>
  32. void link_to_twitter( const twitter::client<Protocol_, Policy_>& twitter )
  33. {
  34. auth_.set_key( twitter.get_auth_key() );
  35. }
  36. //
  37. void set_key( const string_type& key ) { key_ = key; }
  38. const string_type& get_key() const { return key_; }
  39. private:
  40. //
  41. std::stringstream make_request( const string_type& method, const string_type& path, string_type&, const string_type& body, const string_type& content_type ) const
  42. {
  43. std::stringstream ss;
  44. ss << method << " " << path << " HTTP/1.1\r\n";
  45. ss << "Host: " << Policy::get_domain() << "\r\n";
  46. ss << "Accept-Charset: utf-8\r\n";
  47. ss << "User-Agent: " << Policy::get_user_agent() << "\r\n";
  48. ss << "Content-type: " << content_type << "\r\n";
  49. ss << "X-Auth-Service-Provider: " << "https://api.twitter.com/1/account/verify_credentials.json" << "\r\n";
  50. ss << "X-Verify-Credentials-Authorization: " << auth_.make_header( "GET", "https://api.twitter.com/1/account/verify_credentials.json" ) << ", realm=\"" << util::url::encode( "http://api.twitter.com/" ) << "\"" << "\r\n";
  51. ss << "Content-Length: " << body.size() << "\r\n";
  52. ss << "Connection: close\r\n";
  53. ss << "\r\n";
  54. ss << body << std::flush;
  55. return ss;
  56. }
  57. template<class T>
  58. string_type custom_parameter( string_type&, string_type&, param_type& ) const
  59. {
  60. return "";
  61. }
  62. //only oririnal
  63. template<class T>
  64. //string_type custom_body( const param_type param )
  65. string_type custom_body( param_type& param ) const
  66. {
  67. param.insert( param_type::value_type( "key", util::u8( key ) ) );
  68. return generate_body_helper( param );
  69. }
  70. template<class T>
  71. string_type form_data_body( const string_type& boundary, const param_type& param )
  72. {
  73. return T::generate_body( boundary, key_, param );
  74. }
  75. template<class T>
  76. string_type custom_url( const string_type& name, param_type& ) const
  77. {
  78. return name;
  79. }
  80. string_type key_;
  81. };
  82. }
  83. }
  84. }
  85. #endif /*YUTOPP_TWITPIC_CLIENT_HPP*/