I'm trying to implement testing my post route. It works in my project. I have problems only with pytest.
main.py:
#app.post('/create_service', status_code=status.HTTP_201_CREATED)
async def post_service(
response: Response, service: CreateService, db: Session = Depends(get_db)
):
service_model = models.Service()
service_name = service.name
service_instance = db.query(models.Service).filter(
models.Service.name == service_name
).first()
if service_instance is None:
service_model.name = service_name
db.add(service_model)
db.commit()
serviceversion_model = models.ServiceVersion()
service_instance = db.query(models.Service).filter(
models.Service.name == service_name
).first()
serviceversion_instance = db.query(models.ServiceVersion).filter(
models.ServiceVersion.service_id == service_instance.id
).filter(models.ServiceVersion.version == service.version).first()
if serviceversion_instance:
raise HTTPException(
status_code=400, detail='Version of service already exists'
)
serviceversion_model.version = service.version
serviceversion_model.is_used = service.is_used
serviceversion_model.service_id = service_instance.id
db.add(serviceversion_model)
db.commit()
db.refresh(serviceversion_model)
service_dict = service.dict()
for key in list(service_dict):
if isinstance(service_dict[key], list):
sub_dicts = service_dict[key]
if not sub_dicts:
response.status_code = status.HTTP_400_BAD_REQUEST
return HTTPException(status_code=400, detail='No keys in config')
servicekey_models = []
for i in range(len(sub_dicts)):
servicekey_model = models.ServiceKey()
servicekey_models.append(servicekey_model)
servicekey_models[i].service_id = service_instance.id
servicekey_models[i].version_id = serviceversion_model.id
servicekey_models[i].service_key = sub_dicts[i].get('service_key')
servicekey_models[i].service_value = sub_dicts[i].get('service_value')
db.add(servicekey_models[i])
db.commit()
return 'created'
test_main.py:
def test_create_service(client, db: Session = Depends(get_db)):
key = Key(service_key='testkey1', service_value='testvalue1')
service = CreateService(
name="testname1",
version="testversion1",
is_used=True,
keys=[key, ]
)
response = client.post("/create_service", params={"service": service.dict()})
assert response.status_code == 200
I tried to post service as json, as CreateService instance and finally as params dictionary. I have no errors at response line only with the last one . But I got 422 response status code. What is wrong?
If it can help:
schemas.py
class Key(BaseModel):
service_key: str
service_value: str
class CreateService(BaseModel):
name: str
version: str
is_used: bool
keys: list[Key]
class Config:
orm_mode = True
Don't use params. Use json:
response = client.post("/create_service", json=service.dict())
I am attempting to make an HTTP PUT request from XLRelease to update data in Adobe Workfront. I have been able to successfully login using the API client and GET data. I have also been able to successfully update data using Postman as well as using a native Python script. I am using the HttpRequest library within XLR. I am receiving the same response back in XLR as I am when successfully updating when using Postman, however, the data is not updated when using XLR.
My code is as follows:
import json
WORKFRONT_API_HOST = releaseVariables['url']
WORKFRONT_API_VERSION = releaseVariables['wfApiVersion']
WORKFRONT_API_KEY = releaseVariables['apiKey']
WORKFRONT_USERNAME = releaseVariables['wfUsername']
FI_ID = releaseVariables['target_customer_id']
newPortfolioId = releaseVariables['target_portfolio_id']
WORKFRONT_API_URL = WORKFRONT_API_HOST + WORKFRONT_API_VERSION
def wfLogin():
sessionID = ""
login_endpoint = "/login"
login_request = HttpRequest({'url': WORKFRONT_API_URL})
login_response = login_request.get(login_endpoint + "?apiKey=" + str(WORKFRONT_API_KEY).replace("u'","'") + "&username=" + WORKFRONT_USERNAME, contentType='application/json')
if login_response.getStatus() != 200:
print('# Error logging into WF\n')
print(login_response.getStatus())
print(login_response.errorDump())
sys.exit(1)
else:
json_response = json.loads(login_response.getResponse())
print ("Logged in to WF")
sessionID = json_response['data']['sessionID']
return sessionID
def wfLogout(sessionID):
logout_endpoint = "/logout"
logout_request = HttpRequest({'url': WORKFRONT_API_URL})
logout_response = logout_request.get(logout_endpoint + "?sessionID=" + sessionID, contentType='application/json')
if logout_response.getStatus() != 200:
print('# Error logging out of WF\n')
print(logout_response.getStatus())
print(logout_response.errorDump())
sys.exit(1)
else:
json_response = json.loads(logout_response.getResponse())
print ("Logged out of WF")
result = []
session_id = wfLogin()
if session_id != "":
customer_request = HttpRequest({'url': WORKFRONT_API_URL})
endpoint = '/prgm/%s?sessionID=%s&portfolioID=%s&customerID=%s' % (FI_ID, session_id, newPortfolioId, FI_ID)
jsonObj = "{}"
payload = {}
customer_response = customer_request.put(endpoint, jsonObj, contentType='application/json')
if customer_response.getStatus() != 200:
print('# Error connecting to WF\n')
print(customer_response)
print(customer_response.getStatus())
print(customer_response.errorDump())
sys.exit(1)
else:
response_json = json.loads(customer_response.getResponse())
print ("response_json: ", response_json)
#log out of current session
wfLogout(session_id)
else:
print ("No sessionID is available")
sys.exit(1)
Trying to hit Jersey multipart service with httpclient, and seeing some issues. Could you please share your insights to resolve this issue. Below I posted client code, service, stack trace.
Thanks for your support.
It works good when I use below client and register classes. Not finding any facility to register these classes for http client.
javax.ws.rs.client.Client
client.register(JacksonJsonProvider.class);
client.register(MultiPartFeature.class);
CLIENT CODE:
final String CONTENT_TYPE_MULTIPART = "multipart/related";
final String CONTENT_TYPE = "application/octet-stream";
final String BOUNDARY = "--upload_boundary--";
String responseStr = "";
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create().setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntityBuilder.addBinaryBody("file_upload", inputStream, ContentType.create(CONTENT_TYPE), "filename");
HttpPost httpPost = new HttpPost(finalURL);
httpPost.setHeader(HttpHeaders.CONTENT_TYPE, CONTENT_TYPE_MULTIPART);//+";type="+CONTENT_TYPE+";boundary="+BOUNDARY);
httpPost.setEntity(multipartEntityBuilder.build());
CloseableHttpResponse response = null;
try {
response = httpclient.execute(httpPost);
HttpEntity entity = response.getEntity();
responseStr = entity.toString();
RestHelper.verifyResponse(response, responseStr);
} catch (ClientProtocolException e) {
LOGGER.error("ClientProtocolException during upload",e);
} catch (IOException e) {
LOGGER.error("IOException during upload",e);
} finally {
response.close();
httpclient.close();
}
REST SERVICE:
#Consumes(MULTIPART_RELATED)
public String addDocument(MultiPart multipart)
STACKTRACE:
httpResponse :::::::::::::::::::: HttpResponseProxy{HTTP/1.1 400 Bad Request [Content-Type: text/html;charset=ISO-8859-1, $WSEP: , Content-Language: en-US, Transfer-Encoding: chunked, X-Cnection: Close, Date: Mon, 10 May 2021 11:50:32 GMT, Set-Cookie: dev-issapps.us=2036276652.6195.0000; path=/; Httponly] ResponseEntityProxy{[Content-Type: text/html;charset=ISO-8859-1,Chunked: true]}}
17:20:32.220 [main] ERROR us.dc.httpproxy.RestHelper - Response Error: RestException{us.dc.httpproxy.RestException
: HTTP/1.1 400 Bad Request', statusCode=400, detail='ResponseEntityProxy{[Content-Type: text/html;charset=ISO-8859-1,Chunked: true]}'}
17:20:32.221 [main] ERROR us.dc.httpproxy.RestClient - Exception in executeMultiPartRequest http://XXXXXXXXXXXXXXXX/XXXXXXXXXXXXXXXX/api/applications/GWEW/documents
RestException{ us.dc.httpproxy.RestException: HTTP/1.1 400 Bad Request', statusCode=400, detail='ResponseEntityProxy{[Content-Type: text/html;charset=ISO-8859-1,Chunked: true]}'}
Here is the solution, hope it helps somebody:
compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.13'
compile group: 'org.apache.httpcomponents', name: 'httpmime', version: '4.5.3'
try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
File file = new File("src/main/resources/48-1.jpg");
MultipartEntityBuilder entitybuilder = MultipartEntityBuilder.create();
entitybuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
entitybuilder.addBinaryBody("image", new FileInputStream(file), ContentType.APPLICATION_OCTET_STREAM, file.getName());
entitybuilder.setContentType(ContentType.create("multipart/related"));
HttpEntity mutiPartHttpEntity = entitybuilder.build();
RequestBuilder reqbuilder = RequestBuilder.post(url);
reqbuilder.setEntity(mutiPartHttpEntity);
HttpUriRequest multipartRequest = reqbuilder.build();
HttpResponse httpresponse = httpclient.execute(multipartRequest);
System.out.println("response status = " + httpresponse.getStatusLine().getStatusCode());
System.out.println("filenet id = " + EntityUtils.toString(httpresponse.getEntity()));
}catch(Exception e) {
e.printStackTrace();
}
We make a callout from one Salesforce org to another Salesforce org using the REST API. That worked until end of november. We didn't make any changes at the affected classes or configuration.
Now, while sending a request to the rest api a callout exception will be thrown with the message : "Unable to tunnel through proxy. Proxy returns "HTTP/1.0 503 Service Unavailable".
The authorisation to the rest api is done by session id.
Does anyone have any idea what the problem is?
Here the code snipped:
final String WS_ENDPOINT = 'https://login.salesforce.com/services/Soap/c/24.0';
final String REST_ENDPOINT = 'https://eu2.salesforce.com/services/apexrest/UsageReporterService';
final String USERNAME = '*****';
final String PASSWORD = '*****';
HTTP h = new HTTP();
HTTPRequest req = new HTTPRequest();
req.setMethod('POST');
req.setEndpoint(REST_ENDPOINT);
req.setHeader('Content-Type', 'application/json');
req.setTimeout(60000);
HTTP hLogin = new HTTP();
HTTPRequest reqLogin = new HTTPRequest();
reqLogin.setMethod('POST');
reqLogin.setEndpoint(WS_ENDPOINT);
reqLogin.setHeader('Content-Type', 'text/xml');
reqLogin.setHeader('SOAPAction', 'login');
reqLogin.setTimeout(60000);
reqLogin.setCompressed(false);
// get a valid session id
String sessionId;
String loginSoap = '<?xml version="1.0" encoding="UTF-8"?>';
loginSoap += '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:enterprise.soap.sforce.com">';
loginSoap += '<soapenv:Body>';
loginSoap += '<urn:login>';
loginSoap += '<urn:username>' + USERNAME + '</urn:username>';
loginSoap += '<urn:password>' + PASSWORD + '</urn:password>';
loginSoap += '</urn:login>';
loginSoap += '</soapenv:Body>';
loginSoap += '</soapenv:Envelope>';
reqLogin.setBody(loginSoap);
HTTPResponse respLogin;
try {
respLogin = hLogin.send(reqLogin);
} catch(CalloutException c){
return null;
}
System.debug('++++++'+respLogin.getStatus() + ': ' + respLogin.getBody());
Dom.Document doc = new Dom.Document();
doc.load(respLogin.getBody());
Dom.XMLNode root = doc.getRootElement();
String ns = root.getNamespace();
Dom.XMLNode bodyEl = root.getChildElements()[0];
if(bodyEl.getChildElements()[0].getName().equals('loginResponse')){
sessionId = bodyEl.getChildElements()[0].getChildElement('result', ns).getChildElement('sessionId', ns).getText();
}
// finished getting session Id
if(sessionId != null){ // login was successfull
req.setHeader('Authorization', 'Bearer ' + sessionId);
// serialize data into json string
UsageReporterModel usageReporterData = new UsageReporterModel();
String inputStr = usageReporterData.serialize();
req.setBody('{ "usageReportData" : ' + inputStr + '}');
// fire!
HTTPResponse resp;
try {
resp = h.send(req);
} catch(CalloutException c){
return null;
}
}
I suspect this will relate to a change of IP addresses for one of the org's which haven't been whitelisted correctly (or added to the "network access" object). With it being Salesforce to Salesforce I would hope that Salesforce.com support can assist?
According to this sample:
http://www.codeproject.com/KB/mobile/DeepCast.aspx
It's possible to request a gps coordinate (longitude & latitude) including range when sending cellid information (MCC, MNC, towerid, etc)
Can someone tell me the actual parameter to request/post to this address?
http://www.google.com/glm/mmap
It could be something like this
http://www.google.com/glm/mmap?mcc=xxx&mnc=xxx&towerid=xxx
And i would like to know what response we would get.
I have observe OpenCellid website and they provide some nice API to begin with, but i want to know about that in google map too (since they have more completed database).
OpenCellID API
Here is example for work with
#!/usr/bin/python
country = 'fr'
#device = 'Sony_Ericsson-K750'
device = "Nokia N95 8Gb"
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
mmap_url = 'http://www.google.com/glm/mmap'
geo_url = 'http://maps.google.com/maps/geo'
from struct import pack, unpack
from httplib import HTTP
import urllib2
def fetch_latlong_http(query):
http = HTTP('www.google.com', 80)
http.putrequest('POST', '/glm/mmap')
http.putheader('Content-Type', 'application/binary')
http.putheader('Content-Length', str(len(query)))
http.endheaders()
http.send(query)
code, msg, headers = http.getreply()
result = http.file.read()
return result
def fetch_latlong_urllib(query):
headers = { 'User-Agent' : user_agent }
req = urllib2.Request(mmap_url, query, headers)
resp = urllib2.urlopen(req)
response = resp.read()
return response
fetch_latlong = fetch_latlong_http
def get_location_by_cell(cid, lac, mnc=0, mcc=0, country='fr'):
b_string = pack('>hqh2sh13sh5sh3sBiiihiiiiii',
21, 0,
len(country), country,
len(device), device,
len('1.3.1'), "1.3.1",
len('Web'), "Web",
27, 0, 0,
3, 0, cid, lac,
0, 0, 0, 0)
bytes = fetch_latlong(b_string)
(a, b,errorCode, latitude, longitude, c, d, e) = unpack(">hBiiiiih",bytes)
latitude = latitude / 1000000.0
longitude = longitude / 1000000.0
return latitude, longitude
def get_location_by_geo(latitude, longitude):
url = '%s?q=%s,%s&output=json&oe=utf8' % (geo_url, str(latitude), str(longitude))
return urllib2.urlopen(url).read()
if __name__ == '__main__':
print get_location_by_cell(20465, 495, 3, 262)
print get_location_by_cell(20442, 6015)
print get_location_by_cell(1085, 24040)
print get_location_by_geo(40.714224, -73.961452)
print get_location_by_geo(13.749113, 100.565327)
You could use the Google Location API which is used by Firefox (Example see at http://www.mozilla.com/en-US/firefox/geolocation/ ) which has the url www.google.com/loc/json/. In fact this is JSON based webservice and a minimal Perl Example Look like this:
use LWP;
my $ua = LWP::UserAgent->new;
$ua->agent("TestApp/0.1 ");
$ua->env_proxy();
my $req = HTTP::Request->new(POST => 'https://www.google.com/loc/json');
$req->content_type('application/jsonrequest');
$req->content('{"cell_towers": [{"location_area_code": "8721", "mobile_network_code": "01", "cell_id": "7703", "mobile_country_code": "262"}], "version": "1.1.0", "request_address": "true"}');
# Pass request to the user agent and get a response back
my $res = $ua->request($req);
# Check the outcome of the response
if ($res->is_success) {
print $res->content;
} else {
print $res->status_line, "\n";
return undef;
}
Please keep in mind that Google has not officially opened this API for other uses...
The new place for the Google location API is the following :
https://developers.google.com/maps/documentation/geolocation/intro
With this API, you can retrieve a location from Cell information (cellid, mcc, mnc, and lac)
Base on GeolocationAPI, here are some parts of my code:
import java.io.IOException;
import java.io.StringWriter;
import java.net.HttpURLConnection;
import java.net.URL;
//http://code.google.com/p/google-gson/
import com.google.gson.stream.JsonWriter;
...
/**
* Requests latitude and longitude from Google.
*
* #param gsmParams
* {#link GsmParams}
* #return an {#link HttpURLConnection} containing connection to Google
* lat-long data.
* #throws IOException
*/
public HttpURLConnection requestLatlongFromGoogle(GsmParams gsmParams)
throws IOException {
// prepare parameters for POST method
StringWriter sw = new StringWriter();
JsonWriter jw = new JsonWriter(sw);
try {
jw.beginObject();
jw.name("host").value("localhost");
jw.name("version").value("1.1.0");
jw.name("request_address").value(true);
jw.name("cell_towers");
jw.beginArray().beginObject();
jw.name("cell_id").value(gsmParams.getCid());
jw.name("location_area_code").value(gsmParams.getLac());
jw.name("mobile_network_code").value(gsmParams.getMnc());
jw.name("mobile_country_code").value(gsmParams.getMcc());
jw.endObject().endArray().endObject();
} finally {
try {
jw.close();
} catch (IOException ioe) {
}
try {
sw.close();
} catch (IOException ioe) {
}
}
final String JsonParams = sw.toString();
final String GoogleLocJsonUrl = "http://www.google.com/loc/json";
// post request
URL url = null;
HttpURLConnection conn = null;
url = new URL(GoogleLocJsonUrl);
conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout((int) 30e3);
conn.setReadTimeout((int) 30e3);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.getOutputStream().write(JsonParams.getBytes());
conn.getOutputStream().flush();
conn.getOutputStream().close();
int resCode = conn.getResponseCode();
if (resCode == Http_BadRequest || resCode != Http_Ok) {
throw new IOException(String.format(
"Response code from Google: %,d", resCode));
}
return conn;
}
The object GsmParams is just a Java bean containing GSM parameters MCC, MNC, LAC, CID. I think you can create a same class easily.
After getting connection, you can call conn.getInputStream() and get results from Google Maps. Then use JsonReader to parse data...
As noted in other threads also check out https://labs.ericsson.com/apis/mobile-location/documentation/cell-id-look-up-api for a free cell-ID database to get coordinates from cellid, mcc, mnc, and lac .