The microservice will write, append, or read from text files depending on the given action. The microservice reads from file-service.txt and obtains the users choice of action. The first line of file-service.txt will always be the users choice of action. Actions can be "a" for append, "w" for write, or "r" for read. The second line in "file-service.txt" will always be the name of the file to write, read, or append. For only the write and read action, the third line of file-service.txt will be the text to write or append. If the read action is used, the microservice will read the text from the file of the given name and then write that text into file-service.txt so that it is available for the user.
All Communication to and from the microservice is done through the "file-service.txt" in the Text Files directory
- Request file: Text Files/file-service.txt
- Response file: Text Files/file-service.txt
Request Parameter written to file-service.txt:
- action: string action to be taken by the microservice
- file name: string name of the text file the action is to be taken on
- text (optional): string text that is included if the user is using the write or append actions
Parameter Requirements for action:
- Written to the first line of service-file.txt
Parameter Requirements for file_name:
- Written to the second line of file-service.txt
- Does not have to include .txt file extension. The microservice will append .txt if it is not present in the name
Parameter Requirements for text:
- Written to the third line of file-service.txt
Return Value
- read_text: if the read action is chosen by the user, the microservice will return the read text as a string in
- file-service.txt .
- If the user chose the write or append actions then nothing is returned.
Return Value Requirements
- Read from the first line of file-service.txt
The requesting program will write "r" into the first line of file-service.txt, which is located in "Text Files" directory. The requesting program will write the name of the file to be read into the second line of file-service.txt. If the file to be read in the "Text Files" directory then the file name given written into line 2 of file-service.txt needs to follow this format: "./Text Files/file_to_read.txt". It is optional for the user to use the .txt in the name of the file to be read. If the extension is not included, then the microservice will append the file extension to the name. The user will run save_text_file.py . The requesting program will wait while the microservice reads from the file of the given name and writes that files contents into file-service.txt. The requesting program will read from file-service.txt to get the contents of the read file and make it accessible to the user.
import time
service_file = "../Text Files/file-service.txt"
action = "r"
file_to_read = "./Text Files/test_read.txt"
with open(service_file, "w") as f:
f.write(action + "\n" + file_to_read)
time.sleep(5)
with open(service_file, "r") as f:
content = f.readlines()
print(content)
The requesting program will write the "a" into the first line of file-service.txt, which is located in the "Text Files" directory. The requesting program will write the name of the file to be appended in the second line of file-service.txt. If the file to be appended is in the "Text Files" then the file name format should be as follows: "Text Files/file_to_append.txt". It is optional for the user to use the .txt in the name of the file to be appended. If the extension is not included, then the microservice will append the file extension to the name. The requesting program will write the text to be appended into the third line of file-service.txt. The user will run save_text_file.py. The microservice will read from file-service.txt and append the third line text into the file name given in the second line. No new lines will be added by the microservice when appending so it is up to the user to include the appropriate "\n" characters to their preference. With the append action, the microservice will not return anything to the requesting program.
service_file = "../Text Files/file-service.txt"
action = "a"
file_to_append = "Text Files/test_append.txt"
text_to_append = "enter text to append here "
with open(service_file, "w") as f:
f.write(action + "\n" + file_to_append + "\n" + text_to_append)
The requesting program will write "w" into the first line of file-service.txt which is located in the "Text Files" directory. The requesting program will write the name of the file to be written in to the second line of file-service.txt. If the text file is to be written into the "Text Files directory then the file name needs to follow this format: "Text Files/file_to_write.txt" It is optional for the user to use the .txt in the name of the file to be written. If the extension is not included, then the microservice will append the file extension to the name. If a text file with the given name already exists in the "Text Files" directory then its contents will be overwritten, otherwise a new text file of the given name will be created. The requesting program will write the text to be written into the third line offile-service.txt. The user will run save_text_file.py. The microservice will read the information from file-service.txt and write (or over-write) the text file of the given name, with the contents of line 3 from file-service.txt. All contents will be automatically written into the first line of the file be written into, so it up to the user to add appropriate "\n" characters as needed. With the append action, the microservice will not return anything to the requesting program.
service_file = "../Text Files/file-service.txt"
action = "w"
saved_file_name = "Text Files/test_write.txt"
saved_file_text = "Input text to save in test_write.txt here"
with open(service_file, "w") as f:
f.write(action + "\n" + saved_file_name + "\n" + saved_file_text)
The requesting program will wait at least one second after the request was made so that the microservice has time to open the file of the given name and copy its contents into file-service.txt. The requesting program will be able to access the received data by reading from file-service.txt which is located in the "Text Files" directory.
import time
service_file = "../Text Files/file-service.txt"
action = "r"
file_to_read = "./Text Files/test_read.txt"
with open(service_file, "w") as f:
f.write(action + "\n" + file_to_read)
time.sleep(5)
with open(service_file, "r") as f:
content = f.readlines()
print(content)
Here is an example call of the user using the microservice write action to create a new file with the name synth-params.txt and the following dictionary as text to save in that file. {"osc" : "sine", "amp": "100", "freq" : "432"}
The above code will write the following lines to the file-service.txt
The microservice will create the following text file in the Text Files directory
Here is an example call of the user using microservice read action to read from the synth_params.txt that was created in the above write action example call.
The above code will write the following to the file-service.txt
The microservice will read from synth_params.txt and write the contents into file-service.txt. The example call will print the contents as shown below.
Here is an example call of the user using the microservice append action to append to the synth_params.txt.
The above code will write the following to the file-service.txt.
The microservice will read the file name and the text to append. The text will be appended to to synth_params.txt as shown below.











