Home > Blockchain >  Using API-request in Python?
Using API-request in Python?

Time:01-27

i would like to use the api-request from this page:

https://www.jamesallen.com/loose-diamonds/all-diamonds/page-4/?Color=Y,J,I,H,G,F,E,D&Cut=Good,Very Good,Ideal,TrueHearts&Shape=all-diamonds&Clarity=I1,SI2,SI1,VS2,VS1,VVS2,VVS1,IF,FL&CaratFrom=0.05&IsLabDiamond=True

I think i found the request-url:

https://product-service.r2net.com/diamond/v/2/

In the payload-tab i also think that i found all the parameters which are used.

So i tried it with the following code -

But unfortunately i get an error in vscode that the payload-assignement is not correct (first { underlined in from payload =)

import requests
import pandas as pd

if __name__ == '__main__':
  headers =   {
      'appid':'jamesallen',
      'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36'
      }
  payload = {
        searchByIDs: (price:{from:200, to:5000000} 
        carat:{from:0.05, to:30} 
        page:{count:5, size:23, number:6}depth:{from:46, to:78}tableSize:{from:52, to:80}color:{from: 1 to: 24} cut:{from: 0 to: 4} clarity:{from: 1 to: 9} isLabDiamond:true shippingDays:999 ) {    
          total
          hits
          pageNumber
          numberOfPages
              items {
                
    categories
    productID
    sku
    productType
    generalType
    isExpressShipping
    galleryUrl
    title
    description
    itemID
    sku
    shippingDate
    shippingDates{
    shippingDate {
      date
      byValentines
      byNewYears
      byXmas
      isFastShipping
    } invShippingDate {
      date
      byValentines
      byNewYears
      byXmas
      isFastShipping
    }}
    isFirmShipping
    price
    salePrice
    originalPrice
    url
    metaTitle
    metaDescription
    targetSite
    status {
      id
      name
    }
    media {
      thumb
      hearts
      arrows
      gallery
      gallerySet
      galleryDisplayType
      stageDisplayType
      segomaPhotoID
      tab
      stage
      stageSet
      supperZoom
      supperZoomSet
      sideView
      cert
      cart
    }
    stone {
      simpleColor
      species
      variety
      treatment
      ratio
      isTrueHearts
      isGemstonePair
      fancyColorOvertone
      measurements
      measureLength
      measureWidth
      isLabDiamond
      isArgyle
      carat
      shape {
        id
        name
      }
      color {
        id
        name
        isFancy
      }
      cut {
        id
        name
      }
      clarity {
        id
        name
      }
      lab {
        id
        name
      }
      flour {
        id
        name
        fullName
      }
      symmetry {
        id
        name
        fullName
      }
      polish {
        id
        name
        fullName
      }
      colorIntensity {
        id
        name
      }
      depth
      tableSize
    }
    
              }
          }
        }

  url = "https://product-service.r2net.com/diamond/v/2/"

  resp = requests.post(url,data=json.dumps(payload), headers=headers).json()
  print(resp)

How can i use this api in python?

CodePudding user response:

As stated, your payload variable isn't a valid json. It also requires the "query" key.

Give this a try:

import requests

headers =   {
      'appid':'jamesallen',
      'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36'
      }
payload = {"query":"""{
        searchByIDs(price:{from:200, to:5000000} 
        carat:{from:0.05, to:30} 
        page:{count:5, size:23, number:6}depth:{from:46, to:78}tableSize:{from:52, to:80}color:{from: 1 to: 24} cut:{from: 0 to: 4} clarity:{from: 1 to: 9} isLabDiamond:true shippingDays:999 ) {    
          total
          hits
          pageNumber
          numberOfPages
              items {
                
    categories
    productID
    sku
    productType
    generalType
    isExpressShipping
    galleryUrl
    title
    description
    itemID
    sku
    shippingDate
    shippingDates{
    shippingDate {
      date
      byValentines
      byNewYears
      byXmas
      isFastShipping
    } invShippingDate {
      date
      byValentines
      byNewYears
      byXmas
      isFastShipping
    }}
    isFirmShipping
    price
    salePrice
    originalPrice
    url
    metaTitle
    metaDescription
    targetSite
    status {
      id
      name
    }
    media {
      thumb
      hearts
      arrows
      gallery
      gallerySet
      galleryDisplayType
      stageDisplayType
      segomaPhotoID
      tab
      stage
      stageSet
      supperZoom
      supperZoomSet
      sideView
      cert
      cart
    }
    stone {
      simpleColor
      species
      variety
      treatment
      ratio
      isTrueHearts
      isGemstonePair
      fancyColorOvertone
      measurements
      measureLength
      measureWidth
      isLabDiamond
      isArgyle
      carat
      shape {
        id
        name
      }
      color {
        id
        name
        isFancy
      }
      cut {
        id
        name
      }
      clarity {
        id
        name
      }
      lab {
        id
        name
      }
      flour {
        id
        name
        fullName
      }
      symmetry {
        id
        name
        fullName
      }
      polish {
        id
        name
        fullName
      }
      colorIntensity {
        id
        name
      }
      depth
      tableSize
    }
    
              }
          }
        }"""}

url = "https://product-service.r2net.com/diamond/v/2/"

resp = requests.post(url,json=payload, headers=headers).json()
print(resp)

Output:

print(resp)
{'data': {'searchByIDs': {'total': 44785, 'hits': 43633, 'pageNumber': 6, 'numberOfPages': 1898, 'items': [[{'categories': ['999'], 'productID': 12470344, 'sku': '12142391', 'productType': 'diamond', 'generalType': 'stone', 'isExpressShipping': False, 'galleryUrl': 'fancy-color-diamonds/yellow/', 'title': 'Lab-Created 0.40 Carat Fancy Vivid Yellow-VS1 Pear Shaped Diamond', 'description': 'This pear shaped 0.4 carat Lab-Created Fancy Vivid Yellow color vs1 clarity has a diamond grading report from IGI', 'itemID': 12142391, 'shippingDate': '2022-01-28T00:00:00.000Z', 'shippingDates': {'shippingDate': {'date': '2022-01-28T00:00:00.000Z', 'byValentines': True, 'byNewYears': False, 'byXmas': False, 'isFastShipping': True}, 'invShippingDate': None}, 'isFirmShipping': True, 'price': 290, 'salePrice': None, 'originalPrice': None, 'url': 'loose-diamonds/fancy-color/yellow/pear-shaped/0.40-carat-vs1-clarity-sku-12142391', 'metaTitle': None, 'metaDescription': None, 'targetSite': 'Jamesallen', 'status': {'id': 1, 'name': 'Available'}, 'media': {'thumb': 'sgmdirect/photoID/19018210/Diamond/12142391/Diamond-pear-0.4-Carat_1_first_.jpg', 'hearts': None, 'arrows': None, 'gallery': 'sgmdirect/photoID/19018210/Diamond/12142391/Diamond-pear-0.4-Carat_3_first_.jpg', 'gallerySet': 'sgmdirect/photoid/19018210/', 'galleryDisplayType': 'diamondGallery', 'stageDisplayType': 'StageBinary', 'segomaPhotoID': 19018210, 'tab': 'sgmdirect/photoID/19018210/Diamond/12142391/Diamond-pear-0.4-Carat_2_first_.jpg', 'stage': 'sgmdirect/photoID/19018210/Diamond/12142391/Diamond-pear-0.4-Carat_4_first_.jpg', 'stageSet': 'sgmdirect/photoid/19018210/', 'supperZoom': 'sgmdirect/photoID/19018210/Diamond/12142391/Diamond-pear-0.4-Carat_0_first_.jpg', 'supperZoomSet': 'sgmdirect/photoid/19018210/',...
  •  Tags:  
  • Related