File: wado-rs-client.cpp

package info (click to toggle)
odil 0.13.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,476 kB
  • sloc: cpp: 55,982; python: 3,947; javascript: 460; xml: 182; makefile: 99; sh: 36
file content (142 lines) | stat: -rw-r--r-- 3,752 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <iostream>
#include <sstream>
#include <string>

#include <boost/asio.hpp>

#include <odil/DataSet.h>
#include <odil/Value.h>
#include <odil/webservices/HTTPRequest.h>
#include <odil/webservices/HTTPResponse.h>
#include <odil/webservices/WADORSRequest.h>
#include <odil/webservices/WADORSResponse.h>

struct Printer
{
    typedef void result_type;

    std::ostream & stream;
    std::string indent;

    Printer(std::ostream & stream, std::string const & indent="")
    : stream(stream), indent(indent)
    {
        // Nothing else
    }

    template<typename T>
    void operator()(T const & value) const
    {
        for(auto const & item: value)
        {
            this->stream << item << " ";
        }
    }

    void operator()(odil::Value::DataSets const & value) const
    {
        this->stream << "\n";

        auto const last_it = --value.end();
        for(auto it=value.begin(); it!= value.end(); ++it)
        {
            Printer const printer(this->stream, this->indent+"  ");
            printer(*it);
            if(it != last_it)
            {
                this->stream << "----\n";
            }
        }
    }

    void operator()(odil::Value::Binary const & value) const
    {
        this->stream << this->indent << "(binary)";
    }

    void operator()(std::shared_ptr<odil::DataSet> const & data_set) const
    {
        for(auto const & item: *data_set)
        {
            this->stream << this->indent << item.first << " " << as_string(item.second.vr) << " ";
            odil::apply_visitor(*this, item.second.get_value());
            this->stream << "\n";
        }
    }
};

int main()
{
    odil::webservices::URL const root{
        "http", "demo.orthanc-server.com", "/dicom-web"};

    odil::webservices::WADORSRequest wado_request(root);

    wado_request.request_dicom(
        odil::webservices::Representation::DICOM_XML,
        std::map<std::string, std::string>(
        {
            {"study", "2.16.840.1.113669.632.20.1211.10000357775"},
            {"series", "1.3.46.670589.11.0.0.11.4.2.0.8743.5.5396.2006120114314125550"},
            {"instance", "1.3.46.670589.11.0.0.11.4.2.0.8743.5.5396.2006120114333648576"}
        })
    );

    auto http_request = wado_request.get_http_request();
    // Explicitly use HTTP/1.0 to avoid chunked encoding
    http_request.set_http_version("HTTP/1.0");
    http_request.set_header("Host", root.authority);
    http_request.set_header("Connection", "close");

    boost::asio::ip::tcp::iostream stream;
    stream.connect(root.authority, root.scheme);
    if(!stream)
    {
      std::cerr << "Error connecting: "
#if BOOST_VERSION >= 104700
            << stream.error().message()
#endif
            << std::endl;
      return 1;
    }

    stream << http_request;
    if(!stream)
    {
      std::cerr << "Error sending: "
#if BOOST_VERSION >= 104700
            << stream.error().message()
#endif
            << std::endl;
      return 1;
    }

    odil::webservices::HTTPResponse http_response;
    stream >> http_response;
    if(!stream)
    {
      std::cerr << "Error receiving: "
#if BOOST_VERSION >= 104700
            << stream.error().message()
#endif
            << std::endl;
      return 1;
    }

    odil::webservices::WADORSResponse const wado_response(http_response);
    std::cout
        << "Datasets: " << wado_response.get_data_sets().size() << std::endl;
    std::cout
        << "Bulk data: " << wado_response.get_bulk_data().size() << std::endl;
    std::cout
        << "Partial content: " << std::boolalpha << wado_response.is_partial()
        << std::endl;

    Printer printer(std::cout);
    for(auto const & data_set: wado_response.get_data_sets())
    {
        printer(data_set);
    }

    return 0;
}