reference

This documentation is automatically generated from the openFrameworks source code using doxygen and refers to the most recent release, version 0.12.0.

ofURLFileLoader.h
Go to the documentation of this file.
1#pragma once
2
3#include "ofEvents.h"
4// ofBuffer only
5#include "ofFileUtils.h"
6#include "ofConstants.h"
7#include <map>
8
10
14public:
16 ofHttpRequest(const std::string& url, const std::string& name,bool saveTo=false);
17
18 std::string url;
19 std::string name;
20 bool saveTo;
21 std::map<std::string,std::string> headers;
22 std::string body;
23 std::string contentType;
24 std::function<void(const ofHttpResponse&)> done;
25 size_t timeoutSeconds = 0;
26
28 int getId() const;
29 OF_DEPRECATED_MSG("Use getId().", int getID());
30
36
37private:
38 int id;
39 static int nextID;
40};
41
45public:
47 ofHttpResponse(const ofHttpRequest& request, const ofBuffer& data, int status, const std::string& error);
48 ofHttpResponse(const ofHttpRequest& request, int status, const std::string& error);
49
50 operator ofBuffer&();
51
54 int status;
55 std::string error;
56};
57
62ofHttpResponse ofLoadURL(const std::string& url);
63
69int ofLoadURLAsync(const std::string& url, const std::string& name=""); // returns id
70
76ofHttpResponse ofSaveURLTo(const std::string& url, const of::filesystem::path& path);
77
80
86int ofSaveURLAsync(const std::string& url, const of::filesystem::path& path);
87
90void ofRemoveURLRequest(int id);
91
94
96void ofStopURLLoader();
97
99
100template<class T>
102 ofAddListener(ofURLResponseEvent(),obj,&T::urlResponse);
103}
104
105template<class T>
107 ofRemoveListener(ofURLResponseEvent(),obj,&T::urlResponse);
108}
109
111
115 public:
116
118
123 ofHttpResponse get(const std::string& url);
124
130 int getAsync(const std::string& url, const std::string& name="");
131
137 ofHttpResponse saveTo(const std::string& url, const of::filesystem::path& path);
138
144 int saveAsync(const std::string& url, const of::filesystem::path& path);
145
148 void remove(int id);
149
151 void clear();
152
154 void stop();
155
156 // \brief low level HTTP request implementation
160
161 // \brief low level HTTP request implementation
164 int handleRequestAsync(const ofHttpRequest& request);
165
166 private:
167 std::shared_ptr<ofBaseURLFileLoader> impl;
168};
169
170
174public:
175
177
182 virtual ofHttpResponse get(const std::string& url)=0;
183
189 virtual int getAsync(const std::string& url, const std::string& name="")=0;
190
196 virtual ofHttpResponse saveTo(const std::string& url, const of::filesystem::path& path)=0;
197
203 virtual int saveAsync(const std::string& url, const of::filesystem::path& path)=0;
204
207 virtual void remove(int id)=0;
208
210 virtual void clear()=0;
211
213 virtual void stop()=0;
214
218 virtual ofHttpResponse handleRequest(const ofHttpRequest & request) = 0;
219 virtual int handleRequestAsync(const ofHttpRequest& request)=0; // returns id
220
221};
loads a file from a URL using an HTTP request
Definition ofURLFileLoader.h:173
virtual ofHttpResponse handleRequest(const ofHttpRequest &request)=0
low level HTTP request implementation blocks until a response is returned or the request times out
virtual void stop()=0
stop & remove all active and waiting HTTP requests
virtual void clear()=0
clear all active HTTP requests from the queue
virtual ofHttpResponse saveTo(const std::string &url, const of::filesystem::path &path)=0
make an HTTP request and save the response data to a file blocks until a response is returned or the ...
virtual ofHttpResponse get(const std::string &url)=0
make an HTTP request blocks until a response is returned or the request times out
virtual int saveAsync(const std::string &url, const of::filesystem::path &path)=0
make an asynchronous HTTP request and save the response data to a file will not block,...
virtual int handleRequestAsync(const ofHttpRequest &request)=0
virtual int getAsync(const std::string &url, const std::string &name="")=0
make an asynchronous HTTP request will not block, placed in a queue and run using a background thread
virtual void remove(int id)=0
remove an active HTTP request from the queue
virtual ~ofBaseURLFileLoader()
Definition ofURLFileLoader.h:176
Definition ofFileUtils.h:15
Definition ofEvent.h:444
an HTTP GET or POST request
Definition ofURLFileLoader.h:13
std::string body
POST body data.
Definition ofURLFileLoader.h:22
size_t timeoutSeconds
Definition ofURLFileLoader.h:25
enum ofHttpRequest::Method method
std::string name
optional name key for sorting
Definition ofURLFileLoader.h:19
std::function< void(const ofHttpResponse &)> done
Definition ofURLFileLoader.h:24
std::string contentType
POST data mime type.
Definition ofURLFileLoader.h:23
OF_DEPRECATED_MSG("Use getId().", int getID())
ofHttpRequest(const std::string &url, const std::string &name, bool saveTo=false)
int getId() const
Definition ofURLFileLoader.cpp:348
bool saveTo
save to a file once the request is finised?
Definition ofURLFileLoader.h:20
ofHttpRequest()
Definition ofURLFileLoader.cpp:332
Method
HTTP request type.
Definition ofURLFileLoader.h:32
@ GET
request data from a specified resource (via url)
Definition ofURLFileLoader.h:33
@ POST
submit data to be processed to a specified resource (via url)
Definition ofURLFileLoader.h:34
std::map< std::string, std::string > headers
HTTP header keys & values.
Definition ofURLFileLoader.h:21
std::string url
request url
Definition ofURLFileLoader.h:18
an HTTP response to a GET or POST request
Definition ofURLFileLoader.h:44
std::string error
HTTP error string, if any (OK, Not Found, etc)
Definition ofURLFileLoader.h:55
ofHttpResponse(const ofHttpRequest &request, const ofBuffer &data, int status, const std::string &error)
int status
HTTP response status (200: OK, 404: Not Found, etc)
Definition ofURLFileLoader.h:54
ofHttpRequest request
matching HTTP request for this response
Definition ofURLFileLoader.h:52
ofHttpResponse(const ofHttpRequest &request, int status, const std::string &error)
ofHttpResponse()
Definition ofURLFileLoader.cpp:357
ofBuffer data
response raw data
Definition ofURLFileLoader.h:53
loads a file from a URL using an HTTP request
Definition ofURLFileLoader.h:114
int handleRequestAsync(const ofHttpRequest &request)
Definition ofURLFileLoader.cpp:320
ofHttpResponse handleRequest(const ofHttpRequest &request)
Definition ofURLFileLoader.cpp:316
void clear()
clear all active HTTP requests from the queue
Definition ofURLFileLoader.cpp:308
void stop()
stop & remove all active and waiting HTTP requests
Definition ofURLFileLoader.cpp:312
int saveAsync(const std::string &url, const of::filesystem::path &path)
make an asynchronous HTTP request and save the response data to a file will not block,...
Definition ofURLFileLoader.cpp:300
ofHttpResponse saveTo(const std::string &url, const of::filesystem::path &path)
make an HTTP request and save the response data to a file blocks until a response is returned or the ...
Definition ofURLFileLoader.cpp:296
int getAsync(const std::string &url, const std::string &name="")
make an asynchronous HTTP request will not block, placed in a queue and run using a background thread
Definition ofURLFileLoader.cpp:292
ofHttpResponse get(const std::string &url)
make an HTTP request blocks until a response is returned or the request times out
Definition ofURLFileLoader.cpp:288
void remove(int id)
remove an active HTTP request from the queue
Definition ofURLFileLoader.cpp:304
ofURLFileLoader()
Definition ofURLFileLoader.cpp:278
void ofRemoveListener(EventType &event, ListenerClass *listener, void(ListenerClass::*listenerMethod)(const void *, ArgumentsType &), int prio=OF_EVENT_ORDER_AFTER_APP)
Definition ofEventUtils.h:120
void ofAddListener(EventType &event, ListenerClass *listener, void(ListenerClass::*listenerMethod)(const void *, ArgumentsType &), int prio=OF_EVENT_ORDER_AFTER_APP)
Definition ofEventUtils.h:19
ofHttpResponse ofSaveURLTo(const std::string &url, const of::filesystem::path &path)
make an HTTP GET request and save the response data to a file blocks until a response is returned or ...
int ofSaveURLAsync(const std::string &url, const of::filesystem::path &path)
make an asynchronous HTTP request and save the response data to a file will not block,...
ofHttpResponse ofLoadURL(const std::string &url)
make an HTTP GET request blocks until a response is returned or the request times out
void ofRegisterURLNotification(T *obj)
Definition ofURLFileLoader.h:101
void ofRemoveURLRequest(int id)
remove an active HTTP request from the queue
Definition ofURLFileLoader.cpp:399
int ofLoadURLAsync(const std::string &url, const std::string &name="")
make an asynchronous HTTP GET request will not block, placed in a queue and run using a background th...
void ofUnregisterURLNotification(T *obj)
Definition ofURLFileLoader.h:106
void ofStopURLLoader()
stop & remove all active and waiting HTTP requests
Definition ofURLFileLoader.cpp:407
ofEvent< ofHttpResponse > & ofURLResponseEvent()
Definition ofURLFileLoader.cpp:19
void ofRemoveAllURLRequests()
remove all active HTTP requests from the queue
Definition ofURLFileLoader.cpp:403