Django REST Client - django

I have server. He return json in next format:
{
"count": 6,
"next": null,
"previous": null,
"results": [
{
"id": 2,
"court": "http://reg-corruption.herokuapp.com/api/court/2/",
"result": "result2",
"date_meeting": "2014-03-12T17:50:30Z"
},
{
"id": 3,
"court": "http://reg-corruption.herokuapp.com/api/court/2/",
"result": "result22",
"date_meeting": "2014-03-13T17:50:46Z"
}, ] }
I want to write client. In client I can copy models.py, but how I can use it to send request to server. Main problem: I have file models.py and it is work with sqlite database. Can I use this file to connect to REST server? And can I load links to foreign key automatically?
Or what I mast to read or use?

You need neither the models.py nor DB to connect to the server. Once you have the REST API url, required headers and authentication details, you can just write a REST client in any language you are comfortable in and connect to the server.
I will put few examples:
Python : Rest-api-in-python
Python : python-rest-client
Java : java-restful-client
From you tags I understand you are using DRF for the server. You can check http://www.django-rest-framework.org/api-guide/testing/ also, for inbuilt testing REST client library in DRF.

Related

Intercom API (v2.0) search conversation not working

I tried to search conversation from Intercom API using Postman but it always return server error message.
I just followed their API docs.
request url: POST https://api.intercom.io/conversations/search?query=updated_at>1590278400
The query should be in JSON format in the Body, not in the querystring like you do in your example:
{
"query": {
"field": "updated_at",
"operator": ">",
"value": 1590278400
}
}
In Postman it looks like this (note: don't forget to add your authentication)

What is the best way to handle backend in flutter? Can it be linked with django?

I was researching and I could not find a proper solution. If you have some examples, please also explain why.
"Best way" of handling Flutter backend is a bit subjective as this will still depend on the experience of the developer/s or the team that will be handling the project.
Just like any other backend, one of the primary options we have to communicate it with our Flutter apps is thru REST APIs.
If you are using django, which I hope you do, you can use the package djangorestframework to expose API endpoints containing your data models from your database, presented in a JSON format.
Please visit this link in your browser (hope you got your JSON extension w/ you) or in Postman:
https://api.github.com/users/joshuadeguzman
You should then see the following output.
/ 20191230121253
// https://api.github.com/users/joshuadeguzman
{
"login": "joshuadeguzman",
"id": 20706361,
"node_id": "MDQ6VXNlcjIwNzA2MzYx",
"avatar_url": "https://avatars1.githubusercontent.com/u/20706361?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/joshuadeguzman",
"html_url": "https://github.com/joshuadeguzman",
"followers_url": "https://api.github.com/users/joshuadeguzman/followers",
"following_url": "https://api.github.com/users/joshuadeguzman/following{/other_user}",
"gists_url": "https://api.github.com/users/joshuadeguzman/gists{/gist_id}",
"starred_url": "https://api.github.com/users/joshuadeguzman/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/joshuadeguzman/subscriptions",
"organizations_url": "https://api.github.com/users/joshuadeguzman/orgs",
"repos_url": "https://api.github.com/users/joshuadeguzman/repos",
"events_url": "https://api.github.com/users/joshuadeguzman/events{/privacy}",
"received_events_url": "https://api.github.com/users/joshuadeguzman/received_events",
"type": "User",
"site_admin": false,
"name": "Joshua de Guzman",
"company": "#freelancer",
"blog": "jdg.ph",
"location": "Manila, PH",
"email": null,
"hireable": true,
"bio": "Software Engineer at #freelancer. Building tools for humans.",
"public_repos": 98,
"public_gists": 39,
"followers": 75,
"following": 21,
"created_at": "2016-07-28T15:19:54Z",
"updated_at": "2019-12-29T02:28:50Z"
}
Next thing you want to do is to parse this JSON string so that you can use it (as objects) in your Flutter App.
You can use the http plugin for that.
Example request
Future<http.Response> fetchGithubProfile(String username) {
return http.get('https://api.github.com/users/$');
}
Some articles you can read:
https://medium.com/flutter-community/django-search-flutter-1cb3e8a5db1a
https://flutter.dev/docs/cookbook/networking/fetch-data
https://alligator.io/flutter/flutter-http/
I also wrote an article on how you can setup Django + Django Rest Framework:
https://dev.to/joshuadeguzman/definitive-guide-developing-restful-apis-using-python-django-and-drf-2h7e
Some example apps that might help you:
https://github.com/ReeceRose/django-flutter-todo (Django + Flutter)
https://github.com/joshuadeguzman/flutter-moviehub (Demonstrates usage of http package with BLoC)

Generating new id for each request

My app communicates with the server in json rpc format - and each request from the client needs to have a new id generated by the client. jsonrpc specs example:
{"jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": 1}
and the next request would have an id=2 and so on.
I read that having global variables in ember is not recommended, so I guess I should keep the id in the application route, and have a function that sets id to be id++ and return the new value. However, I dont know how to access this function as I keep getting errors on stuff like
data["id"] = Addapp.ApplicationRoute.getNewJRId();
where the ApplicationRoute is undefined...
My questions are: Would this be a proper approach?
I do this, how?

Django REST FRAMEWORK, How can I create a custom URL linking a nested List

Referring to the Django Rest Framework tutorial app:
https://github.com/tomchristie/rest-framework-tutorial
Referring to the users page, that is:
domain/port/snippets/users/
The response is as follows, in my case:
HTTP 200 OK
Vary: Accept
Content-Type: text/html; charset=utf-8
Allow: GET, HEAD, OPTIONS
{
"count": 2,
"next": null,
"previous": null,
"results": [
{
"url": "domain/port/snippets/users/1/",
"username": "super",
"snippets": [
"domain/port/snippets/1/",
"domain/port/snippets/3/"
]
},
{
"url": "domain/port/snippets/users/2/",
"username": "super2",
"snippets": [
"domain/port/snippets/4/"
]
}
]
}
What I have been trying to do is to somehow replace the "snippets" list with a single URL that links to a page for user1_snippets in the case of the first user and to a page for user2's snippets in the case of user 2...
By "snippets list", I'm referring to this chunk of text (which could be long) : "snippets": [
"domain/port/snippets/1/",
"domain/port/snippets/3/"
]
So I just need to get the framework to produce something like the following code:
snippets
snippets
Then it will be really easy to develop the matching url conf and view.
The reason I want to do this is that in my own app, my analogous 'snippets' are very large in number, so I think it would be sensible to group them - on a separate page - for one (analogous) user. Then I can have the "user" page just for discovery of users. It will be quick to load, easy to interpret etc.
I know the answer lies in the documentation below, and I'll get there eventually but a few pointers would be really helpful.
http://django-rest-framework.org/api-guide/relations.html
Much appreciated,
There is a section on so called "Hyperlinking" in the tutorial:
http://django-rest-framework.org/tutorial/5-relationships-and-hyperlinked-apis.html
and as you said, you need to look for HyperlinkedIdentityField in the relations article.
So your serializer could look like this:
class HyperlinkedSerializer(serializers.HyperlinkedModelSerializer):
snippets = serializers.HyperlinkedIdentityField(view_name='snippet-list')
class Meta:
model = User
fields = ('snippets')
Then the url is resolved using the provided 'snippet-list' view_name.
Hope this helps.

how to create a HTTPserver / HTTPClient

I want to understand what kind of server responds to my HTTPClient? is this an ordinary apache HTTP server?
I need to know how i can submit POST via URL form, similar to
http://myserver/post?message=text&submit=send
can you give me some pointers on what to study?
this is a sample C++ HTTPClient code for a micro-controller
HTTPMap map;
HTTPText inText(str, 512);
map.put("Hello", "World");
map.put("test", "1234");
printf("\nTrying to post data...\n");
ret = http.post("http://httpbin.org/post", map, &inText);
if (!ret)
{
printf("Executed POST successfully - read %d characters\n", strlen(str));
printf("Result: %s\n", str);
}
else
{
printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
}
And this is the result it returns
Trying to post data...
Executed POST successfully - read 344 characters
Result: {
"headers": {
"Content-Length": "21",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"Connection": "close"
},
"url": "http://httpbin.org/post",
"data": "",
"origin": "<//my IP>",
"args": {},
"form": {
"Hello": "World",
"test": "1234"
},
"json": null,
"files": {}
}
It can be any HTTP server (web server). You can build a stand-alone HTTPServer using any .NET language, but also by writing an ISAPI plugin for ISS, or by installing PHP.
In fact, a normal web server could also be a web service. The difference usually is that a web server serves web pages, while a service serves specialized chunks of information, often not in HTML, but rather in JSON or another format that is more friendly for further processing.
It's mainly a symantic difference, technically there is virtually no difference.