Emir Hıfsı Öztoprak API Documentation - bounswe/2021SpringGroup1 GitHub Wiki
API Documentation
DESCRIPTION
Due to some personal problems (which I informed my teachers about), my API functions wasn't over by the time of the homework deadline and the Milestone Report 2. My teachers thankfully allowed me to continue my work after the deadline, so I pushed my last version of homework in a branch named 'emir'. I used a 3rd party API named Detect Language API. This API takes a string parameter, detects the languages used in that string and returns the corresponding data in JSON format.
Firstly I wrote a simple GET method named detectLanguagesInString, but then I couldn't think of a good POST method that is still relevant to our project, so I edited the createPost POST method written by Berke. Berke wrote that method for adding a new post to a community, then I added the feature where the method concatenates all the text related to the post, namely title, description and all text DataFields, and then the method detects the languages used in that post and stores the output as a DataField. I will give the documentation for the 2 API functions below. I also wrote multiple unit tests for each of these 2 methods. Due to the limited time, I couldn't deploy my project to an AWS EC2 instance, but you can run my code locally and try the API calls at the base url: http://localhost:8000 (or if you ran the program using docker: http://localhost:8080)
API FUNCTIONS
1. Detect Languages in a String
This GET method takes a 'text' parameter, then it gets the languages detected by Detect Language API, and it returns the detected languages in JSON format. If there are more than 1 language detected in the given string, the languages are concatenated, e.g. "tr,en".
Endpoint: /mainapp/external/detectLanguagesInString
Method: GET
Authorization: None
Parameters:
- text: You can write any string you prefer to this parameter. Please keep in mind that the 3rd party API detects languages more consistently if the text is longer.
Frontend Page
Example Usages and Responses:
- Example 1
-
Usage:
http://localhost:8000/mainapp/external/detectLanguagesInString?text=
-
Response:
-
{"Detected languages": }
- Example 2
-
Usage:
http://localhost:8000/mainapp/external/detectLanguagesInString?text=Hello world!
-
Response:
-
{"Detected languages": "en"}
- Example 3
-
Usage:
http://localhost:8000/mainapp/external/detectLanguagesInString?text=The text in this example will be in both English and Turkish. Kullandığımız API dil ne kadar çok kullanılırsa o kadar doğru sonuç veriyor.
-
Response:
-
{"Detected languages": "tr,en"}
- Example using Postman
GET request
2. Create Post
NOTE: I did not create this API function, this function was initially written by Berke. I only edited it to implement a language detection feature to the newly created post objects. He reviewed my pull request and told me it looked okay (pull request #91).
I could not think of a smart way of implementing a POST function that is still related to our project, then I thought all created posts should have their languages detected as well. So I added some code to Berke's function and managed to successfully add this new feature. It concatenates all the text related to the created post, namely its title, description and text DataFields. Then it sends the result string to the 3rd party API with a POST request. And then it stores the detected languages as a text DataField with the name 'Detected languages'. Below I will give further detail about this method.
Endpoint: /mainapp/external/createPost
Method: POST
Authorization: None
Parameters:
- post_template_id: You need to create a PostTemplate object before creating a post and enter the corresponding PostTemplate 'id' to this parameter. This is a required parameter.
- description: You can enter a relevant description to your post. This is a required parameter.
- title: You can enter a title to your post. This is a required parameter as well.
- i_textcontent or i_urlcontent: i is the id of a corresponding DataFieldTemp belonging to the selected PostTemplate. All the DataFieldTemp fields belonging to the selected PostTemplate are required.
Example Usages and Responses:
- Example using Postman
-
Info: In this example, before the POST request below, a PostTemplate object (id=2) is created with 1 DataFieldTemp belonging to that template named 'Question' (id=5).
-
Request:
-
-
- Response:
Response is showed as raw JSON data in the photo above, below I will put the JSON response in a better looking format:
{
"id": 9,
"posterid": 0,
"title": "İngilizce Türkçe karışık soru",
"description": "Bu kısmı Türkçe dolduruyorum.",
"date": "2021-06-23T12:18:30.833Z",
"dataFields": {
"1": {
"id": 12,
"postid": 9,
"name": "question",
"type": "text",
"content": {
"text": "\"This part will be in English. I wonder if our API will detect the correct languages, I am excited.\""
}
},
"2": {
"id": 13,
"postid": 9,
"name": "Detected languages",
"type": "text",
"content": {
"text": "en,tr"
}
}
}
}