-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOSC_REST.py
More file actions
143 lines (114 loc) · 3.69 KB
/
OSC_REST.py
File metadata and controls
143 lines (114 loc) · 3.69 KB
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
143
# OSC-REST
# A bridge between Open Sound Control (OSC) and REST APIs
# Office Hours Global Community Project
# Created and maintained by Andy Carluccio - Washington, D.C.
#OSC variables & libraries
from pythonosc import dispatcher
from pythonosc import osc_server
from pythonosc import osc_message_builder
from pythonosc import udp_client
from pythonosc import osc_bundle
from pythonosc import osc_bundle_builder
#Argument management
import argparse
#system
import sys
#REST
import requests
import json
#GET Request
def getRequest(unused_addr, *args):
print("Received info for GET Request")
uri = args[0]
file_name = ""
tag = args[1]
#determine if a file name was provided as an arg
if(len(args) > 2):
file_name = args[2]
response = requests.get(uri)
res_str = "OSC-REST ERROR"
try:
res_str = str(response.json())
except:
print("OSC-REST ERROR: Could not communicate with server, or server returned 204 / other response code")
#if a file name was provided, save the response to disk and notify via OSC
if(file_name != ""):
print("Sending response via disk")
outFile = open(file_name,"w+")
outFile.write(res_str)
osc_str = "/REST/OSC/fileComplete/" + tag
client.send_message(osc_str, 1)
#otherwise, send the entire response via OSC
else:
print("Sending response via OSC")
osc_str = "/REST/OSC/" + tag
client.send_message(osc_str, res_str)
#POST Request
def postRequest(unused_addr, uri, tag, args):
print("Received info for POST request")
in_json = json.loads(args)
print(str(in_json))
reply = requests.post(uri, json = in_json)
res_str = "OSC-REST ERROR"
try:
res_str = str(reply.text)
except:
print("OSC-REST ERROR: Could not communicate with server, or server returned 204 / other response code")
osc_str = "/REST/OSC/" + tag
client.send_message(osc_str, res_str)
#PUT Request
def putRequest(unused_addr, uri, tag, args):
print("Received info for PUT request")
in_json = json.loads(args)
print(str(in_json))
reply = requests.put(uri, json = in_json)
res_str = "OSC-REST ERROR"
try:
res_str = str(reply.text)
except:
print("OSC-REST ERROR: Could not communicate with server, or server returned 204 / other response code")
osc_str = "/REST/OSC/" + tag
client.send_message(osc_str, res_str)
#Main execution script--------------------------------------
if __name__ == "__main__":
#Greeting
print("Welcome to OSC-REST")
print("Created by Andy Carluccio")
print("This program establishes a bidirectional OSC interface with REST APIs")
print("See ReadMe for commands and useage")
print()
#OSC Setup
print("Would you like to [1] Input network parameters or [2] use default: 127.0.0.1:1234 (sending) :7050 (receiving)")
send_ip = "127.0.0.1"
send_port = 1234
receive_port = 7050
selection = int(input())
if(selection == 1):
print("Input network parameters")
send_ip = str(input("Send ip?: "))
send_port = int(input("Send port?: "))
receive_port = int(input("Receive port?: "))
else:
print("Using default network settings")
#create the osc sending client
client = udp_client.SimpleUDPClient(send_ip,send_port)
#catches OSC messages
dispatcher = dispatcher.Dispatcher()
#map functions here:
dispatcher.map("/OSC/REST/GET", getRequest)
dispatcher.map("/OSC/REST/POST", postRequest)
dispatcher.map("/OSC/REST/PUT", putRequest)
#set up server to listen for osc messages
server = osc_server.ThreadingOSCUDPServer((send_ip,receive_port),dispatcher)
#Print the info
sys.stdout.write("Opened Client on: ")
sys.stdout.write(send_ip)
sys.stdout.write(":")
sys.stdout.write(str(send_port))
sys.stdout.write('\n')
sys.stdout.write("Listening on: ")
sys.stdout.write(str(receive_port))
sys.stdout.write('\n')
print()
#begin the infinite loop
server.serve_forever()