├── images ├── get.png ├── get2.png ├── get3.png ├── post.png └── representational-state-transfer-diagram.png ├── users.csv ├── flask_app.py └── README.md /images/get.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abugraokkali/Rest-API/HEAD/images/get.png -------------------------------------------------------------------------------- /images/get2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abugraokkali/Rest-API/HEAD/images/get2.png -------------------------------------------------------------------------------- /images/get3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abugraokkali/Rest-API/HEAD/images/get3.png -------------------------------------------------------------------------------- /images/post.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abugraokkali/Rest-API/HEAD/images/post.png -------------------------------------------------------------------------------- /users.csv: -------------------------------------------------------------------------------- 1 | name,age,city 2 | Tom,30,New York 3 | Rob,27,Washington 4 | John,25,London 5 | Ali,21,Ankara 6 | -------------------------------------------------------------------------------- /images/representational-state-transfer-diagram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abugraokkali/Rest-API/HEAD/images/representational-state-transfer-diagram.png -------------------------------------------------------------------------------- /flask_app.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, request 2 | from flask_restful import Api, Resource 3 | import pandas as pd 4 | 5 | app = Flask(__name__) 6 | api = Api(app) 7 | 8 | class Users(Resource): 9 | def get(self): 10 | data = pd.read_csv('users.csv') 11 | data = data.to_dict('records') 12 | return {'data' : data}, 200 13 | 14 | def post(self): 15 | json = request.get_json() 16 | req_data = pd.DataFrame({ 17 | 'name' : [json['name']], 18 | 'age' : [json['age']], 19 | 'city' : [json['city']] 20 | }) 21 | data = pd.read_csv('users.csv') 22 | data = pd.concat([data, req_data], ignore_index=True) 23 | data.to_csv('users.csv', index=False) 24 | return {'message' : 'Record successfully added.'}, 200 25 | 26 | def delete(self): 27 | name = request.args['name'] 28 | data = pd.read_csv('users.csv') 29 | 30 | if name in data['name'].values: 31 | data = data[data['name'] != name] 32 | data.to_csv('users.csv', index=False) 33 | return {'message': 'Record successfully deleted.'}, 200 34 | else: 35 | return {'message': 'Record not found.'}, 404 36 | 37 | class Cities(Resource): 38 | def get(self): 39 | data = pd.read_csv('users.csv',usecols=[2]) 40 | data = data.to_dict('records') 41 | return {'data' : data}, 200 42 | 43 | class Name(Resource): 44 | def get(self,name): 45 | data = pd.read_csv('users.csv') 46 | data = data.to_dict('records') 47 | for entry in data: 48 | if entry['name'] == name : 49 | return {'data' : entry}, 200 50 | return {'message' : 'No entry found with this name !'}, 404 51 | 52 | 53 | # Add URL endpoints 54 | api.add_resource(Users, '/users') 55 | api.add_resource(Cities, '/cities') 56 | api.add_resource(Name, '/') 57 | 58 | 59 | if __name__ == '__main__': 60 | # app.run(host="0.0.0.0", port=5000) 61 | app.run() 62 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # REST API Nedir ? 2 | 3 | REST (**R**epresentational **S**tate **T**ransfer) Temsili Durum Aktarımı anlamına gelir. 4 | 5 | Bir istemci bir sunucudan kaynaklar hakkında bilgi almak için bir istekte bulunduğunda, sunucu kaynağın mevcut durumunu istemci makineye geri aktarır. 6 | 7 | ![representational-state-transfer-diagram](images/representational-state-transfer-diagram.png) 8 | 9 | Yukarıdaki şekilde de görebilceğiniz gibi , istemci bir veri tabanı sunucusundan veri talep edebileceğiniz PC'nizdir ve tüm iletişim REST API'leri üzerinden yapılır. 10 | 11 | Bunun için de birkaç farklı yöntem vardır : 12 | 13 | - **GET** - İstemci tarafından sunucudan veri seçmek veya almak için kullanılır. 14 | 15 | - **POST** - İstemci tarafından sunucuya veri göndermek veya yazmak için kullanılır. 16 | 17 | - **PUT** - İstemci tarafından sunucudaki mevcut verileri güncellemek için kullanılır. 18 | 19 | - **DELETE** - İstemci tarafından sunucudaki mevcut verileri silmek için kullanılır. 20 | 21 | 22 | 23 | # REST API Nasıl Oluşturulur ? 24 | 25 | API'ler Java, C#, Python vb. gibi istediğiniz herhangi bir programlama dili kullanılarak oluşturulabilir. 26 | 27 | Bu uygulamada, bir API oluşturmak için Python'u kullanacağız ve bunun için Flask olarak bilinen bir kütüphaneden yararlanacağız. Flask, bizim için bir sunucu oluşturmak için ağır kaldırmanın çoğunu yapan popüler bir hafif web uygulaması geliştirme çerçevesidir ve geliştiriciler olarak yalnızca API'leri oluşturmak için iş mantığına odaklanmamız gerekir. 28 | 29 | 30 | 31 | # Kurulum 32 | 33 | Şimdi, Rest-API'yi oluşturmak için kullanacağımız flask, flask_restful ve veri işlemede kullanacağımız pandas paketlerini python3-pip ile kuralım. 34 | 35 | ```bash 36 | $ sudo apt install python3-pip 37 | ``` 38 | 39 | ```bash 40 | $ pip3 install flask 41 | ``` 42 | 43 | ```bash 44 | $ pip3 install flask_restful 45 | ``` 46 | 47 | ```bash 48 | $ pip3 install pandas 49 | ``` 50 | 51 | # Kod 52 | 53 | Gerekli paketlerin import edilmesi. 54 | ```python 55 | from flask import Flask 56 | from flask_restful import Api, Resource, reqparse 57 | import pandas as pd 58 | ``` 59 | Api nesnesinin oluşturulması. 60 | ```python 61 | app = Flask(__name__) 62 | api = Api(app) 63 | ``` 64 | GET çağırıldığında users.csv okunması, dict formatına çevrilmesi ve döndürülmesi. 65 | ```python 66 | class Users(Resource): 67 | def get(self): 68 | data = pd.read_csv('users.csv') 69 | data = data.to_dict('records') 70 | return {'data' : data}, 200 71 | ``` 72 | POST çağırıldığında girdilerin parse edilmesi ve users.csv'ye bir satır eklenmesi. 73 | ```python 74 | def post(self): 75 | name = request.args['name'] 76 | age = request.args['age'] 77 | city = request.args['city'] 78 | 79 | data = pd.read_csv('users.csv') 80 | 81 | new_data = pd.DataFrame({ 82 | 'name' : [name], 83 | 'age' : [age], 84 | 'city' : [city] 85 | }) 86 | 87 | data = data.append(new_data, ignore_index = True) 88 | data.to_csv('users.csv', index=False) 89 | return {'data' : new_data.to_dict('records')}, 200 90 | ``` 91 | 92 | Çoğu zaman bir API'de kaynağınızın birden çok URL'si olacaktır. Api nesnesindeki add_resource() fonksiyonu ile birden çok URL iletebilirsiniz. Her biri bir Resource'a yönlendirilecektir. 93 | ```python 94 | api.add_resource(Users, '/users') 95 | ``` 96 | 97 | 98 | # Çalıştırma 99 | 100 | ```bash 101 | $ python3 flask_app.py 102 | 103 | Serving Flask app 'flask_app' (lazy loading) 104 | 105 | Environment: production 106 | WARNING: This is a development server. Do not use it in a production deployment. 107 | Use a production WSGI server instead. 108 | 109 | Debug mode: off 110 | 111 | Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) 112 | ``` 113 | 114 | # Testler 115 | VSCode eklentisi olan [Thunder Client](https://www.thunderclient.com) kullanılabilir. 116 | 117 | ```GET http://127.0.0.1:5000/users``` 118 | ![Screenshot_3](images/get.png) 119 | 120 | ```POST http://127.0.0.1:5000/users?name=Bugra&age=29&city=Istanbul``` 121 | ![Screenshot_5](images/post.png) 122 | 123 | ```GET http://127.0.0.1:5000/users``` 124 | ![Screenshot_5](images/get2.png) 125 | 126 | ```GET http://127.0.0.1:5000/Tom``` 127 | ![Screenshot_6](images/get3.png) 128 | 129 | 130 | Bu repository'nin dev.to yazısına [linkten](https://dev.to/aciklab/rest-api-e26) ulaşabilirsiniz. 131 | --------------------------------------------------------------------------------