Gbox 4.20
Grow box automation and monitoring - <a href='https://sites.google.com/site/growboxguy/'>https://sites.google.com/site/growboxguy/</a>
 
Loading...
Searching...
No Matches
ELClientRest.h
Go to the documentation of this file.
1
7// Copyright (c) 2016 by B. Runnels and T. von Eicken
8
9#ifndef _EL_CLIENT_REST_H_
10#define _EL_CLIENT_REST_H_
11
12#include <Arduino.h>
13#include "FP.h"
14#include "ELClient.h"
15
16// Default timeout for REST requests when waiting for a response
17#define DEFAULT_REST_TIMEOUT 5000
19typedef enum {
20 HTTP_STATUS_OK = 200
22
23// The ELClientRest class makes simple REST requests to a remote server. Each instance
24// is used to communicate with one server and multiple instances can be created to make
25// requests to multiple servers.
26// The ELClientRest class does not support concurrent requests to the same server because
27// only a single response can be recevied at a time and the responses of the two requests
28// may arrive out of order.
29// A major limitation of the REST class is that it does not store the response body. The
30// response status is saved in the class instance, so after a request completes and before
31// the next request is made a call to getResponse will return the status. However, only a pointer
32// to the response body is saved, which means that if any other message arrives and is
33// processed then the response body is overwritten by it. What this means is that if you
34// need the response body you best use waitResponse or ensure that any call to ELClient::process
35// is followed by a call to getResponse. Ideally someone improves this class to take a callback
36// into the user's sketch?
37// Another limitation is that the response body is 100 chars long at most, this is due to the
38// limitation of the SLIP protocol buffer available.
40 public:
42
43 // Initialize communication to a remote server, this communicates with esp-link but does not
44 // open a connection to the remote server. Host may be a hostname or an IP address,
45 // security causes HTTPS to be used (not yet supported). Returns 0 if the set-up is
46 // successful, returns a negative error code if it failed.
47 int begin(const char* host, uint16_t port=80, boolean security=false);
48
49 // Make a request to the remote server. The data must be null-terminated
50 void request(const char* path, const char* method, const char* data=NULL);
51
52 // Make a request to the remote server.
53 void request(const char* path, const char* method, const char* data, int len);
54
55 // Make a GET request to the remote server with NULL-terminated data
56 void get(const char* path, const char* data=NULL);
57
58 // Make a POST request to the remote server with NULL-terminated data
59 void post(const char* path, const char* data);
60
61 // Make a PUT request to the remote server with NULL-terminated data
62 void put(const char* path, const char* data);
63
64 // Make a DELETE request to the remote server
65 void del(const char* path);
66
67 // Retrieve the response from the remote server, returns the HTTP status code, 0 if no
68 // response (may need to wait longer)
69 uint16_t getResponse(char* data, uint16_t maxLen);
70
71 // Wait for the response from the remote server, returns the HTTP status code, 0 if no
72 // response (timeout occurred). This is not recommended except for quick demos, use
73 // getResponse periodically instead.
74 uint16_t waitResponse(char* data, uint16_t maxLen, uint32_t timeout=DEFAULT_REST_TIMEOUT);
75
76 // Set the user-agent for all subsequent requests
77 void setUserAgent(const char* value);
78
79 // Set the Content-Type Header for all subsequent requests
80 void setContentType(const char* value);
81
82 // Set a custom header for all subsequent requests
83 void setHeader(const char* value);
84
85 private:
86 int32_t remote_instance;
87 ELClient *_elc;
88 void restCallback(void* resp);
89 FP<void, void*> restCb;
91 int16_t _status;
92 uint16_t _len;
93 void *_data;
96};
97#endif // _EL_CLIENT_REST_H_
HTTP_STATUS
@ HTTP_STATUS_OK
#define DEFAULT_REST_TIMEOUT
Definitions for ELClient.
Core Utility - Templated Function Pointer Class.
void setUserAgent(const char *value)
Set user agent of header.
void put(const char *path, const char *data)
Send PUT request to REST server.
void del(const char *path)
Send DELETE request to REST server.
int begin(const char *host, uint16_t port=80, boolean security=false)
Initialize communication to a REST server.
void get(const char *path, const char *data=NULL)
Send GET request to REST server.
void request(const char *path, const char *method, const char *data=NULL)
Send request to REST server.
void setHeader(const char *value)
Set generic header content.
uint16_t waitResponse(char *data, uint16_t maxLen, uint32_t timeout=DEFAULT_REST_TIMEOUT)
Wait for the response.
void post(const char *path, const char *data)
Send POST request to REST server.
void setContentType(const char *value)
Set content type of header.
uint16_t getResponse(char *data, uint16_t maxLen)
Retrieve response.
API abstraction for a Function Pointers.
Definition FP.h:136