generate marshmallow-sqlalchemy ModelSchema like JSONAPI format - python

I am using flask-Restplus for API, and in GET method I want to return JSON like JSONAPI format:
{"data":[
{
"type":"articles",
"id":"1",
"attributes":{
"title":"JSON API paints my bikeshed!"
},
"relationships":{
"author":{
"links":{
"self":"http://example.com/articles/1/relationships/author",
"related":"http://example.com/articles/1/author"
},
"data":{
"type":"people",
"id":"9"
}
},
"comments":{
"links":{
"self":"http://example.com/articles/1/relationships/comments",
"related":"http://example.com/articles/1/comments"
},
"data":[
{
"type":"comments",
"id":"5"
},
{
"type":"comments",
"id":"12"
}
]
}
},
"links":{
"self":"http://example.com/articles/1"
}
}]}
What I am doing is creating Schema class and mentioning every field in it, instead of doing this manual work I want to generate it based on my database model.
So I have used marshmallow-sqlalchemy for it and mentioned model name in it, but it returns simple JSON. I want same JSON structure like JSONAPI by using marshmallow-sqlalchemy.
How can I do this?

Related

Tweepy get_bookmarks() - User Fields Not Working Despite Proper Arguments

I am using the tweepy library to retrieve user bookmarks, but I am unable to retrieve the profile picture URL despite passing the correct user_field.
According to the tweepy documentation, the only available expansion for this endpoint is "expansions=owner_id". I am not sure what this means or how to retrieve the profile picture URL.
Can someone help me understand this and provide a solution?
bookmarks = client.get_bookmarks(expansions=["author_id"],
user_fields=["profile_image_url"])
The response object is as follows:
{
"data": [
{
"id": 1621356248334483456,
"text": "The majesty of nature is on full display at Yosemite National Park. Visit now to experience it for yourself: https://natureloversparadise.com"
},
{
"id": 1620813010569945091,
"text": "Discover the beauty of the Great Barrier Reef with a snorkeling adventure. Book now at https://greatbarrierreefadventures.com"
}
],
"includes": {
"users": [
{
"id": 123456789,
"username": "naturelover",
"name": "Nature Enthusiast"
},
{
"id": 987654321,
"username": "oceanadventurer",
"name": "Ocean Adventurer"
}
]
}
}
As you can see, the includes section only contains basic information about the authors of the tweets (id , username , and name ), and no information about profile pictures.
Thank you for your help.
I have successfully made the necessary scope access and authentication. However, I think the get_bookmark() method might only return basic author information like author_id user fields and need confirmation before using user_id to call user information from different method.

What is best way to deserialize JSON into multiple models in django rest framework

I am quite new to django rest framework and I would like to discuss what is the best way to implement an API endpoints I am aiming to have.
I have following JSON example:
[
{
'top_lvl_key1': {
id: 1
'some_key1': "value"
'some_key2' : "value"
}
},
{
'top_lvl_key2': {
id: 1
'some_key1': "value"
'some_key2' : "value"
}
},
{
'top_lvl_key1': {
id: 2
'some_key1': "value"
'some_key3' : "value"
}
},
{
'top_lvl_key3': {
id: 1
'some_key1': "value"
'some_key2' : "value"
}
}
]
and I want to have 3 endpoints
[POST] /insert inserting and parsing the entire JSON to store it into models
[GET] /detail/<top_lvl_key>/ Display all record for specfici top_lvl_key
[GET] /detail//<top_lvl_key>/id/ Display a record for specific top_lvl_key and specific id (only 1 record)
As you can see, I need to store each type of top_lvl_key to different model so I can retrieve for GET requests. However I am not sure how should I got about serializing the JSON, since the dictionaries of the top_lvl_keys can differ. For example top_lvl_key_1 can sometimes have some_key_1 and some_key_2, but sometimes some_key_1 and some_key_2. So far I have been implementing the models with blank=True, null=True, but I am not sure this is best solution as there a lot of attributes which can be NULL now. Is there a better way to go about this?
Thanks a lot in advance

How to find available operations in GraphQL URL?

Pardon for such vague question. I have a URL http://example.com/graphql. I want to know what operations are available. Is there anyway to find it out?
GraphQL has the introspection system which allow you to query its schema.You can construct GraphQL query to explore its types.
For example ,to find out all the query and mutation root fields , you can POST the following JSON body to http://example.com/graphql with content type set to application/json
{
"query" : "{__schema {queryType {name fields {name}}mutationType {name fields {name}}}}"
}
Or using GET and putting the GraphQL query to the query parameter query :
http://example.com/graphql/?query={__schema {queryType {name fields {name}}mutationType {name fields {name}}}}
which will return something likes:
{
"data": {
"__schema": {
"queryType": {
"name": "Query",
"fields": [
{
"name": "searchFoo"
},
{
"name": "searchBar"
}
]
},
"mutationType": {
"name": "Mutation",
"fields": [
{
"name": "createFoo"
},
{
"name": "updateFoo"
}
]
}
}
}
But the much easier way is to use a GraphQL client such as GraphiQL or Insomnia or ..... which most probably will provide you an interactive UI to exploring the GraphQL Schema and has some auto-complete/hint feature for assisting you to construct the GraphQL query.

Edit response object of login in flask-security

I am making a small app which requires login functionality. I am using flask for making api's and flask-security to have login feature.
Login feature is working fine, but I need response object in some customised form.
Right now I am getting response object as:
{
"meta": {
"code": 200
},
"response": {
"user": {
"authentication_token": "some token",
"id": "some id"
}
}
}
I need output in some customised form which will be:
{
"meta": {
"code": 200
},
"response": {
"user": {
"authentication_token": "some token",
"id": "some id",
"role": "some role"
}
}
}
I want role of the user along with response. I saw flask-security-config but didn't get any way to do so.
Is there any way, in which I can get this desired behaviour either by changing some config of flask-security or writing some wrapper function on top of this api.
Any useful suggestion will be very helpful. Thanks!

Django REST framework inconsistent data

When I request my user data from localhost:8000/users, I get users that look like this:
{
"username": "iv4ha05k",
"email": "xxxx#xxx.com",
"profile": {
"image": "http://localhost:8000/media/images-4.jpg"
},
"pk": 303
},
But, if I request user data from the django manage shell, the data will look like this:
{
"username": "iv4ha05k",
"email": "xxxx#xxx.com",
"profile": {
"image": "/media/images-4.jpg"
},
"pk": 303
},
Notice how the image url becomes truncated. I am querying the same exact database in the same exact state, and it has nothing to do with migrations or anything of the like. I discovered this issue when trying to write custom API views. When I would query the User viewset, it would return data with the full image address. But, when I try to write a custom API view that queries the user database, it will return it with the truncated address. I am using django's FileSystemStorage, if that helps anyone.. Thanks.

Categories